Ubiquitous Religions(POJ并查集板子题)

原题链接

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.

单词

  • infeasible 不可行的
  • Furthermore 此外
  • subscribes to 订阅
  • at most 最多
  • specify 指定

原文翻译

当今世界上有太多不同的宗教,很难一一掌握。你有兴趣了解你大学中有多少不同宗教信仰的学生。你知道你大学里有n个学生。你问每个学生他们的宗教信仰是不可行的。此外,许多学生不愿意表达自己的信念。避免这些问题的一种方法是,问m对学生,并询问他们是否信仰相同的宗教。 从这些数据中,你可能不知道每个人都信仰什么,但是可以对校园中可以代表多少种不同宗教的上限有所了解(可以假设每个学生最多参加一种宗教)

题目大意

有n个人,每人都有一个信仰,有m个描述,每个描述是两个数字x和y表示x和y是同一信仰,现在问信仰的最大可能是多少?
题目问的是信仰的上限,当告知x和y信仰相同时,需要做的就是将两个群体合并,通过这种方式维护集合的个数,最终有多少独立的集合即是答案

样例分析

  • 样例1

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10

最终只有一个集合:1-2-3-4-5-6-7-8-9-10

  • 样例2

10 4
2 3
4 5
4 8
5 8

最终有7个集合:
①1 ②2-3 ③4-5-8 ④6 ⑤7 ⑥9 ⑦10

AC代码

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 50010;
int parent[N];//disjoint set

int find(int x)//find + path compression
{
    if (x != parent[x]) parent[x] = find(parent[x]);
    return parent[x];
}

void Union(int x,int y)//union
{
    int a = find(x);
    int b = find(y);
    if (a != b) parent[a] = b;
}

int main()
{
    int n,m;
    int num = 0;//num = case
    while (scanf("%d%d",&n,&m) != EOF)
    {
        num++;
        if (n == 0 && m == 0) break;
        
        for (int i = 1;i <= n;i++) parent[i] = i;//init disjoint set
        
        int x,y;
        for (int i = 0;i < m;i++)
        {
            scanf("%d%d",&x,&y);
            Union(x,y);
        }
        
        int sum = 0;//sum = the number of disjoint set
        for (int i = 1;i <= n;i++)
        {
            if (parent[i] == i) sum++;
        }
        
        printf("Case %d: %d\n",num,sum);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值