标签:src bottom cas mono return lan field blank ati
http://acm.hdu.edu.cn/showproblem.php?pid=1325
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26387 Accepted Submission(s): 6039
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 using namespace std; 8 #define pii pair<int,int> 9 #define inf 0x3f3f3f3f 10 int f[50005],tot[50005]; 11 map<int,int>M; 12 int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);} 13 int main() 14 { 15 int a,b,i,j,k; 16 int N,cas=0; 17 while(cin>>a>>b){bool ok=1;N=0; 18 if(a<=-1&&b<=-1)break; 19 M.clear(); 20 if(a==0&&b==0){printf("Case %d is a tree.\n",++cas);continue;} 21 M[a]=++N; 22 if(a!=b)M[b]=++N; 23 else M[b]=1; 24 memset(tot,0,sizeof(tot)); 25 for(i=1;i<=50005;++i)f[i]=i; 26 tot[M[b]]++; 27 int fa=getf(M[a]),fb=getf(M[b]); 28 if(fa!=fb) f[fa]=fb; 29 while(cin>>a>>b){if(a==0&&b==0) break; 30 if(!M[a])M[a]=++N; 31 if(!M[b])M[b]=++N; 32 a=M[a]; 33 b=M[b]; 34 35 fa=getf(a),fb=getf(b); 36 if(fa!=fb) f[fa]=fb; 37 N=max(N,max(a,b)); 38 tot[b]++; 39 } 40 for(i=1;i<=N;++i) 41 { 42 if(tot[i]>1) {ok=0; break;} 43 } 44 int s=0; 45 for(i=1;i<=N;++i) 46 { 47 if(i==getf(i)) s++; 48 if(s>1){ok=0;break;} 49 } 50 ok?printf("Case %d is a tree.\n",++cas):printf("Case %d is not a tree.\n",++cas); 51 } 52 return 0; 53 }
标签:src bottom cas mono return lan field blank ati
原文地址:http://www.cnblogs.com/zzqc/p/7608826.html