Graph Theory NO.4 HDU_1856_More is better_并查集

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int father[100009];
int num[100009];
int Init(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        num[i]=1;
    }
    return 0;
}

int Find(int n)
{
    if(n!=father[n])
    {
        father[n]=Find(father[n]);
    }
    return father[n];
}

int Union(int n,int m)
{
    if(n!=m)
    {
        father[n]=m;
        num[m]+=num[n];
    }
}
int main()
{
    int n;
    int max_=-1;
    while(~scanf("%d",&n))
    {
        int a,b;
        memset(num,0,sizeof(num));
        memset(father,0,sizeof(father));
        Init(100009);

        while(n--)
        {
            scanf("%d%d",&a,&b);
            if(a>max_)max_=a;
            if(b>max_)max_=b;
            Union(Find(a),Find(b));
        }
        sort(num,num+max_+1);
        printf("%d\n",num[max_]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37862025/article/details/78196071