空树也是树,森林不是树……还有注意ca++,大囧……
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h>
int f[109];
int jin[109];
int find(int pos)
{
if(f[pos]==-1)return pos;
return f[pos]=find(f[pos]);
}
int un(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa==fb)return 0;
f[fa]=fb;return 1;
}
int main()
{
int i,j,ca=0;
while(1)
{
ca++;
for(i=1;i<=100;i++)
{
f[i]=-1;
jin[i]=0;
}
int t=0,add=0;
while(scanf("%d%d",&i,&j),i||j)
{
t++;
if((i==-1)&&(j==-1))
{
return 0;
}
if(un(i,j))
{
add++;
}
jin[j]++;
}
if(t==0&&i==0&&j==0)
{
printf("Case %d is a tree.\n",ca);
continue;
}
for(i=1;i<=100;i++)
{
if(jin[i]>=2)
break;
}
int a=0,set,temp;
for(j=1;j<=100;j++)
{
temp=find(j);
if(a==0&&f[j]!=-1)
{
set=temp;
a=1;
}
if(a==1&&f[j]!=-1)
{
if(set!=temp)
break;
}
}
if(add==t&&i==101&&j==101)
printf("Case %d is a tree.\n",ca);
else
printf("Case %d is not a tree.\n",ca);
}
}