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

提供两个模板:

模板一:

 1 int ufs[MAXN];    //并查集
 2
 3 void Init(int n)    //初始化
 4 {
 5     int i;
 6     for(i=0;i<n;i++){
 7         ufs[i] = i;
 8     }
 9 }
10
11 int GetRoot(int a)    //获得a的根节点。路径压缩
12 {
13     if(ufs[a]!=a){    //没找到根节点
14         ufs[a] = GetRoot(ufs[a]);
15     }
16     return ufs[a];
17 }
18
19 void Merge(int a,int b)    //合并a和b的集合
20 {
21     ufs[GetRoot(b)] = GetRoot(a);
22 }
23
24 bool Query(int a,int b)    //查询a和b是否在同一集合
25 {
26     return GetRoot(a)==GetRoot(b);
27 }

模板二:

 1 int pre[30000];
 2 int find(int x){//查找x父节点
 3     int r=x; //委托r去找父节点
 4     while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
 5      r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
 6      return r;//父节点驾到~~~
 7 }
 8 void join(int x,int y){  //我想让x节点和节点连成一条线
 9     int fx=find(x),fy=find(y);//寻找x,y的父节点
10     if(fx!=fy)//x和y的父节点显然不是同一个
11     pre[fx]=fy;//让x的父节点成为y的子节点
12 } 

题意:
  有n个学生属于m个团体,(0<n<=30000,0<=m<=500)一个学生可以属于多个团体。一个学生疑似患病,则他属于整个团体都疑似患病,已知0号学生疑似患病,求一共多少个学生患病。
思路:
  很经典的并查集的题目,找一个pre[]数组记录存储每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
  代码:

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstdio"
 4 #include"cstring"
 5 using namespace std;
 6 int pre[30000];
 7 int find(int x){//查找x父节点
 8     int r=x; //委托r去找父节点
 9     while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
10      r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
11      return r;//父节点驾到~~~
12 }
13 void join(int x,int y){  //我想让x节点和节点连成一条线
14     int fx=find(x),fy=find(y);//寻找x,y的父节点
15     if(fx!=fy)//x和y的父节点显然不是同一个
16     pre[fx]=fy;//让x的父节点成为y的字节点
17 }
18 int main(){
19     std::ios::sync_with_stdio(false);
20     int n,m;
21     while(cin>>n>>m){
22     memset(pre,0,sizeof(pre));
23     if(n==0&&m==0)break;
24     for(int i=0;i<n;i++) pre[i] = i;
25     while(m--){
26         int t, a,b;
27         cin>>t>>a;
28         for(int i=1;i<t;i++){
29             cin>>b;
30             join(a,b);
31             a=b;
32         }
33     }
34         int sum=0;
35         for(int i=1;i<n;i++){
36             if(find(i)==find(0))
37             sum++;
38         }
39     cout<<sum+1<<endl;
40    }
41     return 0;
42 }

原文地址:https://www.cnblogs.com/huangdf/p/12227121.html

时间: 2024-08-10 21:29:03

poj 1611 :The Suspects经典的并查集题目的相关文章

【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 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 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 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

POJ 1611 The Suspects(特别无语的并查集)

很简单的一道题目,开始用的是并查集的分离集合森林做,不知道怎么的特别不稳定,太奇怪了,WA无数次,无奈之下改成一维数组了..sad AC #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define PI acos(-1,0) using namespa

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

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

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

POJ 2492 A Bug&#39;s Life (并查集)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

poj 3415 后缀数组分组+排序+并查集

Source Code Problem: 3415   User: wangyucheng Memory: 16492K   Time: 704MS Language: C++   Result: Accepted Source Code #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 510000