#include <bits/stdc++.h>
using namespace std;
int m, n, pre[1000000];
set<int>s;
int find(int x) {
if (pre[x] == x) return x;
return find(pre[x]);
}
int main()
{
// 请在此输入您的代码
cin >> m >> n;
for (int i = 1; i <= m * n; i++)
pre[i] = i;
int k, a, b;
cin >> k;
for (int i = 1; i <= k; i++) {
cin >> a >> b;
int x=find(a),y=find(b);
if(x!=y) pre[x] = y;
}
for (int i = 1; i <= n * m; i++) {
s.insert(find(i));
}
cout << s.size();
return 0;
}
标签:pre,合根,return,int,查集,蓝桥,find
From: https://blog.csdn.net/m0_75073165/article/details/137269266