Links: http://poj.org/problem?id=2524
Ubiquitous Religions
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 27774 |
|
Accepted: 13610 |
Description
There is, many different religions in the world today that it's difficult to keep track of them all. You is interested in finding-many different religions students in your university believe in.
You know that there is n students in your university (0 < n <= 50000). It is infeasible for your to ask every student their religious beliefs. Furthermore, many students is not comfortable expressing their beliefs. One-to-avoid these problems is-ask m (0 <= m <= N (n-1)/2) pairs of students and ask them whether they believe In the same religion (e.g they may know if they both attend the same church). From this data, you may not be know what is believes in, but can get a idea of the the upper bound of how many diff Erent religions can is possibly represented on campus. You may assume the student subscribes to the most one religion.
Input
The input consists of a number of cases. Each case starts with a line specifying the integers n and M. The next m lines each consists of both integers I and j, specifying that students I and J believe in the same religion. The students is numbered 1 to N. The end of input is specified by a line in which n = m = 0.
Output
for each test case, print to a single line the case number (starting with 1) followed by the maximum number of differe NT Religions that the students in the university believe in.
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
Huge input, scanf is recommended.
Test instructions: A student wants to know how many religious types the school students believe in, so in the total number of students is N, he investigates the m pair and finally outputs the total number of different religions.
The idea of solving problems: it is obvious to use the difference set. All we have to do is divide the N students into a set, the belief that the same religion is placed in the same set, the final set number is the number of different religions, that is, the question is converted to how many different sets.
<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int Maxn=50010;int father[maxn],rank[maxn];int b[maxn];int Find (int x)//parent node Find {if (x==father[x]) ret Urn x; else return Father[x]=find (Father[x]);} void Unite (int x,int y)//collection Merge {int fx=find (x); int Fy=find (y); if (fx==fy) return; if (Rank[fx]>rank[fy]) {father[fy]=fx; } else{Father[fx]=fy; if (Rank[fx]==rank[fy]) rank[fy]++; }}int Main () {int x,y,n,m,cas=0; while (scanf ("%d%d", &n,&m) && n+m) {for (int i=1;i<=n;i++) {father[i]=i;rank[i]=0;} while (m--) {scanf ("%d%d", &x,&y); Unite (x, y); } for (int i=1;i<=n;i++) b[i]=find (i);//Just beginning fing (i) I wrote it into Father[i], and the result WA five or six times, in this we encourage the sort (b+1,b+ N+1); int Ans=1; for (int i=2;i<=n;i++) if (b[i]! = b[i-1]) ++ans; printf ("Case%d:%d\n", ++cas,ans); } return 0;} </span>
Copyright NOTICE: This article for Bo Master original article, reprint remember famous source, thank you!
POJ 2524 Ubiquitous religions (religious species: and the difference set)