查并集 在hdu过不了~~pku过了
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int p[100005];
int vis[100005];
void make_set(int i)
{
p[i]=i;
}
int find_set(int i)
{
int j=i;
if(j!=p[j])
{
j=p[j];
}
return p[i]=j;
}
int Union(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return 0;
p[x]=y;
return 1;
}
int main()
{
int a,b;
int n1=0,s=0,n2=0;
int cas=1;
while(scanf("%d%d",&a,&b),a>=0&&b>=0)
{
if(a==0&&b==0)
{
if(n1==0&&n2==0&&s==0)
{
printf("Case %d is a tree.\n",cas++);
continue;
}
if(n1==s-1&&n2==0)
printf("Case %d is a tree.\n",cas++);
else
printf("Case %d is not a tree.\n",cas++);
memset(p,0,sizeof(p));
memset(vis,0,sizeof(vis));
n1=n2=s=0;
continue;
}
if(!vis[a])
{
s++;
vis[a]=1;
make_set(a);
}
if(!vis[b])
{
s++;
vis[b]=1;
make_set(b);
}
if(Union(a,b))
{
n1++; //判断是否连通
}
else
n2++; //判断环
}
return 0;
}