| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
也用的并差集为什么会wrong answer呢?帮忙看看#include<stdio.h>
#define max 100005
int p[max];
int link[max];
int opp[max];
//bool is[max];
int find(int x)
{
while(x!=p[x])x=p[x];
return x;
}
void unio(int a,int b)
{
int root1,root2;
root1=find(a);
root2=find(b);
if(link[root1]<link[root2])
p[root1]=root2;
else
{
p[root2]=root1;
if(link[root1]=link[root2])
link[root1]++;
}
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int flag=0;
int num,test,b,g;
int i;
char c[3];
scanf("%d%d",&num,&test);
for(i=0;i<=num;i++)
{
p[i]=i;
link[i]=1;
opp[i]=0;
// is[i]=false;
}
for(i=0;i<test;i++)
{
getchar();
scanf("%s",c);
// printf("fffffff%s\n",c);
if(c[0]=='A')
{
int root1,root2;
scanf("%d%d",&b,&g);
root1=find(b);
root2=find(g);
if(root1==root2)printf("In the same gang.\n");
else if(root1==find(opp[root2]))printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else
{
scanf("%d%d",&b,&g);
// is[b]=is[g]=true;
if(opp[b]!=0)
{
unio(opp[b],g);
//continue;
}
if(opp[g]!=0)
{
unio(opp[g],b);
//continue;
}
else
{
opp[g]=b;
opp[b]=g;
}
}
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator