题面
题解
这题有个结论,恐怕只能记下来,场上大概率是来不及证的
所以说我们可以给每条边随机边权,然后用 S e S_e Se 内的边权异或和等效 S e S_e Se ,用异或线性基判断是否存在异或和等于 0 的子集。
不过要注意的是,只有在 P P P 的大小比 ω \omega ω(随机边权的有效位数) 小得多时正确性较高,否则,由于加入了 > ω >\omega >ω 个数后一定存在异或等于 0 的非空子集,正确性就完全不对了。
所以,这个做法也只适用于这种
k
k
k 很小的题,要是稍微大一点,可以用较高位数的 bitset
卡一卡,如果没有限制,就只能找别的方法了。
时间复杂度 O ( m + q ω ) O(m+q\omega) O(m+qω) 。
简单口糊下证明:
可以先证必要性,对于确认孤立的连通块,存在 Q Q Q 使得每条跨过孤岛的非树边在 S e S_e Se 中出现两次,
再证充分性,只考虑 ∉ P \not\in P ∈P 的非树边的贡献,对连通块二染色即可,不同色的连通块之间无边。
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 100005
#define MAXM 500005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
#define UIN unsigned int
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
inline LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {if(!x)return ;putpos(x/10);putchar((x%10)^48);}
inline void putnum(LL x) {
if(!x) {putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
inline void AIput(LL x,int c) {putnum(x);putchar(c);}
int n,m,s,o,k;
int U[MAXM],V[MAXM];
ULL tg[MAXM],nm[MAXM];
bool f[MAXN];
vector<PR> g[MAXN];
int d[MAXN],fa[MAXN],fe[MAXN];
ULL c[MAXN];
void dfs0(int x,int ff) {
f[x] = 1;d[x] = d[fa[x] = ff] + 1;
for(auto &y:g[x]) {
if(!f[y.FI]) {
dfs0(y.FI,x);
fe[y.FI] = y.SE;
}
} return ;
}
void dfs(int x) {
for(auto &y:g[x]) {
if(d[y.FI] == d[x] + 1) {
dfs(y.FI);
c[x] ^= c[y.FI];
}
}
if(fe[x]) nm[fe[x]] = c[x];
return ;
}
mt19937 rd(114514);
ULL bt[64];
void clearbit() {memset(bt,0,sizeof(bt));}
bool ins(ULL x) {
for(int i = 63;i >= 0;i --) {
if((x>>i)&1) {
if(bt[i]) x ^= bt[i];
else return bt[i] = x,0;
}
} return 1;
}
int main() {
int Ty = 1;
n = read();m = read();
for(int i = 1;i <= m;i ++) {
s = U[i] = read(); o = V[i] = read();
g[s].push_back({o,i});
g[o].push_back({s,i});
tg[i] = rd(); tg[i] <<= 32;
tg[i] ^= rd();
}
dfs0(1,0);
for(int i = 1;i <= m;i ++) {
if(d[U[i]] > d[V[i]]) swap(U[i],V[i]);
if(fa[V[i]] != U[i]) {
c[V[i]] ^= tg[i];
c[U[i]] ^= tg[i];
nm[i] = tg[i];
}
}
dfs(1);
int Q = read(),las = 0;
for(int i = 1;i <= Q;i ++) {
k = read();
clearbit();
bool flag = 0;
while(k --) {
s = read()^(Ty*las);
flag |= ins(nm[s]);
}
if(!flag) {
printf("Connected\n");
las ++;
}
else printf("Disconnected\n");
}
return 0;
}