Graph Games

Graph Games

题目大意

给N个点,M条边,所有边从1到M编号

定义集合S(x)为从点x可以通过一条边到达的所有点

给Q次操作,有以下两种操作

  1. (1,l,r)将闭区间(l,r)上的边反转——有边变为无边,无边变为有边

  2. (2,u,v)判断S(u)和S(v)是否相等

N<=1e5,M<=2e5,Q<=2e5

思路

对于操作1,我们可以对边分块,对单点直接异或记在点上,对整块批量打上标记即可。

对于操作2,我们用哈希表示每个边的值,点在各块的值用单块内边的异或和表示,所有区间的和与两点异或和相同即为相同。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
int b[N],block,num,id[N],tag[N],u[N],v[N];
int n,m,q;
long long sum[500][N],s[N];
void init() {
int x,y;
scanf("%d%d",&n,&m);
block = sqrt(m);
num = (m - 1) / block + 1;
for (int i = 1; i <= m; ++i)
b[i] = (i - 1) / block + 1;
for (int i = 1; i <= num; ++i) {
tag[i] = 1;
for (int j = 0; j <= n; ++j)
sum[i][j] = 0;
}
for (int i = 1; i <= n; ++i)
s[i] = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d%d",&x,&y);
u[i] = x;
v[i] = y;
sum[b[i]][x] ^= id[y];
sum[b[i]][y] ^= id[x];
}
}
void modify(int x, int y) {
for (int i = x; i <= min(y,b[x]*block); ++i) {
s[u[i]] ^= id[v[i]];
s[v[i]] ^= id[u[i]];
}
if (b[x] != b[y])
for (int i = (b[y] - 1) * block + 1; i <= y; ++i) {
s[u[i]] ^= id[v[i]];
s[v[i]] ^= id[u[i]];
}
for (int i = b[x] + 1; i <= b[y] - 1; ++i)
tag[i] ^= 1;
}

int main() {
int op,x,y;
srand(time(NULL));
for (int i = 0; i < N; ++i) id[i] = rand() + 1;//给边附上哈希值
int T;
scanf("%d",&T);
while(T--) {
init();
scanf("%d",&q);
while(q--) {
scanf("%d%d%d",&op,&x,&y);
if (op == 1) modify(x,y);
if (op == 2) {
long long ans1 = s[x], ans2 = s[y];
for (int i = 1; i <= num; ++i)
if (tag[i]) {
ans1 ^= sum[i][x];
ans2 ^= sum[i][y];
}
putchar(ans1==ans2?'1':'0');
}
}
putchar('\n');
}
return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!