Sample Input
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
int root[1000],leaf[1000];
int main()
{
int a,b,t=1,flag=1,max=0,flag2=0;//flag2主要用来判断是不是一颗空树,空树也是树
while(cin>>a>>b)
{
if(a==-1) break;
if(a==0&&b==0)
{
if(flag2==1&&flag==1){
int sum=0;
for(int i=1;i<1000;i++)
{
if(root[i]>0&&leaf[i]==0) sum++;//只能有一个树根
}
if(sum!=1) flag=0;
}
memset(root,0,sizeof(root));
memset(leaf,0,sizeof(leaf));
if(flag) cout<<"Case "<<t<<" is a tree."<<endl;
else cout<<"Case "<<t<<" is not a tree."<<endl;
t++;
flag=1;
flag2=0;
max=0;
continue;
}
flag2=1;
root[a]++;//表示a的叶子个数
leaf[b]++;//b的根个数
if(leaf[b]>1) flag=0;//如果有一个叶子有多个根 则形成回路,不符合要求
}
}