还是种类并查集!!
题目链接:
题目大意:
给出不属于同一集合的人的关系。给出询问并判断是不是同一个组织或者当前信息不能判断
其关系。
#include<cstdio>
#include<iostream>
using namespace std;
int fa[100010];
int re[100010];
void init(int n)
{
for(int i=1;i<=n;i++)
{
fa[i]=i;
re[i]=0;
}
}
int find(int x)
{
if(fa[x]!=x)
{
int t=fa[x];
fa[x]=find(fa[x]);
re[x]=(re[x]+re[t])%2;
}
return fa[x];
}
void Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fx]=fy;
re[fx]=(re[y]+2-re[x]+1)%2;
}
int main()
{
int T,n,m,x,y;
char u;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init(n);
getchar();
for(int i=0;i<m;i++)
{
scanf("%c%d%d",&u,&x,&y);
if(u=='D')
{
Union(x,y);
}
else
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
printf("Not sure yet.\n");
else
{
if((re[x]+re[y])%2)
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
}
getchar();
}
}
return 0;
}