poj 2524:Ubiquitous Religions(并查集,入门题)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 23997 | Accepted: 11807 |
Description
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
Output
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
Source
#include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 50010 int par[MAXN]; //par[x]表示x的父节点
int n; void Init() //初始化
{
int i;
for(i=;i<=n;i++)
par[i] = i;
} int Find(int x) //查询x的根节点并路径压缩
{
if(par[x]!=x)
par[x] = Find(par[x]);
return par[x];
} void Union(int x,int y) //合并x和y所在集合
{
par[Find(x)] = Find(y);
} int main()
{
int m,x,y,i,Case=;
while(scanf("%d%d",&n,&m)!=EOF){
if(n== && m==)
break;
//初始化
Init();
//m次询问
while(m--){
scanf("%d%d",&x,&y);
Union(x,y);
}
int ans = n;
for(i=;i<=n;i++) //统计
if(par[i]!=i)
--ans;
printf("Case %d: %d\n",Case++,ans);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 2524:Ubiquitous Religions(并查集,入门题)的更多相关文章
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- POJ 2524 Ubiquitous Religions (幷查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23090 Accepted: ...
- poj 2524 Ubiquitous Religions (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- POJ 2524 Ubiquitous Religions
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 20668 Accepted: ...
随机推荐
- HEOI 2016 游记
闲来无事,把这玩意儿补上. OI生涯中第一次正经的考试.挂的很惨. Day -1 不小心把机油(雾)sm惹毛了. 好像没啥别的事儿. Day 0 说好了上午直接去机房,然而临时说让我们上完前两节课再去 ...
- 3.6---双栈排序(CC150)
答,课本上的方法比较好. public static Stack<Integer> sort(Stack<Integer> s) { Stack<Integer> ...
- django ATOMIC_REQUESTS
在数据库配置中,如果配置了此属性为True,如下: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.xxx', 'NAME': '', ...
- MFCC可视化
大多数文章和博客介绍都是MFCC的算法流程,物理意义,这里仅仅从数据分布可视化的角度,清晰 观察MFCC特征在空间中的分布情况,加深理解. MFCC处理流程: MFCC参数的提取包括以下几个步骤: 1 ...
- [20160707]Java中如何关闭Frame窗口
用户类继承自WindowAdapter,而窗口对象Frame作为用户类的数据成员. 1 import java.awt.*; import java.awt.event.*; public class ...
- js弹出提示信息,然后跳转到另一页面
<script language="javascript"> alert("您的用户名与密码已成功修改!"); document.locatio ...
- Java实验四和实验五
实验四 类的继承性和多态性 [开发语言及实现平台或实验环境] Windows2000 或XP,JDK1.6与Jcreator4.0 [实验目的] 1. 掌握OOP方式进行程序设计的方法, 2. 了 ...
- ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)
题意:大致就是要求画出这个有规律的Fractal图形了= = 例如 1 对应 X 2 对应 X X X X X 这个题是个理解分治法很典型的例子(详情请参见Code) 分治法:不断缩小规 ...
- HDU 4865 Peter's Hobby(概率、dp、log)
给出2个影响矩阵,一个是当天天气对湿度的影响,一个是前一天天气对当天天气的影响. 即在晴天(阴天.雨天)发生Dry(Dryish.Damp.Soggy)的概率,以及前一天晴天(阴天.雨天)而今天发生晴 ...
- 修改EsayUi 中 tree 的原有样式,变为according 之类的样式 ,且子菜单显示在右侧
easyUi 中 tree 框架的属性有: 修改原有展开样式代码如下: onExpand:function(node,param){ $(this).children("li"). ...