Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
此方法既不省时间,也不省内存/* 此方法既不省时间,也不省内存 */ #include<iostream> using namespace std; #define MAX 20000000 int Father[MAX]; bool flag[MAX]; void init() { for(int i=1;i<=MAX;i++) Father[i]=i; memset(flag,false,sizeof(flag)); } int Find(int n) { if(Father[n]!=n) return Find(Father[n]); return n; } int main() { int a,b; int k=1; while(cin>>a>>b) { init(); if(a==-1&&b==-1) break; Father[b]=a; flag[a]=true; flag[b]=true; bool ans=true; while(cin>>a>>b) { if(a==0&&b==0) break; if(flag[a]==true&&flag[b]==false) Father[b]=a; else if(flag[a]==false&&flag[b]==true) Father[a]=b; else if(flag[a]==true&&flag[b]==true) { int tempa=Find(a),tempb=Find(b); if(tempa==tempb) ans=false; else Father[tempb]=tempa; } flag[a]=true; flag[b]=true; } if(ans) cout<<"Case "<<k++<<" is a tree."<<endl; else cout<<"Case "<<k++<<" is not a tree."<<endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator