Openjudge 百练 / 2524 - 宗教信仰 [并查集]

URL: http://bailian.openjudge.cn/practice/2524/

【描述】

世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教。

你的学校有n名学生(0 < n <= 50000),你不太可能询问每个人的宗教信仰,因为他们不太愿意透露。但是当你同时找到2名学生,他们却愿意告诉你他们是否信仰同一宗教,你可以通过很多这样的询问估算学校里的宗教数目的上限。你可以认为每名学生只会信仰最多一种宗教。

【输入】

输入包括多组数据。

每组数据的第一行包括n和m,0 <= m <= n(n-1)/2,其后m行每行包括两个数字i和j,表示学生i和学生j信仰同一宗教,学生被标号为1至n。输入以一行 n = m = 0 作为结束。

【输出】

对于每组数据,先输出它的编号(从1开始),接着输出学生信仰的不同宗教的数目上限。

【样例输入】

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

【样例输出 】

Case 1: 1
Case 2: 7

OI C++ Code:

#include <iostream>
#include <cstring>
using namespace std;

int father[50000], root_sum[50000];

int get_root(int child)
{
    while (father[child] != child)
    {
        child = father[child];
    }
    return child;
}

int main() {
    int cnt = 0;
    while (true)
    {
        int n, m;
        cin >> n >> m;

        if (m == 0 && n == 0)
        {
            break;
        }

        ++cnt;
        for (int i = 0; i < n; ++i)
        {
            father[i] = i;
        }
        for (int k = 0; k < m; ++k)
        {
            int i, j;
            cin >> i >> j;
            --i;
            --j;
            father[i] = get_root(i);
            father[j] = get_root(j);
            if (father[i] != father[j])
            {
                father[father[i]] = father[j];
            }
        }

        memset(root_sum, 0, sizeof(root_sum));
        for (int i = 0; i < n; ++i)
        {
            ++root_sum[get_root(i)];
        }
        int root_num = 0;
        for (int i = 0; i < n; ++i)
        {
            if (root_sum[i] != 0)
            {
                ++root_num;
            }
        }
        cout << "Case " << cnt << ": " << root_num << endl;
    }

    return 0;
}

C++ Code:

#include <iostream>
#include <vector>
using namespace std;

int get_root(const vector<int> &father, int child)
{
    while (father[child] != child)
    {
        child = father[child];
    }
    return child;
}

int main() {
    int cnt = 0;
    while (true)
    {
        int n, m;
        cin >> n >> m;

        if (m == 0 && n == 0)
        {
            break;
        }

        ++cnt;
        vector<int> father(n);
        for (int i = 0; i < n; ++i)
        {
            father[i] = i;
        }
        for (int k = 0; k < m; ++k)
        {
            int i, j;
            cin >> i >> j;
            --i;
            --j;
            father[i] = get_root(father, i);
            father[j] = get_root(father, j);
            if (father[i] != father[j])
            {
                father[father[i]] = father[j];
            }
        }

        vector<int> root_sum(n, 0);
        for (int i = 0; i < n; ++i)
        {
            ++root_sum[get_root(father, i)];
        }
        int root_num = 0;
        for (int i = 0; i < n; ++i)
        {
            if (root_sum[i] != 0)
            {
                ++root_num;
            }
        }
        cout << "Case " << cnt << ": " << root_num << endl;
    }

    return 0;
}
时间: 2024-11-08 07:08:40

Openjudge 百练 / 2524 - 宗教信仰 [并查集]的相关文章

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

POJ 2524 Ubiquitous Religions (幷查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted: 11378 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 findi

POJ 2524 (简答并查集) Ubiquitous Religions

题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 50000 + 10; 8 int parent[maxn], n, m; 9 10

Poj OpenJudge 百练 1860 Currency Exchang

1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20706   Accepted: 7428 Description Several currency exchange points are working

Poj OpenJudge 百练 2602 Superlong sums

1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlong sums Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22337   Accepted: 6577 Description The creators of a new programming language D++

Openjudge 百练第4109题

1 # -*- coding:utf-8 -*- 2 3 def set_1(i, q): 4 ''' generate a i*i ARRAY for all relationships 5 if there is a relation set 1 or 0 6 return i*i ARRAY with 1 or 0 7 ''' 8 a = [0 for i in range(i*i)] 9 for j in range(len(q)): 10 n, m = q[j] 11 a[(n-1)*

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

Poj OpenJudge 百练 1062 昂贵的聘礼

1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37631   Accepted: 10872 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金

Poj OpenJudge 百练 1573 Robot Motion

1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10856   Accepted: 5260 Description A robot has been programmed to follow the instru