HDU 1213 - How Many Tables

传送门

HDU 1213 - How Many Tables

题意

给一些认识的人的名单,求要准备几张桌子

思路

并查集

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP(a, b) make_pair(a, b)
using namespace std;
const int MAXN = 1000 + 5;
const int INF = 0x3f3f3f3f;
 
int vis[MAXN], pa[MAXN];
 
int Find(int x)
{
    return pa[x] == x ? x : pa[x] = Find(pa[x]);
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    int T, i, j, n, npep, a, b;
    scanf("%d", &T);
    while (T--)
    {
        int cnt = 0;
        memset(vis, 0, sizeof vis);
        scanf("%d%d", &npep, &n);
        for (i = 1; i <= npep; i++)
            pa[i] = i;
        for (i = 0; i < n; i++)
        {
            scanf("%d%d", &a, &b);
            int x = Find(a), y = Find(b);
            if (x != y)
                pa[x] = y;
        }
        for (i = 1; i <= npep; i++)
        {
            int v = Find(i);
            if (!vis[v])
                cnt++, vis[v] = 1;
        }
        printf("%d\n", cnt);
    }
    return 0;
}

Powered by Jekyll and Theme by solid