题目大意:
在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙 输入D x y代表x于y不在一个团伙里 输入A x y问x与y是否在同一团伙
输入t组,n人,m条语句
N (N <= 10^5)
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int par[100005*2]; int F(int x) { int i=x; while(x!=par[x]) x=par[x]; par[i]=x; return x; } void join(int x,int y) { int xx=F(x); int yy=F(y); par[xx]=yy; } int main() { int n,m,i,t,a,b; char c; cin>>t; while(t--) { cin>>n>>m; for(i=1; i<=n*2; i++) par[i]=i; while(m--) { cin>>c; scanf("%d%d",&a,&b); if(c=='A') { if(F(a)==F(b)) puts("In the same gang."); else if(F(a+n)==F(b)) puts("In different gangs."); else puts("Not sure yet."); } else { join(a,b+n); join(a+n,b); } } } return 0; }