并查集

2024-08-13

具有合并、查找、统计功能的数据结构

//#include <bits/stdc++.h>
#include <iostream>
#define io cin.tie(0), cout.tie(0), ios::sync_with_stdio(false)
#define LL long long
#define ULL unsigned long long
#define EPS 1e-8
#define INF 0x7fffffff
#define SUB -INF - 1
using namespace std;
const int N = 1050;
int s[N];
void init_set()
{
    for (int i = 1; i <= N; i++)
        s[i] = i;
}
int find_set(int x) { return x == s[x] ? x : find_set(s[x]); }
void merge_set(int x, int y)
{
    x = find_set(x);
    y = find_set(y);
    if (x != y)
        s[x] = s[y];
}
int main()
{
    int t, n, m, x, y;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        init_set();
        for (int i = 1; i <= m; i++)
        {
            cin >> x >> y;
            merge_set(x, y);
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
            if (s[i] == i)
                ans++;
        cout << ans << endl;
    }
    return 0;
}