hdu 1325

桉树的定义来就好了

#include<iostream>
#include<stdio.h>
using namespace std;
int p[100005];
int c[100005];
int u[100005];
int rank[100005];
int father[100005];
int find(int m)
{
    if(father[m]==m)
       return m;
    else
       return find(father[m]);
}
void uion(int m,int n)
{
     int x,y;
     x=find(m);
     y=find(n);
     if(rank[x]=rank[y])
     {
        rank[x]++;
        father[y]=x;
     }
     else if(rank[x]>rank[y])
         father[y]=x;
     else
        father[x]=y;
}
int main()
{
    int m,n,iii=1,i;
    while(scanf("%d%d",&m,&n),m>=0&&n>=0)
    {
        memset(p,0,sizeof(p));
        memset(c,0,sizeof(c));
        memset(u,-1,sizeof(u));
        memset(rank,0,sizeof(rank));
        for(i=0;i<100005;i++)
           father[i]=i;
        p[m]=1;
        p[n]=1;
        u[n]=m;
        c[n]++;
        uion(m,n);
        while(scanf("%d%d",&m,&n),m||n)
        {
            p[m]=1;
            p[n]=1;
            if(u[n]!=m)
            {
              c[n]++;
              u[n]=m;
            }
            uion(m,n);
        }
        int sum=0,sum1=0,sum2=0;
        int m=-1;
        for(i=0;i<100005;i++)
        {
            if(p[i]==1&&find(i)!=m)
            {
                 m=find(i);
                 sum2++;
            }
            if(p[i]==1&&c[i]!=1)
                sum++;
            if(p[i]==1&&c[i]==0)
                sum1++;
        }
        if(sum==1&&sum1==1&&sum2==1)
            printf("Case %d is a tree.\n",iii++);
        else
            printf("Case %d is not a tree.\n",iii++);
    }
}

 

posted @ 2011-10-14 22:38  DChipNau  阅读(125)  评论(0编辑  收藏  举报