//构成一棵树 1:不成环
// 2:只有一个入度为零的根节点,其他入度为1
// 没有节点为空树
# include <stdio.h>
# include <algorithm>
using namespace std;
int father[100010];
int vis[10010];
int into[100010];
int find(int x)
{
if(x==father[x])
return x;
return father[x]=find(father[x]);
}
int main()
{
int i,count,t1,t2,flag,cot,x,y,fx,fy;
count=0;
while(1)
{
cot=0;
memset(vis,0,sizeof(vis));
memset(into,0,sizeof(into));
flag=1;
for(i=1;i<=100010;i++)
father[i]=i;
while(scanf("%d%d",&x,&y))
{
if(x<0&&y<0)
break;
if(x==0&&y==0&&cot==0)
{
printf("Case %d is a tree.\n",++count);//空树
continue;
}
if(x==0&&y==0)
break;
cot++;//边数
vis[x]=1;
vis[y]=1;
into[y]++;//只有一个入度为零的根节点,其他入度为1
fx=find(x);
fy=find(y);
if(flag)
{
if(fx!=fy)
father[fy]=fx;
else //成环
flag=0;
}
}
if(x<0&&y<0)
break;
t1=0;
t2=0;
for(i=1;i<=10010;i++)
{
if(vis[i])
t1++;
}
for(i=1;i<=10010;i++)
{
if(into[i]==2)
flag=0;
}
if(flag&&t1==cot+1)
printf("Case %d is a tree.\n",++count);
else
printf("Case %d is not a tree.\n",++count);
}
return 0;
}
hdu 1325 &&poj1308 Is It A Tree?
最新推荐文章于 2021-10-05 21:17:13 发布