poj 1308 Is It A Tree?

来源:互联网 发布:淘宝上如何购买发票 编辑:程序博客网 时间:2024/05/21 15:50
    #include <iostream>using namespace std;struct node{int x,y;}t;int tt,case_t;int parent[105];bool flag[105];void makeset(){int xx;for(xx=1;xx<100;xx++){parent[xx]=xx;flag[xx]=0;}}int findset(int xx){if(xx!=parent[xx])parent[xx]=findset(parent[xx]);return parent[xx];}void unionset(int xx,int yy){xx=findset(xx);yy=findset(yy);if(xx==yy)return ;parent[yy]=xx;}int main(){int fir;while(scanf("%d%d",&t.x,&t.y)!=EOF){if(t.x==-1 && t.y==-1)break;if(t.x==0 && t.y==0){printf("Case %d is a tree./n", ++case_t);continue;}makeset();flag[t.x]=flag[t.y]=1;fir=t.x;bool tree=1;if(t.x==t.y)tree=0;elseunionset(t.x,t.y);while(scanf("%d%d",&t.x,&t.y) &&t.x!=0){flag[t.x]=flag[t.y]=1;if(findset(t.x)==findset(t.y))tree=0;unionset(t.x,t.y);}for(int i=1;i<100;i++)if(flag[i] && findset(i)!=findset(fir))tree=0;if(tree)printf("Case %d is a tree./n", ++case_t);elseprintf("Case %d is not a tree./n", ++case_t);}return 0;}


原创粉丝点击