DZY Loves Chinese II 题解
Description
给出一个无向图,每次求删掉若干条边后是否联通,强制在线。
\(m\le 5\times 10^5,n\le 10^5\)
Solution
挺有意思的,我们可以考虑随机化,先随便构造出一棵树,那么不连通当且仅当存在一条树边使得跨过这条边的边都被删了,那么我们可以考虑给非树边一个随即权值,然后树边的权值设为跨过它的非树边权值异或和,每次只需要判断是否存在异或和等于 \(0\) 的非空集合即可。
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define MAXN 500005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int n,m,fa[MAXN];
int findSet (int x){return fa[x] == x ? x : fa[x] = findSet (fa[x]);}
struct edge{
int u,v,val;
}e[MAXN];
bool ing[MAXN];
#define pii pair<int,int>
#define se second
#define fi first
vector <pii> g[MAXN];
int par[MAXN],dep[MAXN],det[MAXN];
void dfs (int u,int fa){
par[u] = fa;
for (pii it : g[u]) if (it.fi ^ fa) dfs (it.fi,u);
}
void dfs1 (int u,int fa){
for (pii it : g[u]) if (it.fi ^ fa) dfs1 (it.fi,u),e[it.se].val = det[it.fi],det[u] ^= det[it.fi];
}
int d[31];
void ins (int &v){
for (Int i = 30;~i;-- i) if (v >> i & 1){
if (d[i]) v ^= d[i];
else{
d[i] = v;
break;
}
}
}
signed main(){
srand (time(NULL)),read (n,m);
for (Int u = 1;u <= n;++ u) fa[u] = u;
for (Int i = 1;i <= m;++ i){
read (e[i].u,e[i].v);
if (findSet (e[i].u) == findSet (e[i].v)) e[i].val = rand();
else
fa[fa[e[i].u]] = fa[e[i].v],
g[e[i].u].push_back ({e[i].v,i}),
g[e[i].v].push_back ({e[i].u,i});
}
dfs (1,0);
for (Int i = 1;i <= m;++ i) if (e[i].val) det[e[i].u] ^= e[i].val,det[e[i].v] ^= e[i].val;
dfs1 (1,0);
int q,cnt = 0;read (q);
while (q --> 0){
int K;read (K),memset (d,0,sizeof (d));bool flg = 0;
for (Int i = 1,v;i <= K;++ i) read (v),v ^= cnt,v = e[v].val,ins (v),flg |= (v == 0);
cnt += (!flg),puts (flg ? "Disconnected" : "Connected");
}
return 0;
}