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:25383 Accepted:12530

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.

Enter n. m indicates there are n gangs. Enter m rows x and y below to represent Student x and y. Then, ask how many gangs are there.

Train of Thought: typical query set. Find each set and count the number of these sets.


#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;int uset[100010];int rank[100010];void makeset(int n){    memset(uset,0,sizeof(uset));    for(int i=1;i<=n;i++)        uset[i]=i;}int find(int x){    if(uset[x]!=x)        uset[x]=find(uset[x]);    return uset[x];}void unionset(int x,int y){    if((x=find(x))==(y=find(y)))        return;    if(rank[x]>rank[y])        uset[y]=x;    else    {        uset[x]=y;        if(rank[x]==rank[y])            rank[y]++;    }}int main(){    int n,m,i;    int a,b;    int t=1;    while(~scanf("%d %d",&n,&m))    {        if(n==0&&m==0)            break;        makeset(n);        while(m--)        {            scanf("%d %d",&a,&b);            unionset(a,b);        }        int sum=0;        for(i=1;i<=n;i++)        {            if(uset[i]==i)                sum ++;        }        printf("Case %d: %d\n", t++, sum);    }    return 0;}
    
   
  
 


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.