#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=10000;
int p[maxn];
int vis[maxn];
int R[maxn];
void init()
{
for(int i=0; i<=maxn; i++)
{
p[i]=i;
}
memset(vis,0,sizeof(vis));
memset(R,0,sizeof(R));
}
int fin_d(int x)
{
int r=x,k,j;
while(r!=p[r])
{
r=p[r];
}
k=x;
while(p[k]!=r)
{
j=p[k]; //保存下一节点
p[k]=r; //压缩
k=j; //接收下一节点
}
return r;
}
void join(int x,int y)
{
int fx=fin_d(x),fy=fin_d(y);
if(fx!=fy)
p[fy]=fx;
}
int main()
{
int x,y,Case=1;
init();
int cnt=0,count=0;
bool flag=true;
while(cin>>x>>y)
{
if(x<0&&y<0)
break;
if(x==y&&x!=0)
{
flag=false;
}
if(x==y&&x==0)
{
if(cnt==0)
{
printf("Case %d is a tree.\n",Case++);
continue;
}
else
{
for(int i=1;i<maxn;i++){
if(vis[i]){
if(R[i]==0)
count++;
else if(R[i]>=2){
flag=false;
break;
}
}
}
if(count>1)
flag=false;
if(flag)
{
printf("Case %d is a tree.\n",Case++);
}
else
{
printf("Case %d is not a tree.\n",Case++);
}
init();
flag=true;
cnt=0;
count=0;
continue;
}
}
vis[x]=1,vis[y]=1;
R[y]++;
cnt++;
int fx=fin_d(x),fy=fin_d(y);
if(fx!=fy)
{
join(fx,fy);
}
else
{
flag=false;
}
}
return 0;
}
poj A
猜你喜欢
转载自blog.csdn.net/qq_40861069/article/details/81104104
今日推荐
周排行