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

来源:互联网 发布:广州学网络推广 编辑:程序博客网 时间:2024/06/05 06:58
//本题用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;}

0 0
原创粉丝点击