HDU - 1856 - More is better <并查集>

//本题用cin超时啊,要用scanf
#define  _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 10000001;
int pre[maxn];
int topcnt[maxn];

int Find(int x)
{
    int l = x;
    while (l != pre[l])
        l = pre[l];
    while (l != pre[x]){
        int t = pre[x];
        pre[x] = l;
        x = t;
    }
    return l;
}

void join(const int x, const int y)
{
    int fx = Find(x);
    int fy = Find(y);
    pre[fx] = fy;
}

int main()
{
    int n; 
    while (~scanf("%d", &n)){
        if (n == 0){
            printf("1\n");
            continue;
        }
        for (int i = 0; i < maxn; i++)
            pre[i] = i;
        memset(topcnt, 0, sizeof(topcnt));
        while (n--){
            int x, y; scanf("%d %d", &x, &y);
            join(x, y);
        }
        for (int i = 1; i < maxn; i++)
            topcnt[Find(i)]++;
        printf("%d\n", *max_element(topcnt + 1, topcnt + maxn));
    }
    return 0;
}

posted @ 2015-01-19 12:32  Popco  阅读(124)  评论(0编辑  收藏  举报