hdoj 1325 Is It A Tree?

来源:互联网 发布:maxwell软件介绍 编辑:程序博客网 时间:2024/05/17 01:27

此题满足条件:1,不成环(包括自环);2,任何点入度不能超过1;
注意输入两数为负数结束

代码如下:

#include<iostream> #include<cstdio>#include<cstring>#include<algorithm>using namespace std;int pre[1100],vis[1100],aa[1100];int findd(int x){    int r=x;    while(r!=pre[r])        r=pre[r];    int i=x,j;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void bing(int x,int y){    int tx,ty;    tx=findd(x);    ty=findd(y);    if(tx!=ty)        pre[ty]=tx;}int main (){    int tt=0;    while(1)    {        tt++;        int flag=0;        int cnt=1,sum=0;        int xx,yy,i,j,keng=0;        for(i=0;i<1100;i++)        {            pre[i]=0;            vis[i]=0;        }        while(~scanf("%d%d",&xx,&yy))        {            if(xx==0&&yy==0)                break;            if(xx<0&&yy<0)            {                return 0;            }            if(pre[xx]==0)            pre[xx]=xx;            if(pre[yy]==0)            pre[yy]=yy;            if(findd(xx)==findd(yy))                keng=1;            bing(xx,yy);            vis[yy]++;        }        for(i=1;i<1100;i++)        {            if(vis[i]>1)                break;            if(pre[i]==i)                sum++;        }        if(keng==1)        {            printf("Case %d is not a tree.\n",tt);            continue;        }        if(vis[i]>1)        {            printf("Case %d is not a tree.\n",tt);            continue;        }        if(sum==1)        {            printf("Case %d is a tree.\n",tt);        }        else        {            printf("Case %d is not a tree.\n",tt);        }    }}
原创粉丝点击