题目:
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.

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 50005
int p[MAX];
int rank[MAX];
int sum2 = 1,sum;
int find_father(int x)
{int r = x,temp;while(r != p[r]) r = p[r];while(x != p[x]){temp = p[x];p[x] = r;x = temp;}return x;
}
void combine(int a,int b)
{int c = find_father(a);int d = find_father(b);if(c == d) return;if(rank[c] > rank[d]) p[d] = c;else{p[c] = d;if(rank[c] == rank[d]) rank[c]++;}
}
void init(int n)
{for(int i = 1;i <= n;i++) {p[i] = i;rank[i] = 0;}
}
int main()
{int n,m,a,b;while(~scanf("%d%d",&n,&m)){if(n == 0){if(m == 0) break;else {cout << "Case " << sum2 << ": " << sum << endl;sum2++;continue;}}init(n);sum = 0;for(int i = 0;i < m;i++){cin >> a >> b;combine(a,b);}for(int i = 1;i <= n;i++){if(p[i] == i) sum++;}cout << "Case " << sum2 << ": " << sum << endl;sum2++;}return 0;
}

这是一道简单的并查集题目,这里将同样宗教信仰的学生归为一类,为了降低寻找父结点的时间,可以采用路径压缩,或者在合并时高度低的归为高度高的那类,依次来减少总体高度。

计算宗教总数时,遍历所有学生,要是这个点父结点等于其本身就总数累加。

Ubiquitous Religions相关推荐

  1. Ubiquitous Religions (并查集)

    Ubiquitous Religions POJ - 2524 当今世界有许多不同的宗教,很难跟踪它们.你有兴趣找出你大学里有多少不同的宗教学生相信. 你知道你的大学里有 n 学生 (0 < n ...

  2. 【并查集】POJ 2524 Ubiquitous Religions

    POJ 2524 Ubiquitous Religions 就是看集合的数量是多少,将所有节点的祖先结点放进set,输出size就行了 #include <iostream> #inclu ...

  3. POJ-2524 Ubiquitous Religions(无处不在的宗教)解题报告(并查集)

    目录 题目描述 思路分析 今天没有考试,那就再刷点题吧(难道就不怕c语言挂科吗?).因为昨天写了道并查集,所以今天再来一道,还是有所收获的. 题目描述 题目:https://vjudge.net/pr ...

  4. poj 2524 Ubiquitous Religions (简单并查集)

    题目链接:http://poj.org/problem?id=2524 There are so many different religions in the world today that it ...

  5. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  6. POJ2524——宗教(Ubiquitous Religions)【图论,并查集】

    正题 题目链接: http://poj.org/problem?id=2524 大意 有n个学生,告诉你哪两个学生的宗教相等,求校园里有多少个宗教. 解题思路 并查集链接就好了 代码 #include ...

  7. SSL-ZYC POJ 2524 Ubiquitous Religions

    题目大意: 你知道你的大学里有N个学生.每个人都信仰宗教,你向每个学生请教他们的宗教信仰是不可行的.避免这些问题的一种方法是问M对学生,问他们是否相信同一宗教.从这些数据中,你可能不知道每个人都相信什 ...

  8. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  9. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  10. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

最新文章

  1. TensorFlow解析常量、变量和占位符
  2. 吴恩达团队最新成果:用深度学习来改善临终关怀服务
  3. Linux / argv、environ 和 env 的联系
  4. Swift5以及IOS15对于二维码的使用
  5. java开发中对于程序员的几点建议,你们有想到吗?
  6. postman生成python代码_别再用手敲了,这个工具可以自动生成python爬虫代码
  7. 线程的特点 java 1615387415
  8. 项目管理需要使用到的图表
  9. EfficientNet-B4-Ranger:自然复杂环境下温室黄瓜病害识别新方法(同时存在两种疾病)
  10. Java 网络处理(net io URL 等)
  11. android 8 twrp,一加8 解锁bl 刷写第三方twrp 资源
  12. iphone开蓝牙wifi上网慢_iPhone手机网速慢解决方法
  13. 2022 极术通讯-安谋科技纷争尘埃落定,本土半导体产业基石更稳
  14. 美国男人欢迎中国的丑女人?------------说说洁
  15. 世间什么才是最珍贵的?
  16. IOS开发之 ---- 苹果系统代码汉字转拼音
  17. 关于C++ STL中的upper_bound()
  18. Rio手把手教学:如何打造容器化应用程序的一站式部署体验
  19. scp 和 rsync
  20. 带按钮的图片横幅广告_显示带有文字的横幅记住使用cookie的选择

热门文章

  1. 三维分子模型软件PyMOL
  2. Thread线程中的stop方法过时问题
  3. 30天自制操作系统——第一天到第二天
  4. 利用C++创建一个游戏(1)窗口框架(代码+注释)
  5. 人人影视携2000万用户进军区块链,区块链会流行起来吗?
  6. 计算机网络协议 | 只有程序员才能读懂的西游记 | 让我这样学习简直就是一种享受~
  7. Android小技巧
  8. Enterprise Architect 15 使用指南 II
  9. 付费推广的投入产出比达到多少才合理?
  10. 线性MMSE检波和ZF检波