码迷,mamicode.com
首页 > 其他好文 > 详细

【并查集】POJ1611 The Suspects

时间:2018-12-10 00:45:17      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:节点   学生   stream   uniq   ini   HERE   def   ati   pst   

The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 51969   Accepted: 24829

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


并查集经
典题。大意就是非典时期,某学校有n个学生,每个学生从0~n-1被标上号。学校有许多学生小组,如果一组中有一个学生怀疑有感染非典,那个整个小组的学生都成为怀疑对象。
现在0号学生现在是怀疑对象,给出小组信息,求有多少个学生是怀疑对象。
可以看一下并查集经典模板:
 1 //并查集的实现
 2 
 3 #define MAX_N 100
 4 
 5 int par[MAX_N];   //父亲
 6 int Rank[MAX_N];  //树的高度
 7 
 8 //初始化n个元素
 9 void init(int n){
10     for(int i = 0; i < n; i++){
11         par[i] = i;
12         Rank[i] = 0;
13     }
14 }
15 
16 //查询树的根
17 int find(int x){
18     if(par[x] == x) return x;
19     else
20         return par[x] = find(par[x]);  //路径压缩:查询过程中,向上经过的所有节点,都改为直接连到根上
21 }
22 
23 //合并x和y所属的集合:从一个组的根向另一个组的根连边
24 void unite(int x,int y){
25     x = find(x);
26     y = find(y);
27     if(x == y) return;  //x和y已经是同一组的了,直接返回
28     //从高度(Rank)小的向高度大的连边
29     if(Rank[x] < Rank[y]) //x的高度小,x向y连边
30         par[x] = y;
31     else{    //否则,y向x连边
32         par[y] = x;
33         if(Rank[x] == Rank[y]) Rank[x]++;  //如果两个高度一样,合并后树高度+1
34     }
35 }
36 
37 //判断x和y是否属于同一个集合
38 bool same(int x,int y){
39     return find(x) ==  find(y);
40 }

 

把模板用到这道题上,一组的学生就可以看成一棵树,先找出和0一组的所有学生,如果这些学生还在其他组的树中,合并这两棵树,最后输出0所在树中所有节点个数,即可得到答案。
下面给出AC代码:
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int p[30005],a[30005];
 7 
 8 int find(int x){    //查询根节点
 9     if(p[x]==x) return x;
10     else
11         return p[x] = find(p[x]); //路径压缩:查询过程中,向上经过的所有节点,都改为直接连到根上
12 }
13 //合并x和y所属的集合:从一个组的根向另一个组的根连边
14 void unite(int a,int b){
15     int x = find(a),y = find(b);
16     if(x!=y)
17         p[y] = p[x];
18 }
19 
20 int main(){
21     int n,m,k,sum;  //n:学生数 m:组数 k:每组人数 sum:怀疑人总数
22     while(cin>>n>>m&&(n||m)){
23         sum = 0;
24         for(int i = 0;i<n;i++)
25             p[i] = i;
26         while(m--){
27             cin>>k; //每组的人数
28             cin>>a[0];
29             for(int i = 1;i<k;i++){
30                 cin>>a[i];
31                 unite(a[0],a[i]);  
32 } 33 } 34 for(int i = 0;i<n;i++){ 35 if(find(0)==find(i)) //找到同一个根节点说明在同一个树 36 sum++; 37 } 38 cout<<sum<<endl; 39 } 40 41 return 0; 42 }

 

 

【并查集】POJ1611 The Suspects

标签:节点   学生   stream   uniq   ini   HERE   def   ati   pst   

原文地址:https://www.cnblogs.com/Aikoin/p/10094185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!