範例程式碼 uva615
//uva615
#include<iostream>
#include<vector>
using namespace std ;
int main(){
int father[100001];
int x,y;
int tmp;
int root = -1;
int Case = 1 ;
bool is_tree = true ;
for(int i=0 ; i<100001 ;i++) father[i] = -1 ;
while(cin>>x>>y){
//over
if(x<=-1 && y<=-1) return 0 ;
//Case over
if(x==0 && y==0) {
for(int i=0 ; i<100001 ;i++){
if(root == -1 && father[i] != -1) root = father[i] ;
else if (father[i] !=-1 && father[i] != root) is_tree= false ;
}
if(is_tree == false) cout<<"Case "<<Case<<" is not a tree."<<endl;
//for uva judgement
//else cout<<"Case "<<Case<<" is a tree."<<endl;
//
//for cpe
else cout<<"Case "<<Case<<" is a tree. Root is "<<root<<"."<<endl;
//
Case++;
//initialize
root = -1;
is_tree = true ;
for(int i=0 ; i<100001 ;i++) father[i] = -1 ;
}
else{
//more than one parents
if(father[y]!= -1) is_tree= false ;
//cycle
if(x==y) is_tree= false;
if(father[x] != -1) tmp = father[x] ;
else tmp = x ;
//cycle
if(father[x] == y) is_tree= false ;
else if(father[y] == -1) father[y] = tmp ;
for(int i=0 ; i<100001 ;i++) if(father[i]== y) father[i] = tmp ;
}
}
return 0 ;
}