hdu 1856 More is better

#include <stdio.h>
#include <string.h>
#define MAXN 10000003
int ans,father[MAXN];
int find(int x)
{
    int i,t;
    for(i=x; father[i]>0; i=father[i]) ;
    while(x!=i)
    {
        t=father[x];
        father[x]=i;
        x=t;
    }
    return i;
}
void merge(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx==fy) return;
    if(father[fx]>father[fy])
    {
        father[fy]+=father[fx];
        father[fx]=fy;
        int t=-father[fy];
        if(t>ans) ans=t;
    }
    else
    {
        father[fx]+=father[fy];
        father[fy]=fx;
        int t=-father[fx];
        if(t>ans) ans=t;
    }
}
int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        /*n==0时要输入1,why.这里可注释,因为下面ans初始为1
          if(!n)
          {
           printf("1\n");
           continue;
          }*/
        memset(father,-1,sizeof(father));
        ans=1;
        while(n--)
        {
            scanf("%d %d",&i,&j);
            merge(i,j);
        }
        printf("%d\n",ans);
    }
    return 0;
}

posted @ 2010-08-27 19:01  菜到不得鸟  阅读(122)  评论(0)    收藏  举报