题目链接:http://poj.org/problem?id=1611
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.
题面翻译:
严重急性呼吸系统综合征(sars)是一种病因不明的非典型肺炎,2003年3月中旬被认为是一种全球性威胁。为了尽量减少传染给他人,最好的策略是把嫌疑犯与他人分开。
在不传播你的疾病大学(NSYSU),有许多学生团体。同一组的学生经常互相交流,一个学生可以加入几个组。为防止非典型肺炎的传播,南科大收集所有学生团体的成员名单,并在其标准操作程序(SOP)中制定以下规则。
一旦一个组中的成员是嫌疑犯,该组中的所有成员都是嫌疑犯。
然而,他们发现,当一个学生被认定为嫌疑犯时,要找出所有的嫌疑犯并不容易。你的工作是写一个程序来找到所有的嫌疑犯。
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
题目意思:
有n个同学,m个组,如果同一组中有人得SARS,则同组所有人都会被当做SARS患者,初始只有0是SARS患者,问有多少人是SARS患者
解题思路:
用并查集,因为一个人可以是多个组,而一个人若是要被当做SARS患者,则他一定会和0有关系,所以只要找出所有和0一个集合的人。两个不同的集合,指向根节点小的集合,最后判断有多少指向0的,就得出答案了。
代码:
#include
#include
#include
using namespace std;
const int M = 3e4 + 50;
int link[M];
void init(int n){ //初始化
for(int i = 0; i <= n; i++){
link[i] = i;
}
}
int fd(int n){ //找根节点
if(link[n] == n) return n;
else return link[n] = fd(link[n]);
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m) && (n || m)){
init(n); //初始化
for(int j = 0; j < m; j++){
int d;
scanf("%d",&d);
int tx;
int fmin;
int ft;
scanf("%d",&tx);
fmin = fd(tx); //每组先取一个当做根节点
for(int i = 1; i < d; i++){
scanf("%d",&tx);
ft = fd(tx);
if(ft < fmin){ //当遇到比根节点小的节点时,该节点变为根节点
link[fmin] = ft;
fmin = ft;
}
else
link[ft] = fmin;
}
}
int ans = 0;
//遍历一遍,找出所有根节点是0的个数
for(int i = 0; i <= n; i++){
if(fd(link[i]) == 0)ans++;
}
printf("%d\n",ans);
}
return 0;
}