zoukankan      html  css  js  c++  java
  • Ubiquitous Religions

    Ubiquitous Religions 
    Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 2   Accepted Submission(s) : 2
    Problem 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, many students 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 cases. 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 9
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    10 4
    2 3
    4 5
    4 8
    5 8
    0 0
     
    
    Sample Output
    Case 1: 1
    Case 2: 7
     1 /*
     2 //两种初始化方法
     3 #include<iostream>
     4 #include<cstring>
     5 using namespace std;
     6 int father[50001];
     7 
     8 int find_father(int num)
     9 {
    10     while(father[num]>0)
    11         num=father[num];
    12     return num;
    13 }
    14 
    15 int main()
    16 {
    17     int m,n,num,count=1;
    18     while(cin>>n>>m,m||n)
    19     {
    20         int i,fa,fb,a,b;
    21         num=0;
    22         memset(father,-1,sizeof(father));
    23         for(i=0;i<m;++i)
    24         {
    25             cin>>a>>b;
    26             fa=find_father(a);
    27             fb=find_father(b);
    28             if(fa!=fb)
    29                 father[fa]=fb;
    30         }
    31         for(i=1;i<=n;++i)
    32             if(father[i]<0)
    33                 ++num;
    34         cout<<"Case "<<count++<<": "<<num<<endl;  
    35     }
    36     return 0;
    37 }
    38 */
    39 
    40 #include<iostream>
    41 #include<cstring>
    42 using namespace std;
    43 int father[50001];
    44 
    45 int find_father(int num)
    46 {
    47     while(father[num]!=num)
    48         num=father[num];
    49     return num;
    50 }
    51 
    52 int main()
    53 {
    54     int m,n,num,count=1;
    55     while(cin>>n>>m,m||n)
    56     {
    57         int i,fa,fb,a,b;
    58         num=0;
    59         for(i=1;i<=n;++i)
    60             father[i]=i;
    61         for(i=0;i<m;++i)
    62         {
    63             cin>>a>>b;
    64             fa=find_father(a);
    65             fb=find_father(b);
    66             if(fa!=fb)
    67                 father[fa]=fb;
    68         }
    69         for(i=1;i<=n;++i)
    70             if(father[i]==i)
    71                 ++num;
    72         cout<<"Case "<<count++<<": "<<num<<endl;  
    73     }
    74     return 0;
    75 }
    功不成,身已退
  • 相关阅读:
    根据OpenID列表群发 高级群发消息
    redis的使用:获取redis实例的工具类
    火狐,谷歌浏览器checkbox全选的问题
    ie浏览器中图片周围有黑色边框的样式不兼容的问题
    JAVA学习笔记-04
    JAVA学习笔记-03
    JAVA学习笔记-02
    JAVA学习笔记-01
    第一天
    Storm HBase 集成
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2600607.html
Copyright © 2011-2022 走看看