[HDU - 1856]More is better

Posted vikyanite

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDU - 1856]More is better相关的知识,希望对你有一定的参考价值。

并查集求最大集合中元素数量

#include <cstdio>
#include <cstring>
#define maxn 10000000 + 5
using namespace std;
struct friends

    int root;
    int num;
p[maxn];
void init()

    for (int i = 1; i <= maxn; i++)
    
        p[i].root = i;
        p[i].num = 1;
    

int Find(int x)

    return p[x].root == x ? x : (p[x].root = Find(p[x].root));

void Join(int x, int y)

    int fx = Find(x), fy = Find(y);
    if (fx != fy)
    
        p[fx].root = fy;
        p[fy].num += p[fx].num;
        //printf("%d %d %d %d\n", fx, fy, p[fy].num, p[fx].num);
    

int main()

    int n, m;
    while (~scanf("%d", &n))
    
        if(n == 0)
        
             printf("1\n");
             continue;
        
        init();
        for (int i = 0; i < n; i++)
        
            int temp1, temp2;
            scanf("%d %d", &temp1, &temp2);
            Join(temp1, temp2);
        
        int maxnum = 0;
        for (int i = 1; i <= maxn; i++)
            //printf("%d\n", p[i].num);
            if (Find(i) == i)
            
                if (p[i].num > maxnum)
                    maxnum = p[i].num;

            
        printf("%d\n", maxnum);

    

    return 0;

 

以上是关于[HDU - 1856]More is better的主要内容,如果未能解决你的问题,请参考以下文章

More is better HDU - 1856 ?

hdu 1856 More is better(并查集)

HDU 1856 More is better

hdu1856 More is better (并查集)

HDU 1856More is better (裸并查集+记录秩)

[HDU - 1856]More is better