链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

题意:求处理并查集后,树的个数

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int MAX_N = 2000+5;

int par[MAX_N];

void init() {
    for (int i = 1; i < MAX_N; ++i) {
        par[i] = i;
    }
    return;
}

int getPar(int x) {
    if (par[x] == x)  return x;
    else
    {
        return par[x] = getPar(par[x]);
    }
}

void UNION(int u, int v) {
    int x = getPar(u);
    int y = getPar(v);
    par[y] = x;
}

int main()
{
    int t;
    cin >> t;
    while (t--) {
        init();
        int n, m;
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            int u, v;
            cin >> u >> v;
            UNION(u, v);
        }
        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (par[i] == i) {
                ++res;
            }
        }
        cout << res << endl;

    }

    return 0;
}