POJ 2524 Ubiquitous Religions (religious belief)

Source: Internet
Author: User

Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 30666

Accepted: 14860

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 believ E 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.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <fstream>5 using namespacestd;6 intf[50005];7 8 intFindintx) {9     if(X! =F[x])TenF[X] =find (F[x]); One     returnF[x]; A } -  - voidUnion (intAintb) { the     intF1 =find (a); -     intF2 =find (b); -     if(F1! =F2) -F[F2] =F1; + } -  + intMain () { A     //ifstream cin ("Aaa.txt"); at     intN, m, test =1, sum; -  -      while(SCANF ("%d%d", &n, &m)) { -         if(n = =0&& m = =0) -              Break; -Memset (F,0,sizeof(f)); in  -          for(inti =1; I <= N; i++) { toF[i] =i; +         } -  thesum =N; *  $          for(inti =1; I <= m; i++) {Panax Notoginseng             intA, B; -scanf"%d%d", &a, &b); the             if(Find (a)! =find (b)) { + Union (A, b); Asum--; the             } +         } -  $printf"Case %d:%d\n", test++, sum); $  -     } -     //System ("pause"); the     return 0; -}
View Code

1#include <stdio.h>2#include <iostream>3 using namespacestd;4 5 Const intMAXN =50005;/*number of nodes on line*/6 intPA[MAXN];/*P[x] Represents the parent node of x*/7 intRANK1[MAXN];/*Rank[x] is an upper bound of the height of x*/8 intN, ans;9 Ten voidMake_set (intx) One{/*Create a unit set*/ APA[X] =x; -RANK1[X] =0; - } the  - intFind_set (intx) -{/*find with path compression*/ -     if(X! =Pa[x]) +PA[X] =Find_set (pa[x]); -     returnPa[x]; + } A  at /*merge x, y with the collection by rank*/ - voidUnion_set (intXinty) - { -x =Find_set (x); -y =Find_set (y); -     if(x = = y)return ; inans--;//Statistics -     if(Rank1[x] > Rank1[y])/*make rank taller as the parent node .*/ to     { +Pa[y] =x; -     } the     Else  *     { $PA[X] =y;Panax Notoginseng         if(Rank1[x] = =Rank1[y]) -rank1[y]++; the     } + } A //answer to 2524 the intMain () + { -     intM, I, j =1, x, y; $      while(SCANF ("%d%d", &n, &m)) $     { -         if(n = = m && m = =0) Break; -          for(i =1; I <= N; i++) the Make_set (i); -Ans =N;Wuyi          for(i =0; I < m; i++) the         { -scanf"%d%d", &x, &y); Wu union_set (x, y); -         } Aboutprintf"Case %d:%d\n", J, ans); $J + +; -     } -     return 0; -}
View Code

POJ 2524 Ubiquitous Religions (religious belief)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.