POJ 1733 并查集 偏移向量
这题也是用到了偏移向量
一个由0,1组成的数字串~~,现在你问一个人一些问题,第i位到第j位的1的个数为奇数还是偶数。他会告诉你答案, 但是答案可能会自相矛盾,现在就是最多能有前几个回答是不矛盾的。
设r[i]表示第1位到第i位的1个数的奇偶状况,r[i] = 0表示有偶数个1,r[i] = 1表示有奇数个1。
那么要是第i位到第j位为偶数个1时,r[i-1] = 1, r[j] = 1 或r[i-1] = 0, r[j] = 0 所以 r[i-1] ^ r[j] = 0
要是为奇数个1时,r[i-1] = 1, r[j] = 0 或 r[i-1] = 0, r[j] = 1所以r[i-1] ^ r[j] = 1
那么我们可以使用并查集,用一个数组d[i]代表r[i]与其祖先的异或值
如果回答的两个区间的端点以前都出现过,那么就判断异或值是否与以前的那个相等
如果没出现过,就直接合并,更新异或值
[cpp]
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#define MAXN 22222
#define MAXM 55555
#define INF 100000000
using namespace std;
int n, m;
int fa[MAXN];
int l[MAXN], r[MAXN], c[MAXN];
int x[MAXN], d[MAXN];
int find(int x)
{
if(fa[x] == x) return x;
int t = find(fa[x]);
d[x] ^= d[fa[x]];
fa[x] = t;
return t;
}
bool join(int x, int y, int v)
{
int fx = find(x);
int fy = find(y);
if(fx == fy) return ((d[x] ^ d[y]) == v);
else
{
fa[fx] = fy;
d[fx] = d[x] ^ d[y] ^ v;
return 1;
}
}
int bin(int low, int high, int v)
{
while(low <= high)
{
int mid = (low + high) >> 1;
if(x[mid] == v) return mid;
else if(x[mid] > v) high = mid - 1;
else low = mid + 1;
}
return -1;
}
int main()
{
char s[15];
scanf("%d%d", &n, &m);
int cnt = 0;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%s", &l[i], &r[i], s);
x[cnt++] = l[i] - 1;
x[cnt++] = r[i];
if(s[0] == 'e') c[i] = 0;
else c[i] = 1;
}
sort(x, x + cnt);
cnt = unique(x, x + cnt) - x;
for(int i = 1; i < MAXN; i++) fa[i] = i, d[i] = 0;
int fg = 0;
for(int i = 1; i <= m; i++)
{
int L = bin(0, cnt - 1, l[i] - 1) + 1;
int R = bin(0, cnt - 1, r[i]) + 1;
if(!join(L, R, c[i]))
{
fg = 1;
printf("%d\n", i - 1);
break;
}
}
if(!fg) printf("%d\n", m);
return 0;
}
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#define MAXN 22222
#define MAXM 55555
#define INF 100000000
using namespace std;
int n, m;
int fa[MAXN];
int l[MAXN], r[MAXN], c[MAXN];
int x[MAXN], d[MAXN];
int find(int x)
{
if(fa[x] == x) return x;
int t = find(fa[x]);
d[x] ^= d[fa[x]];
fa[x] = t;
return t;
}
bool join(int x, int y, int v)
{
int fx = find(x);
int fy = find(y);
if(fx == fy) return ((d[x] ^ d[y]) == v);
else
{
fa[fx] = fy;
d[fx] = d[x] ^ d[y] ^ v;
return 1;
}
}
int bin(int low, int high, int v)
{
while(low <= high)
{
int mid = (low + high) >> 1;
if(x[mid] == v) return mid;
else if(x[mid] > v) high = mid - 1;
else low = mid + 1;
}
return -1;
}
int main()
{
char s[15];
scanf("%d%d", &n, &m);
int cnt = 0;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%s", &l[i], &r[i], s);
x[cnt++] = l[i] - 1;
x[cnt++] = r[i];
if(s[0] == 'e') c[i] = 0;
else c[i] = 1;
}
sort(x, x + cnt);
cnt = unique(x, x + cnt) - x;
for(int i = 1; i < MAXN; i++) fa[i] = i, d[i] = 0;
int fg = 0;
for(int i = 1; i <= m; i++)
{
int L = bin(0, cnt - 1, l[i] - 1) + 1;
int R = bin(0, cnt - 1, r[i]) + 1;
if(!join(L, R, c[i]))
{
fg = 1;
&nbs
补充:软件开发 , C++ ,