Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21108 | Accepted: 6259 |
Description
Input
Output
Sample Input
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
Source
//有了食物链那题的基础、这题就思路清晰了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define Y 100003
using namespace std;
int f[Y],r[Y];
bool p[Y];
int Find(int x)
{
if(x!=f[x])
{
int tf=f[x];
f[x]=Find(f[x]);
p[x]=!(p[x]^p[tf]);//压缩路径的规律
return f[x];
}
return x;
}
void union_set(int x,int y)
{
int rx=Find(x),ry=Find(y);
if(r[rx]>r[ry])
{
f[ry]=rx;
p[ry]=(p[x]+p[y])%2;//合并的规律
}
else if(r[rx]<r[ry])
{
f[rx]=ry;
p[rx]=(p[x]+p[y])%2;
}
else
{ f[ry]=rx;r[rx]++;
p[ry]=(p[x]+p[y])%2;
}
}
int main()
{
int T;
int N,M;
char c;
int x,y,r1,r2;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
f[i]=i,r[i]=p[i]=1;
while(M--)
{
getchar();
scanf("%c",&c);
scanf("%d%d",&x,&y);
if(N==2&&c=='A'&&x!=y)//特殊情况 2个人 A 1 2是可以确定的
{
printf("In different gangs.\n");continue;
}
if(c=='D')
union_set(x,y);
else
{
r1=Find(x);
r2=Find(y);
if(r1!=r2)
printf("Not sure yet.\n");
else
{
if(p[x]==p[y])
printf("In the same gang.\n");
else
printf("In different gangs.\n");//到这我不得不说句:你妹的!!!,少写了个s,WA了几次,晕
}
}
}
}
return 0;
}