poj--1611--The Suspects
来源:互联网 发布:刚刚学php适合看什么书 编辑:程序博客网 时间:2024/06/15 01:11
The Suspects
Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 40830 Accepted: 19708
Description
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
Sample Output
411
题意:
学生分为不同的小组,每个小组只要有一个感染sars,则认为这个小组的所有人都感染,注意一个学生可以加入很多个小组,编号为零 的一开始认为感染了sars.问 一共有多少人感染了sars.
解题思路:
用并查集,根据同学加入的小组,把符合条件的组,同学并到一起,最后遍历全部同学,把和‘’编号为零‘’同一个根的同学统计出来,就是感染sars的人数。
代码:
C++ Code
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
#include<iostream>
#include<cstdio>
using namespace std;
int a[30005];
int n, m, k;
void init()
{
///初始化
for(int i = 0; i < n; i++)
a[i] = i;
}
int fin_d(int x)
{
///查(递归压缩路径,)
if(a[x] == x)
return a[x];
else
return fin_d(a[x]);
}
void unio_n(int r1, int r2)
{
///并
int t1 = fin_d(r1);
int t2 = fin_d(r2);
if(t1 != t2)
{
a[t2] = t1; ///合并子节点
}
}
int main()
{
while(scanf("%d %d", &n, &m))
{
int one, two;
init();
if(n == 0 && m == 0)
break;
for(int j = 0; j < m; j++)
{
scanf("%d", &k);
if(k >= 1)
{
scanf("%d", &one);
for(int i = 1; i < k; i++)
{
scanf("%d", &two);
unio_n(one, two);
}
}
}
int sum = 0;
for(int i = 0; i < n; i++)
{
///统计有多少同学与零号同学公用一个根
if(fin_d(i) == fin_d(0))
sum++;
}
printf("%d\n", sum);
}
return 0;
}
#include<cstdio>
using namespace std;
int a[30005];
int n, m, k;
void init()
{
///初始化
for(int i = 0; i < n; i++)
a[i] = i;
}
int fin_d(int x)
{
///查(递归压缩路径,)
if(a[x] == x)
return a[x];
else
return fin_d(a[x]);
}
void unio_n(int r1, int r2)
{
///并
int t1 = fin_d(r1);
int t2 = fin_d(r2);
if(t1 != t2)
{
a[t2] = t1; ///合并子节点
}
}
int main()
{
while(scanf("%d %d", &n, &m))
{
int one, two;
init();
if(n == 0 && m == 0)
break;
for(int j = 0; j < m; j++)
{
scanf("%d", &k);
if(k >= 1)
{
scanf("%d", &one);
for(int i = 1; i < k; i++)
{
scanf("%d", &two);
unio_n(one, two);
}
}
}
int sum = 0;
for(int i = 0; i < n; i++)
{
///统计有多少同学与零号同学公用一个根
if(fin_d(i) == fin_d(0))
sum++;
}
printf("%d\n", sum);
}
return 0;
}
阅读全文
0 0
- POJ 1611 The Suspects
- poj 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ-1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 - The Suspects
- POJ 1611 The Suspects
- Poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 the suspects
- POJ 1611 The Suspects
- 单例模式优化
- ios xcode常用调试技巧总结
- TreeSet和HashSet区别
- python实现HMM
- 前端工程师的小伙伴--chrome
- poj--1611--The Suspects
- 您的博文被删除了(1)
- Win10安装silverlight5开发环境,以及坑
- QML实现钟表效果
- Linux查看物理CPU个数、核数、逻辑CPU个数
- python 一个HTML文件,找出正文和链接
- 大数据生态系统基础:Apache Kafka基础(四):最新kafka编程入门:Stream API
- 字符串大小写转换
- Java基础类库