村村通 - 洛谷https://www.luogu.com.cn/problem/P1536
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)int pre[MAXN];
int n, m, x, y;
int find(int x) {if (pre[x] == x) {return x;}return pre[x] = find(pre[x]);
}void unite(int x, int y) {x = find(x);y = find(y);pre[x] = pre[y];
}int main() {while (1) {int ans = 0;scanf("%d", &n);if (n == 0) {return 0;}scanf("%d", &m);for (int i = 1; i <= n; i++) {pre[i] = i;}for (int i = 1; i <= m; i++) {scanf("%d %d", &x, &y);unite(x, y);}for (int i = 1; i <= n; i++) {if (find(i) == i) {ans++;}}printf("%d\n", ans - 1);}return 0;
}