pku 1703 Find them,Catch them

一般的种类并查集,只不过犯了点小错误,一直TLE

View Code
#include<stdio.h>
#include
<string.h>
int fa[100005],rank[100005],n;
void init()
{
for(int i=1;i<=n;i++)
{
fa[i]
=i;
rank[i]
=0;
}
}
int find(int x)
{
if(fa[x]==x) return x;
int tx=find(fa[x]);
rank[x]
=(rank[x]+rank[fa[x]])%2;
fa[x]
=tx;
return fa[x];
}
void unio(int x,int y,int tx,int ty)
{
fa[tx]
=ty;
rank[tx]
=(rank[x]+rank[y]+1)%2;
}
int main()
{
int t,a,b,m;
char str[5];
scanf(
"%d",&t);
while(t--)
{
scanf(
"%d%d",&n,&m);
init();
while(m--)
{

scanf(
"%s%d%d",str,&a,&b);
int ta=find(a);
int tb=find(b);
if(str[0]=='A')
{
if(ta!=tb)
{
printf(
"Not sure yet.\n");
}
else
{
if(rank[a]==rank[b])
{
printf(
"In the same gang.\n");
}
else printf("In different gangs.\n");
}
}
elseif(str[0]=='D')
{
if(ta!=tb)
unio(a,b,ta,tb);
}
}
}
return0;
}

 

并查集进阶!!!! http://www.orzminjie.info/?p=188001

posted @ 2011-09-04 16:07  Because Of You  Views(201)  Comments(0Edit  收藏  举报