算法1:
线段树分治
对于区间[l,r] 把不涉及的边直接联通,然后对[l,mid],[mid+1,r]进行分治
这个过程可以用带撤销并查集做
$O(\sum clog\sum clogn)$(并查集复杂度貌似路径优化无法分析???)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
69
70
71
72
73
74
75
using namespace std;
struct did {
int *p, x;
inline int& add(int &d, int k) {
x = d;
d = k;
p = &d;
return d;
}
inline void ret() {
*p = x;
}
} st[20000000];
int top;
inline int& add(int &x, int k) {
return st[top++].add(x, k);
}
inline void ret(int lasttop) {
while(top > lasttop) st[--top].ret();
}
int fa[100010];
int u[200010], v[200010], n, m, k;
struct query {
int e[4], len;
} q[100010];
int used[200010];
int nowans;
inline int gf(int a) {return fa[a] < 0 ? a : add(fa[a], gf(fa[a]));}
inline void merge(int a, int b) {
a = gf(a), b = gf(b);
if(a == b) return;
add(nowans, nowans - 1);
if(fa[a] < fa[b]) swap(a, b);
add(fa[b], fa[a] + fa[b]);
add(fa[a], b);
}
inline void solve(int l, int r, vector<int> &vc) {
for(int i : vc) {
used[i] = 0;
}
for(int i = l; i <= r; i++)
for(int j = 0; j < q[i].len; j++) used[q[i].e[j]] = 1;
vector<int> use;
int lasttop = top;
for(int i : vc) {
if(used[i]) use.push_back(i);
else merge(u[i], v[i]);
}
if(l == r) {
if(nowans == 1) puts("Connected");
else puts("Disconnected");
}
else {
int mid = l + r >> 1;
solve(l, mid, use);
solve(mid + 1, r, use);
}
ret(lasttop);
}
int main() {
scanf("%d%d", &n, &m);
memset(fa, -1, sizeof fa);
for(int i = 1; i <= m; i++) scanf("%d%d", u + i, v + i);
scanf("%d", &k);
for(int i = 1; i <= k; i++) {
scanf("%d", &q[i].len);
for(int j = 0; j < q[i].len; j++) scanf("%d", q[i].e + j);
}
vector<int> v;
nowans = n;
for(int i = 1; i <= m; i++) v.push_back(i);
solve(1, k, v);
return 0;
}
算法2:
找一棵生成树
非树边随机权值
树边权值为覆盖的非树边的权值异或和
每次询问:
边的权值的集合为$S$,若存在$S$的子集异或和为0,则不连通(感性理解一下)
显然线性基维护即可
$O(n+m+logv\sum c)$