Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 23093   Accepted: 11379

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

解题思路:

并查集的简单应用。

最后分成了多少集合就是结果。

代码:

#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn=50010;
int parent[maxn];
int n,m; void init(int n)
{
for(int i=1;i<=n;i++)
parent[i]=i;
} int find(int x)
{
return parent[x]==x?x:find(parent[x]);
} void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
else
parent[x]=y;
} int main()
{
int c=1;
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
int x,y;
init(n);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
unite(x,y);
}
int cnt=0;
for(int i=1;i<=n;i++)
if(parent[i]==i)
cnt++;
cout<<"Case "<<c++<<": "<<cnt<<endl;
}
return 0;
}

[ACM] POJ 2524 Ubiquitous Religions (并查集)的更多相关文章

  1. POJ 2524 Ubiquitous Religions (幷查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted:  ...

  2. poj 2524 Ubiquitous Religions (并查集)

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

  3. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  4. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  5. poj 2524 Ubiquitous Religions(并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted:  ...

  6. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

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

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

  8. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  9. POJ 2524 Ubiquitous Religions

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 20668   Accepted:  ...

随机推荐

  1. Django+Nginx+uwsgi搭建自己的博客(八)

    在这篇博客中,我们开始为我们的博客开发Blogs App和Users App相关的管理功能,以便每个用户都能管理自己的博客以及评论.目前,Users App的管理功能相对简单,主要功能为查看用户资料以 ...

  2. 【我要学python】函数的系统学习

    我的短期目标:python+CTF reverse 一起加油! #1,函数介绍 1,功能性 函数目的 2,隐藏性 (封装) (避免写重复代码) 例: #round为保留小数的函数 a = 3.1415 ...

  3. BZOJ2599 IOI2011Race

    题目:一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 点分治,我们考虑经过根节点的路径,t[x]表示路径长为x时最少的边数,然后每次拿栈记下来清空. 注意先搜索再更新,这样可以避免同一 ...

  4. 「NOIP2018」保卫王国

    「NOIP2018保卫王国」 题目描述 有一棵 \(n\) 个点, 点有点权 \(a_i\),\(m\) 组询问, 每次求钦点两个节点必须选或者必须不选后的树上最小点覆盖. \(1 \leq n, m ...

  5. 51nod1437 迈克步 单调栈

    考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...

  6. PHP链接sqlserver出现中文乱码

    PHP通过dblib扩展链接sqlserver,使用的是freetds,出现中文乱码. 在freetds的配置文件中(/usr/local/freetds/etc/freetds.conf),[glo ...

  7. CentOS 6.9/7通过yum安装指定版本的JDK/Maven

    说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql xxx可以知道全部文件的地方等等. 一.安装JDK(Oracle JDK 1.8) # wget -- ...

  8. Advanced Features of Delphi DLLs

    http://www.delphisources.ru/pages/faq/master-delphi-7/content/LiB0104.html Beside this introductory ...

  9. FreeRTOS API

    Task Creation xTaskCreate vTaskDelete Task Control vTaskDelay vTaskDelayUntil uxTaskPriorityGet vTas ...

  10. springmvc 配置和spring配置?

    最近在接触mybatis,之间使用springmvc时,配置文件一直是,web.xml+XX-servlet.xml 的配置(xx为web.xml中servlet name名称).为了整合mybati ...