Description
给定一个无向联通图
G=(V,E)
,每次询问删去其中的
k
条边,询问图的连通性。每个询问独立。
Solution
考虑该图的一颗生成树,给所有非树边赋上一个随机权值,一条树边的权值就是所有覆盖自己的非树边权值的异或和。那么这个无向联通图变得不连通当且仅当存在一个删去边的子集权值异或和为零。
考虑维护这个东西,赋权值的过程只要在
dfs
树上打标记更新就好了。判断删去的边是否存在权值异或和为零的子集,只需要维护一个线性基就好啦。
时间复杂度
O(N+M+K×Q×|S|)
,
|S|
为线性基大小。
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 101010;
const int M = 505050;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
inline ll Rand(void) {
return (ll)rand() << 30 | rand();
}
struct LB {
ll a[70];
inline void Clear(void) {
for (int i = 0; i < 65; i++) a[i] = 0;
}
inline bool Insert(ll x) {
for (int i = 63; i >= 0; i--) {
if ((x >> i) & 1) {
if (!a[i]) {
a[i] = x; return true;
}
x ^= a[i];
}
}
return false;
}
};
struct edge {
int to, next;
ll key;
edge(int t = 0, int n = 0, ll k = 0):to(t), next(n), key(k) {}
};
edge G[M << 1];
int head[N], vis[N], id[M];
ll tag[N];
int n, m, q, k, Gcnt, x, y, ans, clc;
LB S;
inline void AddEdge(int from, int to, ll key = 0) {
G[++Gcnt] = edge(to, head[from], key); head[from] = Gcnt;
G[++Gcnt] = edge(from, head[to], key); head[to] = Gcnt;
}
inline void dfs1(int u, int fa) {
int to; vis[u] = 1;
for (int i = head[u]; i; i = G[i].next) {
to = G[i].to; if (to == fa) continue;
if (vis[to]) {
if (G[i].key) continue;
G[i].key = G[i ^ 1].key = Rand();
tag[to] ^= G[i].key;
tag[u] ^= G[i].key;
} else dfs1(to, u);
}
}
inline ll dfs2(int u) {
ll key = tag[u]; vis[u] = clc;
for (int i = head[u]; i; i = G[i].next) {
if (vis[G[i].to] == clc) continue;
key ^= (G[i].key = G[i ^ 1].key = dfs2(G[i].to));
}
return key;
}
int main(void) {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
srand(19260817);
read(n); read(m); Gcnt = 1;
for (int i = 0; i < m; i++) {
read(x); read(y);
AddEdge(x, y);
id[i + 1] = Gcnt;
}
dfs1(1, 0); clc = 2; dfs2(1);
read(q); bool flag;
for (int i = 1; i <= q; i++) {
read(k); S.Clear(); flag = 0;
for (int j = 1; j <= k; j++) {
read(x); x ^= ans;
if (flag) continue;
if (!S.Insert(G[id[x]].key)) flag = 1;
}
if (flag) puts("Disconnected");
else {
puts("Connected"); ans++;
}
}
return 0;
}