poj1611(并查集)

The Suspects

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 48053   Accepted: 23003

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 #include<cstdio>
 2 using namespace std;
 3 const int maxn=30006;
 4 const int inf=0x3f3f3f3f;
 5 int fa[maxn];
 6
 7 int find_fa(int a)
 8 {
 9     if(fa[a]==a)
10         return a;
11     else {
12         return fa[a]=find_fa(fa[a]);
13     }
14 }
15
16 int main()
17 {
18     int n,m;
19     while( ~scanf("%d%d",&n,&m)&&(n||m)){
20         for(int i=0;i<n;i++){
21             fa[i]=i;
22         }
23
24         for(int i=1;i<=m;i++){
25             int k,x,temp;
26             scanf("%d%d",&k,&x);
27             temp=find_fa(x);
28             for(int j=1;j<k;j++){
29                 scanf("%d",&x);
30                 int opx;
31                 opx=find_fa(x);
32                 if(temp<=opx){
33                     fa[opx]=temp;
34                 } else{
35                     fa[temp]=opx;
36                     temp=opx;
37                 }
38             }
39 //            printf("%d\n",temp);
40         }
41         int ans=0;
42         for(int i=0;i<n;i++){
43             if(find_fa(i)==fa[0])
44                 ans++;
45 //            printf("%d ",find_fa(i));
46         }
47 //        printf("\n");
48 //        for(int i=0;i<n;i++)
49 //            printf("%d ",fa[i]);
50 //        printf("\n");
51
52         printf("%d\n",ans);
53     }
54     return 0;
55 }

原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9094173.html

时间: 2024-10-10 22:06:04

poj1611(并查集)的相关文章

poj2236和poj1611并查集问题

POJ 2236 问在计算机坏了,修复若干,问检测两台是否能连通 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N = 1005; struct Point { int x,y; }; Point p[N]; int repaired[N]; int pre[N],rank[N]; int dist(Point A,Point B) {

poj1611 并查集 (路径不压缩)

http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社团,他所在的社团照样全部感染,求感染的人数. 解题思路: 并查集的变种,实质就是求0所在的强连通图的结点数目. 这道题纠结在数据的输入上,他只是告诉你哪些学生是同一个社团的.这就需要处理一下,我的想法是:如果这个社团有num个孩子,new出一个大小为num的数组,第一个孩子不处理,从第二个孩子起,和

详解并查集

详解并查集  Powered by WSY in SSF    2019-11-02  13:46 [1]并查集的定义:   并查集(Disjoint  Set)是一种非常精巧的非常实用的数据结构,它主要用来处理一些不相交集合的合并问题,经典的例子有联通子图,最小生成树的克鲁斯-卡尔算法. [2]并查集的经典问题:   我们通常使用“帮派”.“团伙”等问题举例说明并查集.例如帮派问题: 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌

并查集的基本运用 POJ1611

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

并查集初学(3)无间道之并查集 &amp;&amp; POJ2542 &amp;&amp; POJ1611

1.hihocoder上面讲的一道题 无间道之并查集 水题,精髓在于使用map容器进行打标签 #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; const int maxn=10005; int p[maxn]; map&l

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

poj1611 简单并查集

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

并查集——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(

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