POJ 2524 Ubiquitous Religions (query set)

Source: Internet
Author: User

POJ 2524 Ubiquitous Religions (query set)
Ubiquitous Religions

Time Limit:5000 MS Memory Limit:65536 K
Total Submissions:25556 Accepted:12619

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 <n <= 50000 ). it is infeasible for you to ask every student their religious beliefs. furthermore, inclustudents are not comfortable expressing their beliefs. one way to avoid these problems is to 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 know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. you may assume that each student subscribes to at most one religion.

Input

The input consists of a number of instances. each case starts with a line specifying the integers n and m. the next m lines each consists of two integers I and j, specifying that students I and j believe in the same religion. the students are numbered 1 to n. the end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different 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.
When I look at the plan, it refers to sub-governance, but I think it's just about using and querying the set. I'm wondering if the classification of the plan is wrong, then I went to the Internet to check my blog. Everyone used and checked the collection, and there was no sub-governance ~ Knowing how to use and query sets is simple: Initialize the f [] array. If two people share the same religion, merge them, finally, traverse the f [] array to see if there are still a few unmerged answers. (However, at the end of this article, you do not need to traverse the f [] array. You can merge the number of students once. The total number of students is n -- and the last n is the answer, this may be faster ~) In addition, I defined m as 64-bit here for fear of overflow. I don't know if int will be cracked here. I didn't try it. If you know it, I 'd like to comment on it. Thank you ~ Pai_^
#include 
 
  #include 
  
   #include 
   
    #define MAX 50000typedef long long ll;struct UFSet{int f[MAX+2];int i;void init(int n){for(i=0;i<=n;i++)f[i]=i;}int find(int x){return f[x]==x?x:f[x]=find(f[x]);}void Union(int x,int y){if((x=find(x))==(y=find(y))) return ;f[y]=x;}}S;int main(){int i,j,n,res,cas=1;ll m;int a,b;while(scanf("%d%lld",&n,&m)!=EOF){if(n==0 && m==0)break;S.init(n);res=0;for(i=0;i
    
     

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.