poj 1611 The Suspects

The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 31113   Accepted: 15117

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

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
思路引导:
(1)需掌握的是运用并查集划分集合的同时记录每个集合的元素个数
(2)需注意的是,本题中编号为0的人是已被感染的,所以被感染的人数至少有一个。
(3)如果合并集合时将元素少的集合合并到元素多的集合中,就可以优化程序。
解题报告:
对于一些有联系的节点通过并查集的并操作将把这些节点连接形成一棵树或一片森林。
在本题中,把每个人都视为节点,并且将记录每棵树的节点个数,并且把节点个数记录在根节点元素上。
Step1:为了方便解题首先建立一个结构体结构体中包含两个元素,分别记录节点的父节点以及以这个节点为根节点的树的节点数目初始化时每个节点视为一棵独立的树,这颗树种的元素个数为1;
Step2:输入每对相关联的点时就进行并操作,并操作时将两棵树的节点数之和记录在合并后的树的树根上;
Step3:输入结束后,查询编号0节点的根节点,这个根节点上记录的节点数目就是所要求的结果。
 1 #include <stdio.h>
 2 #include <string.h>
 3 typedef struct{//结构体中pre记录该点的父节点,num记录以该节点为根节点的集合元素个数
 4     int pre;
 5     int num;
 6 }Tree;
 7 Tree tree[30005];
 8 
 9 void unit(int n){
10     for(int i = 0; i < n; i++){
11         tree[i].pre = i;
12         tree[i].num = 1;
13     }
14 }
15 
16 int GetParent(int x){
17     if(x != tree[x].pre)
18         tree[x].pre = GetParent(tree[x].pre);
19     return tree[x].pre;
20 }
21 
22 void merge(int x, int y){
23     int f1 = GetParent(x);
24     int f2 = GetParent(y);
25     if(f1 != f2){
26         tree[f2].pre = f1;
27         tree[f1].num += tree[f2].num;
28     }
29 
30 }
31 
32 int main(){
33     int n, m, k;
34     while(scanf("%d %d", &n, &m)){
35         if(n == 0 && m == 0)
36             break;
37 
38         unit(n);
39 
40         while(m--){
41             scanf("%d", &k);
42             int a, b;
43             scanf("%d", &a);
44             for(int i = 1; i < k; i++){
45                 scanf("%d", &b);
46                 merge(a,b);
47             }
48 
49         }
50 
51         int t = GetParent(0);
52         printf("%d
", tree[t].num);
53     }
54     return 0;
55 }



越努力,越幸运
原文地址:https://www.cnblogs.com/qinduanyinghua/p/5492250.html