Ubiquitous Religions ---并查集入门
2016-08-23 19:52
429 查看
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 21382 | Accepted: 10356 |
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
<span style="font-size: 18px;"><strong>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</strong></span>
Sample Output
<span style="font-size: 18px;"><strong>4 1 1</strong></span>
Source
Asia Kaohsiung 2003
题意:
Description
世界上有许多不同的宗教,现在有一个你感兴趣的问题:找出多少不同的宗教,在你的大学中的大学生信仰了多少种不同的宗教。你知道在你的大学有n个学生(0<n<= 50000)。若直接问每一个学生的宗教信仰不大适合。此外,许多学生还不太愿意说出自己的信仰。有一种方法来避免这个问题,询问m(0<=m<=n(n- 1)/ 2)对学生,询问他们是否信仰同一个宗教(比如,可以询问他们是否都参加同一教堂)。从这个数据,您可能不知道每个人宗教信仰,但是你可以知道有多少不同宗教信仰。你可以假设,每名学生最多信仰一个宗教。
Input
输入包含多组测试数据。每组测试数据的开头包含两个整数n和m。接下来有m行,每行有两个整数i和j,编号为i和j的同学信仰同一个宗教。学生的编号从从1开始到n。当输入使n=0,m=0标志输入的结束。
Output
每组测试数据的输出只有一行,包含数据的组别(从1开始)和学生最多信仰的宗教数。
Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Sample Output
Case 1: 1
Case 2: 7
#include <iostream> #include<cstdio> #define mx 52000 using namespace std; int par[mx], n, m, ans; int Find(int x) { return par[x] == x ? x : par[x] = Find(par[x]); } void Union(int x, int y) { int fx = Find(x), fy = Find(y); if (fx == fy) return ; par[fy] = fx; ans--; } int main() { int cas = 0, x, y; while (scanf("%d%d", &n, &m) && (n||m)) { ans = n; for (int i = 1; i <= n; ++i) par[i] = i; while (m--) { scanf("%d%d", &x, &y); Union(x, y); } printf("Case %d: %d\n", ++cas, ans); } return 0; }
相关文章推荐
- HDU1272小希的迷宫--并查集入门题
- HDU 1232 并查集 (入门)
- (并查集入门)P1034家族
- hdu 1213 并查集入门
- POJ1308 Is It A Tree? 并查集入门|判树
- 感染者(并查集入门)
- hdu 1232 并查集入门
- 并查集入门(hdu1272)
- 杭电1232——畅通工程(并查集的入门…
- HDOJ 1102 Constructing Roads(并查集入门)
- 【并查集入门专题1】F - How Many Tables hdu1213【并查集水题】
- 并查集入门经典题:畅通工程
- (并查集入门)畅通工程
- LA 3644 X-Plosives && LA 3027 Corporative Network (并查集入门)
- 并查集入门
- POJ 1703 Find them, Catch them 种类并查集(入门)
- POJ 1182 食物链 带权并查集入门
- 并查集入门--hd 1863
- Ubiquitous Religions 并查集入门
- 并查集三道入门题