//636K 313MS
#include <cstdio>
const int N = 100005;
int n, m, f[N];
bool rank[N];
inline void init(){
for(int i=1; i<=n; ++i){
f[i]=i;
rank[i]=0; //初始化所有点都在二分图的一边
}
}
int find(int x){
if(x == f[x]) return f[x];
int fa = f[x];
f[x] = find(f[x]);
rank[x] = (rank[x] + rank[fa]) & 1; //模2操作
return f[x];
}
inline void Union(int x,int y){
int rx = find(x), ry = find(y);
if(rx != ry){
f[ry] = rx;
//rank[ry] = (rank[x] - rank[y] + 1) & 1;
if(rank[x] == rank[y]) rank[ry] = 1;
else rank[ry] = 0;
}
}
int main(){
int T,a,b,fa,fb;
char ch;
scanf("%d",&T);
while(T--){
scanf("%d%d%*c", &n, &m);
init();
for(int i=0; i<m; ++i){
scanf("%c%d%d%*c", &ch, &a, &b);
if(ch=='D'){
Union(a,b);
}
else{
fa = find(a), fb=find(b);
if(fa == fb){
if(rank[a]==rank[b]) puts("In the same gang.");
else puts("In different gangs.");
}
else puts("Not sure yet.");
}
}
}
return 0;
}
POJ 1703 Find them, Catch them 二分图 并查集
最新推荐文章于 2020-04-30 19:07:15 发布