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 different R Eligions 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
Test instructions: give you n students, M-line of faith the same two people want you to find the largest number of religious beliefs
Idea: And check the application of the set. Process 2 sets of data per input. Assuming that these 2 data have not yet been marked, the maximum number of religions is reduced by 1.
AC Code:
#include <stdio.h> #define N 50005int f[n],n,sum;int Find (int x) { if (x!=f[x]) f[x]=find (f[x]); return f[x];} void make (int x,int y) { int a=find (x); int B=find (y); if (a!=b) { f[a]=b; sum--;} } int main () { int ans=1,m; while (scanf ("%d%d", &n,&m)!=eof) { sum=n; if (n==0&&m==0) break; for (int i=1;i<n;i++) f[i]=i; for (int i=1;i<=m;i++) { int a, b; scanf ("%d%d", &a,&b); Make (A, b); } printf ("Case%d:%d\n", ans,sum); ans++; } return 0;}
POJ 2524 Ubiquitous Religions