DZY Loves Chinese II 題解
阿新 • • 發佈:2022-04-13
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; }