小白

ACM

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++);
    }
}

阅读更多
文章标签: c
个人分类: ACM-并查集
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭