HDU 1856 More is better

题解:用并查集将所有的朋友合并,最后记录最大的连通块即可:

#include <cstdio>
int f[10000010],max,sum[10000010],cnt;

int sf(int x){
    if(f[x]!=x)f[x]=sf(f[x]);
    return f[x];
}

int main(){
    int n,x,y;
    while(scanf("%d",&n)!=EOF){
        if(n==0){puts("1");continue;}
        for(int i=1;i<10000010;i++){f[i]=i;sum[i]=0;}
        cnt=0;
        for(int i=0;i<n;i++){
            scanf("%d%d",&x,&y);
            f[sf(x)]=sf(y);
            x=x>y?x:y;
            cnt=x>cnt?x:cnt;
        }
        max=0;
        for(int i=1;i<=cnt;i++)
        if ((++sum[sf(i)])>max) max=sum[f[i]];
        printf("%d\n",max);
    }
    return 0;
}

posted @ 2014-02-15 12:45  forever97  阅读(120)  评论(0编辑  收藏  举报