HDOJ 1856 More is better

~~~题目链接~~~


code:

#include <stdio.h>
#define N 10000002
int min = 0, max = 0, r[N], f[N];
void init()
{
    for(int i = 0; i<N; i++)
    {
        r[i] = 1;
        f[i] = i;
    }
}

int find(int x)
{
    if(f[x] != x)
        f[x] = find(f[x]);
    return f[x];
}

void Union(int x, int y)
{
    if(r[x]<r[y])
    {
        r[y] += r[x];
        f[x] = y;
    }
    else
    {
        f[y] = x;
        r[x] += r[y];
    }
}

int ans()
{
    int i = 0, sum = 0;
    for(int i = min; i<=max; i++)
        if(sum<r[i]) sum = r[i];
    return sum;
}
int main()
{
    int i = 0, j = 0, x = 0, y = 0, fx = 0, fy = 0, n = 0, flag = 0;
    while(scanf("%d", &n) != EOF)
    {
        init();
        while(n--)
        {
            scanf("%d %d", &x, &y);
            if(max<=x) max = x;
            if(max<=y) max = y;
            if(min>=x) min = x;
            if(min>=y) min = y;
            fx = find(x), fy = find(y);
            if(fx == fy) continue;
            Union(fx, fy);
        }
        printf("%d\n", ans());
    }
    return 0;
}


你可能感兴趣的:(HDOJ 1856 More is better)