题意:两个帮派,A,查询两人是否为同一个帮派,三种输出结果
D,两人不在同一个帮派。
思路:将两个帮派设置为两个集合,f[d]为一个集合的代表,f[d+n]为另一个集合的代表
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=2e5+10;
int n,m;
int z,x,y;
int f[maxn];
inline void init(){
for(int i=1;i<=2*n;i++)
f[i]=i;
}
int get(int x){
if(f[x]==x)
return x;
return f[x]=get(f[x]);
}
void merge(int x,int y){
int t1=get(x);
int t2=get(y);
if(t1!=t2) f[t1]=t2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
while(m--){
char c;int x,y;
getchar();
scanf("%c%d%d",&c,&x,&y);
if(c=='A'){
if(get(x)==get(y)){
cout<<"In the same gang."<<endl;
}
else if(get(x)==get(y+n)){
cout<<"In different gangs."<<endl;
}
else{
cout<<"Not sure yet."<<endl;
}
}
else {
merge(x,y+n);
merge(x+n,y);
}
}
}
}