POJ 1611 The Suspects(并差集)
2015-08-29 16:36
309 查看
链接:http://poj.org/problem?id=1611
The Suspects
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.
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.
Output
For each case, output the number of suspects in one line.
Sample Input
Sample Output
Source
Asia Kaohsiung 2003
题意:有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入。但是SARS是很容易传染的,
只要在改组有一位同学感染SARS,那么该组的所有同学都被认为得了SARS。现在的任务是计算出有
多少位学生感染SARS了。假定编号为0的同学是得了SARS的
分析:用并差集。用nod[x].father记录其父节点,nod[x].number表示集合成员个数,nod[x].rank表示集合的层数。
The Suspects
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 27854 | Accepted: 13583 |
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.
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.
Output
For each case, output the number of suspects in one line.
Sample Input
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
4 1 1
Source
Asia Kaohsiung 2003
题意:有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入。但是SARS是很容易传染的,
只要在改组有一位同学感染SARS,那么该组的所有同学都被认为得了SARS。现在的任务是计算出有
多少位学生感染SARS了。假定编号为0的同学是得了SARS的
分析:用并差集。用nod[x].father记录其父节点,nod[x].number表示集合成员个数,nod[x].rank表示集合的层数。
<span style="font-size:18px;">#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=30030; int n,m; struct node{ int father; int number; int rank; }nod[maxn]; void init()//初始化 { for(int i=0;i<n+1;i++) { nod[i].father=i; nod[i].number=1; nod[i].rank=0; } } int find(int x){ //路径压缩的父节点查找 int r=x,temp; while(r != nod[r].father) r=nod[r].father; while(r != x) { temp=nod[x].father; nod[x].father=r; x=temp; } return x; } void unite(int x,int y) //按高度合并 { int fx=find(x); int fy=find(y); if(fx==fy) return; if(nod[fx].rank>nod[fy].rank) { nod[fy].father=fx; nod[fx].number +=nod[fy].number; } else { nod[fx].father=fy; nod[fy].number +=nod[fx].number; if(nod[fx].rank==nod[fy].rank) nod[fy].rank++; } } int main() { int t,a,b; while(scanf("%d %d",&n,&m) &&(n||m)) { init(); for(int i=0;i<m;i++) { scanf("%d",&t); if(t>1) { --t; scanf("%d",&a); while(t--) { scanf("%d",&b); unite(a,b); a=b; } } else scanf("%d",&a);//当t=1时记得要读入,刚开始我忘记读入了,EL了整个上午,,orz } int ans=find(0); printf("%d\n",nod[ans].number); } return 0; } </span>
相关文章推荐
- 2015/8/28 Python基础(2):对象
- 读到树人先生的文章,想到他那句中国的脊梁,不得不记下来。
- SpringMVC搭建------最简单的实现
- 对象解耦和设计模式
- Tomact 常见问题
- springMVC两种方式实现多文件上传及效率比较
- 膝盖中了一箭之康复篇4-8周
- Apache htpasswd
- php学习心得二
- 覆盖物-TextOverlay-(12)
- lyx 工具使用与备忘
- linux常用命令
- 翻译Wenguang's Introduction to Universal Disk Format (UDF)--完成
- ZOJ 3430 Detect the Virus ac自动机 超级精简的转化
- mysql远程连接错误1130的解决方法
- C#练习题
- MVC学习笔记---MVC生命周期及管道
- Android-帧动画Animation基本实现
- vsftp黑白名单设置及问题
- hdu 4739 2013杭州赛区网络赛 寻找平行坐标轴的四边形 **