The Suspects POJ - 1611 并查集,同集合的数

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个人,m个集合,每个人的标号为0到n,其中0带有传染病,如果再一个集合中有人有传染病,那么这个集合里的所有人都会有这种传染病,问每个测试中有传染病的有但是人,输入第一行为n,m。往下为m行,每行第一个为该集合的人数,其他为该集合中人的标号。输入以0 0结束。

思路:简单的并查集,套用模板即可,结果只需判断与0同集合的有但是人。

代码:
 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 998244353
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20
21 //fa[x]表示x的最远祖先
22 int fa[30005];
23 //初始化,一开始每个点单独成集合
24 void build(int qwq)
25 {
26     for(int i=0;i<=qwq;i++)
27     {
28         fa[i]=i;
29     }
30     return ;
31 }
32 //找到x的最远祖先,并且压缩路径
33 int find(int x)
34 {
35     if(fa[x]==x)
36     {
37         return x;
38     }
39     return fa[x]=find(fa[x]);
40 }
41 //判断x,y是不是在同一个集合里,直接判断最远祖先是不是一样的
42 bool che(int x,int y)
43 {
44     return find(x)==find(y);
45 }
46 //合并x,y,我们在判断x和y是不是同一个集合里,
47 //路径压缩之后fa[x],fa[y]已经是最远祖先了,
48 //所以直接将fa[x]的父亲连接在fa[y]的祖先上
49 void mer(int x,int y)
50 {
51     if(!che(x,y))
52     {
53         fa[fa[x]]=fa[y];
54     }
55     return ;
56 }
57
58 int main()
59 {
60     int n,m;
61     while(scanf("%d %d",&n,&m)&&(n||m))
62     {
63         //初始化
64         build(n);
65         int k,a,b;
66         for(int i=0;i<m;i++)
67         {
68             scanf("%d",&k);
69             //每组第一个数
70             scanf("%d",&a);
71             //将每组其他数与本组的第一个数合并
72             for(int j=1;j<k;j++)
73             {
74                 scanf("%d",&b);
75                 mer(a,b);
76             }
77         }
78         //0本身也是一个人
79         int ans=1;
80         //遍历寻找与0同集合的数
81         for(int i=1;i<=n;i++)
82         {
83             if(find(i)==find(0))
84             {
85                 ans++;
86             }
87         }
88         printf("%d\n",ans);
89     }
90 }

原文地址:https://www.cnblogs.com/mzchuan/p/11594195.html

时间: 2024-11-08 22:37:33

The Suspects POJ - 1611 并查集,同集合的数的相关文章

poj 1611~并查集基础

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

POJ 1611 并查集

23333......简单的并查集模板.就是被并查集递归式路径压缩RE坑过...学习一下非递归式路径压缩. 附代码: #include<stdio.h>#include<iostream>#include<string.h>#define maxn 30100using namespace std; int fa[maxn];int n, m, k; void init(){    for (int i=0; i<=n; ++i)        fa[i] = i

kuangbin专题五:B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 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 f

B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 61692   Accepted: 29146 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mi

POJ 2524 并查集

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 11609 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding o

并查集—分离集合森林实现

并查集总结 今天总结一下并查集,这个完了之后,寒假学的数据结构基础的模板类的题目差不多就完了,对于模板题,敲上10遍.20遍.30遍,那么模板就不是模板,就成为了你自己的东西,就好像 A+B 一辈子也忘不了,以后每天敲一遍模板题,加深对模板的理解. 并查集,一般使用的是 数组实现.树实现,其中数组实现时间复杂度较高,树实现也就是分离集合森林 查找.合并的时间复杂度不会 超过 O(log2n) n个人,m对有亲戚关系的 10 7 1 2 2 3 2 4 3 4 5 6 6 7 8 9 初始化:{1

poj 1611 :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-Spr

【POJ】The Suspects(裸并查集)

并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace std; const int maxn = 33333; int fa[maxn]; int num[maxn]; int n,m,t; void init(){ for(int i = 0; i < n; i++) {fa[i] = i; num[i] = 1;} } int find_father(i

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?