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: ...
随机推荐
- Linux ffmpeg命令的介绍与使用
ffmpeg使用语法 ffmpeg [[options][`-i' input_file]]... {[options] output_file}... 如果没有输入文件,那么视音频捕捉(只在Linu ...
- 怎样在myEclipse中使用debug调试程序?
怎样在myEclipse中使用debug调试程序? 最基本的操作是: 1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类 ...
- 30 GroupSock(Port)——live555源码阅读(四)网络
30 GroupSock(Port)——live555源码阅读(四)网络 30 GroupSock(Port)——live555源码阅读(四)网络 简介 Port类的定义 Port的构造与全局的 &l ...
- 有向图寻找(一个)奇环 -- find an oddcycle in directed graph
/// the original blog is http://www.cnblogs.com/tmzbot/p/5579020.html , automatic crawling without l ...
- HTK搭建语音拨号系统实验材料下载
选自:http://maotong.blog.hexun.com/6267266_d.html 压缩包包括全部的配置文件,脚本文件,必备的模型文件和实验手册. 全部实验材料的下载链接: 1 http: ...
- string转DateTime(时间格式转换)
1.不知道为什么时间在数据库用varchar(8)来保存,例如"19900505",但是这样的保存格式在处理时间的时候是非常不方便的. 但是转换不能用Convert.ToDateT ...
- css 图形,非常完美
http://www.360doc.com/content/12/0327/13/8674_198243134.shtml
- ODATA 云驱动 http://www.cdata.com/cloud/
ODATA 云驱动 http://www.cdata.com/cloud/ 目前支持:ORACLE.MS SQL . MYSQL. -------------- rssbus ht ...
- jdbc事务处理和连接池
JDBC: * JDBC概念:Java DataBase Connectivity(Java数据库连接) SUN公司提供的一组连接数据库API. * JDBC开发步骤: * 1.注册驱动. * 2.获 ...
- Centos7 修改SSH 端口
修改/etc/ssh/sshd_config vi /etc/ssh/sshd_config #Port 22 //这行去掉#号,防止配置不好以后不能远程登录,还得去机房修改,等修改以后的端口能使用以 ...