99012 - 分数统计

#include<cstdio>
#include<cstring>
int n, m;
int f[10001], num[10001];
int ans[1000], len;

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

void Union(int a, int b)
{
    int fa = find(a), fb = find(b);
    if (fa == fb) return;
    f[fa] = fb;
    num[fb] += num[fa];
    num[fa] = 0;
}

int main()
{
    scanf("%d %d", &n, &m);
    memset(f, 0, sizeof (f));
    for (int i = 1; i <= n; i++) num[i] = 1, f[i] = i;
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        Union(a, b);
    }
    int p = 0;
    for (int i = 1; i <= n; i++)
        if (p < num[i])
            p = num[i];
    len = 1, ans[1] = 1;
    for (int i = 1; i <= p; i++)
    {
        for (int j = 1; j <= len; j++)
            ans[j] <<= 1;
        for (int j = 1; j <= len; j++)
            if (ans[j] >= 10)
            {
                ans[j + 1] += ans[j] / 10;
                ans[j] %= 10;
            }
        while (ans[len + 1] > 0)
        {
            len++;
            ans[len + 1] += ans[len] / 10;
            ans[len] %= 10;
        }
        if (len >= 100) len = 100, ans[101] = 1;
    }
    ans[1]--;
    for (int i = len; i >= 1; i--)
        printf("%d", ans[i]);
    printf("\n");
    return 0;
}