感觉和butterfly(“http://blog.csdn.net/mmc2015/article/details/50153739”)完全一样,但还是WA,好蛋疼。。。
# include<iostream>
# include<stdio.h>
using namespace std;
# define N 100005
struct NODE
{
int p,r;//0表示和父节点相同,1表示不同
};
NODE node[N];
void MakeSet(int n)
{
for(int i=1;i<=n;i++)
{
node[i].p=i;
node[i].r=0;
}
}
int FindParent(int i)
{
if(i==node[i].p)
{
return i;
}
else
{
int ip=node[i].p;
node[i].p=FindParent(ip);
node[i].r= node[i].r==node[ip].r? 0:1;
return node[i].p;
}
}
int main()
{
int t,n,m,i,j,k,a,b,ap,bp;
char c;
//cin>>t;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
//cin>>n>>m;
scanf("%d%d",&n,&m);
MakeSet(n);
for(j=1;j<=m;j++)
{
//cin>>c>>a>>b;
getchar();
scanf("%c%d%d",&c,&a,&b);
ap=FindParent(a);
bp=FindParent(b);
if(c=='A')
{
if(ap!=bp)
{
cout<<"Not sure yet."<<endl;
}
else
{
if(node[a].r==node[b].r)
{
cout<<"In the same gang."<<endl;
}
else
{
cout<<"In different gang."<<endl;
}
}
}
else //D
{
if(ap!=bp)
{
node[bp].p=ap;
node[bp].r= node[a].r==node[b].r? 1:0;
}
}
}
}
return 0;
}