大意看收藏的博客吧,就是每个人都会有一个帮派。。。。
A
题意
给你n个人属于两个帮派 不知道每个人属于哪个,然后d xy 就是xy 不在一个帮派 A XY问xy是否在一个帮派
思路
只有两个帮派是关键,d x,y 就把x 和 y+n 绑定在一个帮派 x+n 和y 绑定在一个帮派 如果再来个d cy 那么 c 和y +n 是一个帮派 那么 x 和c 就是一个了
代码
#include <iostream>
#include <cstdio>
using namespace std;
int pre[200010];
int find1(int root)
{
int son,temp;
son=root;
while(root!=pre[root])
root=pre[root];//寻找掌门
/* while(son != root) //路径压缩
{
temp = pre[son];
pre[son] = root;
son = temp;
}*/
return root; //掌门驾到~
}
int find(int x)
{
if(pre[x]==x) return x;
else
return pre[x]=find(pre[x]);
}
void unit(int x,int y)//联合
{
x=find(x);
y=find(y);
if(x==y)
return ;
else
pre[y]=x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
getchar();
for(int i=1;i<=2*n;i++)
pre[i]=i;//各自都是自己的掌门
while(m--)
{
char a;
int x,y;
scanf("%c %d %d",&a,&x,&y);
getchar();
if(a=='D')
{
unit(x+n,y);
unit(x,y+n);
}
else
{
if(find(x)==find(y))
cout<<"In the same gang."<<endl;
else if(find(x)==find(y+n))
cout<<"In different gangs."<<endl;
else cout<<"Not sure yet."<<endl;
}
}
}
return 0;
}