并查集 PKU 1611
2014-10-09 22:41
302 查看
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 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
Sample Output
[/code]
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 23014 | Accepted: 11178 |
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
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> #include <iostream> #include <queue> #include <stack> #include <deque> #include <vector> #include <set> #include <map> using namespace std; #define MAXN 30010 int father[MAXN]; int N,M; int sum[MAXN];//每个节点下有几个,每次要加入新的节点的时候就放入到节点数更多的下面 int flag;// int find(int x){ if(x != father[x]){ father[x] = find(father[x]); } return father[x]; } void u(int x,int y){ int f1 = find(x); int f2 = find(y); if(f1 == f2){ return ; } if(sum[f1] < sum[f2]){ father[f1] = f2; sum[f2]+=sum[f1]; if(f1 == flag){ flag = f2; } } else{ father[f2] = f1; sum[f1]+=sum[f2]; if(flag == f2){ flag = f1;//判断0在哪个集合里面 } } } int main(){ int N,M,num; int i,j; int num1,num2; while(~scanf("%d%d",&N,&M)){ if(N==0 && M==0){ break; } for(i=0;i<N;i++){ father[i] = i; sum[i] = 1; } flag = 0; while(M--){ scanf("%d",&num); for(i=0;i<num;i++){ if(i == 0){ scanf("%d",&num1); } else{ scanf("%d",&num2); u(num1,num2); } } } printf("%d\n",sum[flag]); } return 0; }
[/code]
相关文章推荐
- pku 1611 The Suspect(最简单的并查集)
- pku1611 并查集
- 并查集裸题——pku1611
- pku 1611(并查集)
- PKU1611-2485-1861使用并查集实现Kruskar算法求最小生成树。
- PKU POJ 1611解题报告(并查集)
- PKU 1611 并查集
- pku 1611 The Suspects 并查集的应用
- Pku acm 1611 The Suspects数据结构题目解题报告(九)---- 并查集的应用
- POJ 1611 / ZOJ 1789: The Suspects - 并查集
- poj 1611(并查集)
- poj 1611 The Suspects 【并查集】
- Pku acm 2492 A Bug's Life数据结构题目解题报告(十)---- 并查集的应用
- POJ ~ 1611 ~ The Suspects (并查集)
- POJ 1611--The Suspects【并查集水题】
- POJ 1611 并查集详解+基础应用
- 并查集 - 1611 The Suspects
- POJ 1611-The Suspects【并查集】
- poj 1611 The Suspects 并查集变形题目
- POJ 1611 The Suspects(并查集)