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 |
come on/* 不一定要用并查集,把关系处理好就可以了 可以利用图的度的性质来进行即可 1.空值也为树 2.重边不是树 3.根节点只有一个 4.不能自身指向自身 5.保证联通即入度为0的点只能有 */ #include<stdio.h> #include<string.h> #define N 11000 #define inf 0x3fffffff int vis[N]; int degree[N]; int Min(int v,int vv) { return v>vv?vv:v; } int Max(int v,int vv) { return v>vv?v:vv; } int main() { int a,b,aa,bb,minn,maxx,cnt,i,k=0,flag,cur; while(scanf("%d%d",&a,&b)!=EOF) { if(a==0&&b==0) { printf("Case %d is a tree.\n",++k); continue; } else if(a<=0||b<=0) return 0; memset(vis,0,sizeof(vis)); memset(degree,0,sizeof(degree)); flag=0;minn=inf;maxx=-1; degree[b]++; vis[a]=vis[b]=1; if(a==b) flag=1; minn=Min(a,minn); minn=Min(b,minn); maxx=Max(a,maxx); maxx=Max(b,maxx); while(scanf("%d%d",&a,&b),a||b) { if(a==b) flag=1; degree[b]++; vis[a]=vis[b]=1; minn=Min(a,minn); minn=Min(b,minn); maxx=Max(a,maxx); maxx=Max(b,maxx); } if(flag) { printf("Case %d is not a tree.\n",++k); continue; } cnt=0; for(i=minn;i<=maxx;i++) { if(vis[i]&°ree[i]==0) cnt++; // printf("%d %d\n",i,degree[i]); } // printf("%d\n",cnt); cur=0; for(i=minn;i<=maxx;i++) if(vis[i]&°ree[i]>1) cur++; if(cnt==1&&cur==0) { printf("Case %d is a tree.\n",++k); continue; } printf("Case %d is not a tree.\n",++k); } return 0;} Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator