Ubiquitous Religions ---并查集入门

来源:互联网 发布:matlab矩阵逻辑运算 编辑:程序博客网 时间:2024/04/28 17:57
Time Limit: 1000MS
Memory Limit: 20000KTotal Submissions: 21382
Accepted: 10356

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

<span style="font-size: 18px;"><strong>100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0</strong></span>

Sample Output

<span style="font-size: 18px;"><strong>411</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 52000using 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;}

1 0
原创粉丝点击