我是靠谱客的博主 碧蓝长颈鹿,这篇文章主要介绍并查集--POJ - 1611 The Suspects,现在分享给大家,希望可以做个参考。

Description

严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁。为了减少传播给别人的机会, 最好的策略是隔离可能的患者。
在Not-Spreading-Your-Sickness大学( NSYSU), 有许多学生团体。同一组的学生经常彼此相通,一个学生可以同时加入几个小组。为了防止非典的传播,NSYSU收集了所有学生团体的成员名单。他们的标准操作程序(SOP)如下:
一旦一组中有一个可能的患者, 组内的所有成员就都是可能的患者。
然而,他们发现当一个学生被确认为可能的患者后不容易识别所有可能的患者。你的工作是编写一个程序, 发现所有可能的患者。
 

Input

输入文件包含多组数据。
对于每组测试数据:
第一行为两个整数n和m, 其中n是学生的数量, m是团体的数量。0 < n <= 30000,0 <= m <= 500。
每个学生编号是一个0到n-1之间的整数,一开始只有0号学生被视为可能的患者。
紧随其后的是团体的成员列表,每组一行。
每一行有一个整数k,代表成员数量。之后,有k个整数代表这个群体的学生。一行中的所有整数由至少一个空格隔开。
n = m = 0表示输入结束,不需要处理。

Output

对于每组测试数据, 输出一行可能的患者。

Sample Input

复制代码
1
2
3
4
5
6
7
8
9
10
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0

Sample Output

复制代码
1
2
3
4 1 1

分析:属于并查集的套路,主要就是两个操作,一是查找任一结点的根结点,一是合并两个集合


复制代码
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
#include <cstdio> using namespace std; const int maxn = 30000 + 10; int parent[maxn]; int depth[maxn]; int num[maxn]; void make_set(int i) { parent[i] = i; depth[i] = 0; num[i] = 1; } int find_set(int i) { int r = i; while(r != parent[r]) r = parent[r]; while(i != r) { int tmp = parent[i]; parent[i] = r; i = tmp; } return i; } void union_set(int x, int y) { x = find_set(x); y = find_set(y); if(x == y) return; if(depth[x] > depth[y]) { parent[y] = x; num[x] += num[y]; } else { parent[x] = y; num[y] += num[x]; if(depth[x] == depth[y]) depth[y]++; } } int main() { int n, m; while(~scanf("%d%d", &n, &m) &&n ){ for(int i=0; i<n; i++) { make_set(i); } for(int i=0; i<m; i++) { int t, x; scanf("%d", &t); scanf("%d", &x); for(int j=1; j<t; j++) { int y; scanf("%d", &y); union_set(x, y); x = y; } } int x = find_set(0); printf("%dn", num[x]); } return 0; }

可以直接把结点个数放在根结点,初始化为-1,结点数为负数

复制代码
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
#include <cstdio> #include <cstring> using namespace std; const int maxn = 30000 + 10; int pan[maxn]; //parent:negative means the number of nodes int find_set(int i) { int r = i; while(pan[r] > -1) r = pan[r]; while(i != r) { int tmp = pan[i]; pan[i] = r; i = tmp; } return i; } void union_set(int x, int y) { x = find_set(x); y = find_set(y); if(x == y) return; int tmp = pan[x] + pan[y]; if(pan[x] > pan[y]) { pan[x] = y; pan[y] = tmp; } else { pan[y] = x; pan[x] = tmp; } } int main() { int n, m; while(~scanf("%d%d", &n, &m) &&n ){ for(int i=0; i<n; i++) pan[i] = -1; for(int i=0; i<m; i++) { int t, x; scanf("%d", &t); scanf("%d", &x); for(int j=1; j<t; j++) { int y; scanf("%d", &y); union_set(x, y); } } int x = find_set(0); printf("%dn", -pan[x]); } return 0; }


最后

以上就是碧蓝长颈鹿最近收集整理的关于并查集--POJ - 1611 The Suspects的全部内容,更多相关并查集--POJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部