POJ 2524 Ubiquitous Religions (幷查集)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 23090 | Accepted: 11378 |
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
end of input is specified by a line in which n = m = 0.
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 <cstdio>
using namespace std;
int f[50005],sum;
int find (int x)
{
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
void make(int a,int b)
{
int f1=find(a);
int f2=find(b);
if(f1!=f2)
{
f[f2]=f1;
sum--; //宗教数减一。
}
}
int main()
{
int n,m,p=1,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
for(i=1;i<=n;i++)
f[i]=i;
sum=n;
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
make(a,b);
}
printf("Case %d: %d\n",p++,sum);
}
return 0;
}
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 (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 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: ...
随机推荐
- c#笔记2019-01-06
using System; using System.Collections.Generic; using System.Linq; using System.Text; /*2019-01-06C# ...
- 【管理】个人主义—>集体主义
导读:这个月作为学术部负责人,暴露了很多问题,个人的,集体的!我需要思考的,有很多.现在,我反思图书馆丢书这件事的处理方案:我虽然站在了管理层,却做着员工干的事儿.以一种个人主义.英雄主义去做事儿.却 ...
- POJ 1971-Parallelogram Counting,暴力1063ms!
Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...
- iOS学习笔记06-手势识别
一.UIGestureRecognizer简单介绍 我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸事件处理都需要实现3个touches方法,比较繁琐,实际上我们可以使用更加简单的触摸事 ...
- 【Luogu】P1896互不侵犯King(状压DP)
题目链接 真是可恶,被数据范围坑了一把.想要一遍AC的希望破灭了…… 以后大家在做状压DP的时候一定要开long long…… 设f[i][j][k]表示考虑前i行,总共放了j个King,第i行状态为 ...
- PC端有什么好用的倍速播放的软件?
倍速播放的目的是让自己可以以更快的速度去看视频, 加快获取信息的效率 potplay吧,和KMP是一个作者.加速播放快捷键是c,减速时x,z恢复正常.下载官网 Global Potplayer
- SpringBoot项目整合Druid进行统计监控
0.druid介绍,参考官网 1.在项目的POM文件中添加alibaba的druid依赖 <dependency> <groupId>com.alibaba</group ...
- linux的sar命令未找到
linux的sar命令未找到 一般的命令可以直接使用yum安装,但是sar和mpstat命令这两个命令都是在sysstat包里, 网上的解决方法:rpm -ivh gd-2.0.32-23.2.i58 ...
- input标签附带提示文字(bootstrap里面输入框的两侧同时添加额外元素)
一直不太喜欢用定位,今天写界面的时候,要在输入框右边添加默认的元素符号. 第一次尝试,因为本身项目用的是bootstrap所以就想利用输入框的两侧同时添加额外元素. 但是写了代码发现效果不同. 第二次 ...
- springboot jetty替换tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...