poj2524 Ubiquitous Religions

Posted Al_X

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2524 Ubiquitous Religions相关的知识,希望对你有一定的参考价值。

Ubiquitous Religions

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 34826   Accepted: 16788

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

Hint

Huge input, scanf is recommended.

Source

 
题目大意:给出学生总人数与已知相同宗教的对数,给出已知相同宗教的学生编号,求最多有多少种不同宗教。
 
解题思路:若输入的一对学生原先不连通则总宗教数-1,若原先联通则总宗教数不变。所以关键是如何判断所输入的数原先是否连通。
 
  开始我是打算用几个数组来存放相同同宗教的学生的编号,一个宗教开一个数组,在数组中查找输入的编号原先是否存在,但是这种方法会超时,不可取。
  后来取用并查集的办法,每个不同的宗教选取一个头结点,那么判断两学生原先是否连通则可以通过递归查找头节点,判断头结点是否相等的方式来判断。若原先不连通则将其中一个的头结点设置成另一个的头结点的子节点,再将总宗教数减一即可。
 
完整代码:
技术分享
#include <iostream>
using namespace std;
int parent[50005];
int findSource(int s)
{
    return parent[s]==s?s:findSource(parent[s]);
}
int main()
{
    ios::sync_with_stdio(false);
    int n,m,CASE=0;
    while(cin>>n>>m)
    {
        CASE++;
        if(n==0&&m==0)
        {
            return 0;
        }
        int s1,s2;
        for(int i=0;i<50005;++i)
        {
            parent[i]=i;
        }
        while(m--)
        {
            cin>>s1>>s2;
            if(findSource(s1)!=findSource(s2))
            {
                parent[findSource(s2)]=findSource(s1);
                n--;
            }
        }
        cout<<"Case "<<CASE<<": "<<n<<endl;
    }
    return 0;
}
View Code

 



以上是关于poj2524 Ubiquitous Religions的主要内容,如果未能解决你的问题,请参考以下文章

POJ2524 Ubiquitous Religions

poj2524 Ubiquitous Religions

POJ 2524 Ubiquitous Religions

POJ 2524 独一无二的宗教(裸并查集)

poj2524(并查集水题)

POJ2524