您的位置:首页 > 其它

poj 1703 种类并查集

2014-09-28 17:05 435 查看
切点

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int n,m;
char ts[10];
int parent[200020];
int root(int p){
if(parent[p]==-1) return p;
else return parent[p]=root(parent[p]);
}
void merge(int a,int b){
a=root(a);
b=root(b);
parent[a]=b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j;
int ta,tb;
memset(parent,-1,sizeof(parent));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%s%d%d",ts,&ta,&tb);
if(ts[0]=='D')
{
if(root(ta*2)!=root(tb*2+1))
merge(ta*2,tb*2+1);
if(root(ta*2+1)!=root(tb*2))
merge(ta*2+1,tb*2);
}
else
{

if(root(ta*2)==root(tb*2))
printf("In the same gang.\n");
else if(root(ta*2)==root(tb*2+1))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: