题目1444:More is better

题目1444:More is better_第1张图片
#include 
#define max 10000010
int tree[max];
int people[max];
int findroot(int x)
{
    if(tree[x]==-1)
        return x;
    else
    {
        int tap=findroot(tree[x]);
        tree[x]=tap;
        return tap;
    }
}
int main()
{
    int n,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        for (int i = 0; i < max; ++i)
        {
            tree[i]=-1;
            people[i]=1;
        }
        while(n--)
        {
            scanf("%d %d",&a,&b);
            a =findroot(a);
            b =findroot(b);
            if(a!=b)
            {
                tree[a]=b;
                people[b]+=people[a];
            }
        }
        int people_max=0;
        int index=0;
        for (int i = 0; i < max; ++i)
        {
            if(people[i]>people_max)
            {
                people_max=people[i];
                index=i;
            }
        }
        printf("%d\n",people_max);
    }
    return 0;
}
/**************************************************************
    Problem: 1444
    User: cust123
    Language: C++
    Result: Accepted
    Time:880 ms
    Memory:79144 kb
****************************************************************/

你可能感兴趣的:(九度OJ,----并查集)