POJ2524 Ubiquitous Religions 并查集
来源:互联网 发布:青岛惠普大数据烂尾 编辑:程序博客网 时间:2024/05/16 15:44
1.题目描述:
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.
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.
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.
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.
10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Case 1: 1Case 2: 7
Huge input, scanf is recommended.
2.题意概述:一个学校学生可能信奉的宗教不同,要你判断也多少个宗教
3.解题思路:
并查集裸题
4.AC代码:
#include <stdio.h>#define maxn 50500using namespace std;int pa[maxn];int n;void init(){for (int i = 1; i <= n; i++)pa[i] = i;}int findset(int x){while (pa[x] != x)x = pa[x];return x;}void unionset(int a, int b){int a1 = findset(a);int b1 = findset(b);if (a1 != b1)pa[a1] = b1;}int main(){int m, kase = 0;while (scanf("%d%d", &n, &m) != EOF && n + m){int ans = 0;init();for (int i = 0; i < m; i++){int tmp1, tmp2;scanf("%d%d", &tmp1, &tmp2);unionset(tmp1, tmp2);}for (int i = 1; i <= n; i++){if (pa[i] == i)ans++;}printf("Case %d: %d\n", ++kase, ans);} return 0;}
0 0
- POJ2524 Ubiquitous Religions 并查集
- poj2524 Ubiquitous Religions 并查集入门
- POJ2524 Ubiquitous Religions 并查集
- 【并查集】:poj2524,Ubiquitous Religions
- 并查集-poj2524-Ubiquitous Religions
- POJ2524 Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions 并查集
- POJ2524 Ubiquitous Religions(简单并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524--Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524——Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions 暨并查集入门
- POJ2524 - Ubiquitous Religions (并查集的基础应用)
- poj2524——Ubiquitous Religions(并查集)
- POJ2524——Ubiquitous Religions 并查集基础
- POJ2524 Ubiquitous Religions —— 并查集
- oracle隐含参数的查看与修改
- 抽象工厂
- Win7和linux双系统主引导丢失解决办法
- linux date命令计算时间
- ubuntu 16.04+kinect v1配置运行 Kintinuous
- POJ2524 Ubiquitous Religions 并查集
- LeetCode 84. Largest Rectangle in Histogram(Hard)
- Spark on Yarn集群搭建
- hibernate 延迟加载的错误
- 2017/2/20-2017/2/26
- 是时候来一发高精度辣!!
- 自定义View:测量measure,布局layout,绘制draw
- win7 x64调试驱动
- MySQL时间戳转换时间10位 13位