-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy path11.1.cpp
71 lines (63 loc) · 1.88 KB
/
11.1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* 题目名称:畅通工程
* 题目来源:浙江大学复试上机题
* 题目链接:http://t.cn/AiOvBHj9
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 1000 + 10;
int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
void Initial(int n) { //初始化
for (int i = 0; i <= n; i++) {
father[i] = i; //每个结点的父亲为自己
height[i] = 0; //每个结点的高度为零
}
}
int Find(int x) { //查找根结点
if (x != father[x]) { //路径压缩
father[x] = Find(father[x]);
}
return father[x];
}
void Union(int x, int y) { //合并集合
x = Find(x);
y = Find(y);
if (x != y) { //矮树作为高树的子树
if (height[x] < height[y]) {
father[x] = y;
} else if (height[y] < height[x]) {
father[y] = x;
} else {
father[y] = x;
height[x]++;
}
}
return ;
}
int main() {
int n, m;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
scanf("%d", &m);
Initial(n); //初始化
while (m--) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
Union(x, y); //合并集合
}
int answer = -1;
for (int i = 1; i <= n; i++) {
if (Find(i) == i) { //集合数目
answer++;
}
}
printf("%d\n", answer);
}
return 0;
}