Ubiquitous Religions
Time Limit: 5000MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status
Description
There are many different religions in the world today and it is difficult to understand them. You are interested in finding out how many different religions are in your university. You know there are n students in your university (0 < n <= 50000). You cannot ask the religious beliefs of each student. In addition, many students don't want to say what they believe in. One way to avoid these problems is to ask m (0 <= m <= N (n-1)/2) to students and ask them whether they believe in the same religion (for example they may know whether they have gone to the same church). In this data, you may not know the religion that everyone believes in, but you can see how many different religions are possible on campus. It is assumed that each student believes in one religion at most.
Input
There are multiple sets of data. For each set of data: The first line: two integers n and M. The following m lines: Each row contains two integers I and j, representing the students I and J believe in the same religion. Student numbers from 1 to N. In the last line of the input, n = m = 0.
Output
For each set of test data, output a row, output the data sequence number (starting from 1) and the maximum number of different religions in the university. (see example)
Sample Input
10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Sample Output
Case 1:1case 2:7
Hint
The input is huge and scanf is recommended.
Simple and check the set, to find the undirected graph connected components.
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int maxn = 50000 + 50; int parent[maxn];int N, m;void Make_set () {for (int i = 0; I <= N; i++) parent[i] = 1;} int find_set (int t) {if (parent[t] = =-1) return T;elsereturn parent[t] = Find_set (Parent[t]);} void Union_set (int a, int b) {int T1 = Find_set (a); int t2 = Find_set (b); if (t1! = t2) Parent[t2] = t1;} int main () {int casen;casen = 1;while (scanf ("%d%d", &n, &m)! = eof&&n) {int A, b;make_set (); for (int i = 0; I < m; i++) {scanf ("%d%d", &a, &b); Union_set (A, b);} int sum = 0;for (int i = 1; I <= n; i++) {if (parent[i] = = 1) sum++;} printf ("Case%d:", Casen); casen++;p rintf ("%d\n", Sum);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ-2524 Ubiquitous Religions (Simple and collection)