http://acm.hdu.edu.cn/showproblem.php?pid=1325
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MA 1000100
#define PI 3.141592654
using namespace std;
typedef struct
{
int num,root,con;
}Node;
Node aa[MA];
void in()
{
for(int i=0;i<MA;i++)
{
aa[i].num=0;
aa[i].root=i;
aa[i].con=0;
}
}
int find_root(int x)
{
if(aa[x].root!=x)
return aa[x].root=find_root(aa[x].root);
return aa[x].root;
}
void Union(int x,int y)
{
int a=find_root(x);
int b=find_root(y);
if(a==b)
return ;
aa[b].root=a;
}
int main()
{
int n,m,i,j,k;
j=1;
bool flag=true;
in();
while(scanf("%d%d",&n,&m)&&n>=0&&m>=0)
{
if(!flag&&n!=0&&m!=0)
continue;
if(n==0&&m==0)
{
k=0;
for(i=0;i<MA;i++)
{
if(aa[i].num&&find_root(i)==i)
k++;
if(aa[i].con>1)
{
flag=false;
break;
}
}
if(k>1)
flag=false;
if(flag)
printf("Case %d is a tree.\n",j++);
else
printf("Case %d is not a tree.\n",j++);
flag=true;
in();
continue;
}
if(m!=n&&find_root(n)==find_root(m))
flag=false;
else
{
aa[m].num=1;
aa[n].num=1;
aa[m].con++;
Union(n,m);
}
}
return 0;
}