-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path100000615-01.cpp
76 lines (64 loc) · 1.37 KB
/
100000615-01.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
72
73
74
75
76
#include <cstdio>
#define MAXN 1005
int set[MAXN], // 并查集
n, m;
bool flag[MAXN] = {}; // 如果出现某个元素簇的 root,则令 flag[root] = true;
int find(int x)
{
if (x == set[x])
return x;
// find root
int root = x;
while (root != set[root])
root = set[root];
// optimize
int index = x;
while (index != set[index])
{
int temp = index;
set[temp] = root;
index = set[index];
}
return root;
}
void union_sets(int a, int b)
{
int root_a = find(a), root_b = find(b);
if (root_a != root_b)
set[root_a] = set[root_b];
}
void init(int n)
{
for (int i = 0; i <= n; i++)
set[i] = i;
for (int i = 0; i <= n; i++)
flag[i] = false;
}
int main()
{
while (scanf("%d", &n))
{
if (n == 0) break;
scanf("%d", &m);
// 初始化
init(n);
// 输入及建树
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b);
union_sets(a, b);
}
// 标记簇的 root
for (int i = 1; i <= n; i++)
flag[find(i)] = true;
// 计算簇的个数
int cnt = 0;
for (int i = 1; i <= n; i++)
if (flag[i])
cnt++;
// 输出
printf("%d\n", cnt - 1);
}
return 0;
}