• 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 }
    功不成,身已退
  • 相关阅读:
    Asp.net弹出浏览器客户端确认对话框代码 Carlwave
    VS 2005 与SQL Server 2005整合优势在哪里?(from csdn.net) Carlwave
    如何让搜索引擎收录我的站点 Carlwave
    超强扩展性的DNNDotNetNuke模块功能分类列表(from 中国DNN) Carlwave
    DotNetNuke命名空间概述 Carlwave
    Most Popular Questions and Answers on ASP.NET Whidbey(from asp.net forums,write by ASP.NET Team) Carlwave
    火箭官方宣告麦蒂缺阵五周 季后赛前景蒙上阴影 Carlwave
    asp.net有效使用缓存(转) Carlwave
    《Business Rules Engine Overview》《业务规则引擎概述》write by Mark Kamoski Carlwave
    中国详细省市县自治区名称列表(含access数据库和sql2000备份数据库) Carlwave
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2600607.html
Copyright © 2020-2023  润新知