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<stdio.h> #include<iostream> #include<string.h> using namespace std; #define N 100000 int father[N]; int flag[N]; int ok; void init() { for(int i=0;i<N;i++) { father[i]=i; } memset(flag,0,sizeof(flag)); ok=0; } int find_father(int x) { return father[x]==x?x:father[x]=find_father(father[x]); } void merge(int a,int b) { int x=find_father(a); int y=find_father(b); if(x==y) ok=1; else { if(x>y) father[x]=y; else father[y]=x; } } int main(void) { int a,b; int m; int t=0; while(scanf("%d %d",&a,&b)!=EOF) { if(a<0&&b<0) break; t++; if(a==0&&b==0) { printf("Case %d is a tree.\n",t); continue; } m=0; if(a>m) m=a; if(b>m) m=b; init(); flag[a]=1; flag[b]=1; merge(a,b); while(scanf("%d %d",&a,&b)) { if(a==0&&b==0) break; flag[a]=1; flag[b]=1; merge(a,b); if(a>m) m=a; if(b>m) m=b; } int cnt=0; for(int i=1;i<=m;i++) { if(flag[i]==1) { if(father[i]==i) cnt++; } } if(cnt>1) ok=1; if(ok==0) printf("Case %d is a tree.\n",t); else printf("Case %d is not a tree.\n",t); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator