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: ...
随机推荐
- Colorful tree
cnbb 我被数组清零卡了一天.. 子树改色询问子树颜色数.. 先考虑颜色为x的节点对祖先答案的贡献,那么我们考虑把所有这些节点都搞出来,按dfs序排序,然后考虑每个节点a掌管的祖先是它和按dfs序的 ...
- PE556
考虑推广sum(i in Z){mu^2(i)}的做法. #include"roundCount.cpp" #include<cstdio> #include<v ...
- poj 1572
一道字符串替换的题目. 题意:给你2*n组字符串,一个是规则,一个是替换的结果. 字符串的题目,确实麻烦,有些细节不处理好就是wa. 这里我提供1组数据 intput 1 abcdef a abcde ...
- ACM/ICPC 之 暴力打表(求解欧拉回路)-编码(POJ1780)
///找到一个数字序列包含所有n位数(连续)一次且仅一次 ///暴力打表 ///Time:141Ms Memory:2260K #include<iostream> #include< ...
- 初识 MySQL 5.6 新功能、参数
摘要: 继上一篇的文章 初识 MySQL 5.5 新功能.参数 之后,现在MySQL5.6 针对 MySQL5.5 各个方面又提升了很多,特别在性能和一些新参数上面,现在看看大致提升了哪些方面(后续不 ...
- VB中字符串操作函数
Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数. Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltrim(string) ...
- Navicat连接oracle,出现Only compatible with oci version 8.1 and&nb (转)
与本地oracle连接的时候,一般没问题,sqlplus和oci都是本地oracle自带的,(设置: 工具->选项->oci) 分别为: oci:D:\app\pcman\produc ...
- jquery.base64.js简单使用
jquery.base64.js, 加密,使用,先引入jquery,然后引入jquery.base64.js 使用如下 js中加密如下 $.base64.encode(result[i].ipadre ...
- 【编程题目】一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),取出其中一段,要求包含所有 N 中颜色,并使长度最短。
40.百度研发笔试题 2)一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有 N 中颜色,并使长度最短.并分析时间复杂度与空间复杂度. 思路: ...
- 20145213《Java程序设计学习笔记》第六周学习总结
20145213<Java程序设计学习笔记>第六周学习总结 说在前面的话 上篇博客中娄老师指出我因为数据结构基础薄弱,才导致对第九章内容浅尝遏止地认知.在这里我还要自我批评一下,其实我事后 ...