-
Is It A Tree?
- POJ - 1308
-
#include<iostream> using namespace std; #define maxn 1000 int fa[maxn],u,v; int fond(int x) { return x==fa[x]?x:fa[x]=fond(fa[x]); } int main() { ios::sync_with_stdio(false); for(int j=1;; j++) { bool flag=0; int sum=0,x,y; while(cin>>u>>v) { if(u==-1&&v==-1) return 0; if(u==0&&v==0) break; if(fa[u]==0)fa[u]=u; if(fa[v]==0)fa[v]=v; x=fond(u); y=fond(v); if(x==y) flag=1; else fa[x]=y; } for(int i=1; i<=maxn; i++) { if(fa[i]==i) sum++; fa[i]=0; } if(sum>1||flag) cout<<"Case "<<j<<" is not a tree."<<endl; else cout<<"Case "<<j<<" is a tree."<<endl; } return 0; }