【并查集】POJ1611 The Suspects

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 }
 

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

时间: 2024-10-11 20:41:54

【并查集】POJ1611 The Suspects的相关文章

并查集——poj1611(入门)

传送门:The Suspects 并查集水题 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 50005; int n,m; int a[maxn],b,ans; int pre[maxn]; void init() { for(int i=0;i<n;i++){ pre[i] = i; } } int findPre(

并查集 P - The Suspects

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-Sprea

并查集 poj1611&amp;poj2492

poj1611 简单题 代码中id记录父节点,sz记录子树规模.一个集合为一棵树. #include <iostream> #include <cstdio> using namespace std; int id[300005]; int sz[300005]; void add(int a, int b) { int i, j; for (i = a; i != id[i]; i = id[i]); for (j = b; j != id[b]; j = id[j]); if

并查集 - 1611 The Suspects

题目地址: http://poj.org/problem?id=1611 分析: - 数据结构 - parent[x] 表示 x 元素的父节点位置. - rank[x] 记录x的子链的长度, 以便在合并的时候减少链条长度. 查找的时候使用了路劲压缩, 所以两个节点的rank差不会大于1, 所以提高的效率也不是很大, 但还是很有帮助. - quantity[x] 表示x的子节点的个数(包含自身). 对于根节点来说, 就是这个集合的大小. - build(n) 由于规模 n 在变化, 所以需要多大的

poj1611——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 t

poj1611 The Suspects(并查集)

题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔离.起初学生0是疑似患者,求要隔离多少人. 思路 使用并查集求解. 代码 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int

The Suspects (并查集)

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-Sprea

并查集的基本运用 POJ1611

用并查集的情况是在比如 A和B联通  B和C联通那么 ABC联通 这类情况下算某个元素所在集合的元素个数.. 并查集数据结构用数组就行,数组的下标表示相应的一个主体,对应的值表示它的父节点的索引,指向父节点.根节点的值特殊一下就行: 原理:先把所有结点(元素)看成独立的集合,然后按给的条件来合并,合并的过程就是最重要的,结合已知条件,因为并查集是用于A和B联通  B和C联通那么 ABC联通 这类情况下算某个元素所在集合的元素个数   所以合并过程是把2个元素所在集合合并,实际情况就是比如已知AC

POJ 1611 The Suspects(并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22296   Accepted: 10842 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T