02: religious beliefs 02: religious beliefs

Source: Internet
Author: User

02: religious beliefs 02: religious beliefs

Total time limit:
5000 ms
 
Memory limit:
65536kB
Description
There are many religions in the world. You are interested in the number of religions your school students believe. Your school has n students (0 <n <= 50000). You are unlikely to ask everyone about their religious beliefs because they are reluctant to disclose their beliefs. But when you find two students at the same time, they are willing to tell you if they believe in the same religion. You can estimate the upper limit of the number of religions in the school by asking a lot like this. You can think that each student only believes in one of the most religions.

 

Input
The input includes multiple groups of data.
The first row of each group of data includes n and m, 0 <= m <= n (n-1)/2, and the next m row contains two numbers, I and j, indicates that student I and student j share the same religion and the student ID is 1 to n. The input end with a row of n = m = 0.
Output
For each group of data, first output its number (starting from 1), and then output the maximum number of different religions of students' beliefs.
Sample Input
10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Sample output
Case 1: 1 Case 2: 7

First glance: Tarjan
Second eye: just blind, and check the collection to take away

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=50001; 7 void read(int &n) 8 { 9     char c='+';int x=0;bool flag=0;10     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}11     while(c>='0'&&c<='9')12     x=(x<<1)+(x<<3)+c-48,c=getchar();13     flag==1?n=-x:n=x;14 }15 int n,m;16 int fa[MAXN];17 bool vis[MAXN];18 int find(int x)19 {20     if(fa[x]==x)21         return fa[x];22     return fa[x]=find(fa[x]);23 }24 void unionn(int x,int y)25 {26     int fx=find(x);27     int fy=find(y);28     fa[fx]=fy;29 }30 int tot=0;31 int main()32 {33     while(scanf("%d%d",&n,&m))34     {35         if(n==0&&m==0)break;36         for(int i=1;i<=n;i++)37             fa[i]=i;38         memset(vis,0,sizeof(vis));39         40         for(int i=1;i<=m;i++)41         {42             int x,y;43             read(x);read(y);44             if(find(x)!=find(y))45                 unionn(x,y);46         }47         int ans=0;48         for(int i=1;i<=n;i++)49         {50             if(vis[find(i)]==0)51             {52                 vis[find(i)]=1;53                 ans++;54             }55         }56         printf("Case %d: %d\n",++tot,ans);57     }58     return 0;59 } 

 



Related Article

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.