poj 2524 Ubiquitous Religions 一简单并查集
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 22389 | Accepted: 11031 |
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
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
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

#include<algorithm>
#include<iostream>
#include<cstring>
#include<set>
#include<cstdio>
using namespace std;
int father[];
int vis[];//用来标记出现过没有;
void begin(int m)
{
for(int i=;i<=m;i++)
{father[i]=i;vis[i]=;} }
int find(int x)
{
if(father[x]!=x)
{
father[x]=find(father[x]);
}
return father[x];
}
int main()
{
int m,n,x,y,a,b,mm,ans,k=;
while(scanf("%d %d",&m,&n) && m+n)
{ ans=;mm=; //mm 统计的是出现的个数,减去就是没出现的个数;
begin(m);
for(int i=;i<=n;i++)
{
scanf("%d %d",&x,&y);//输入尽量用scanf(344ms),cin(4688ms)差别略大啊;
if(x<=m && y<=m)
{
a=find(x);b=find(y);//各找各的父亲;
father[a]=b;
if(vis[x]==) //标记统计出现过多少个数;
{mm++;vis[x]=;}
if(vis[y]==)
{mm++;vis[y]=;}
}
}
for(int i=;i<=m;i++) //统计共有几个集合,实在不好想,用了个笨方法;
{
if(father[i]==i && vis[i]==)//父亲的父亲,是他本身,并且他已经出现过了
ans++;
}
printf("Case %d: %d\n",k++,ans+m-mm);
}
return ;
}
poj 2524 Ubiquitous Religions 一简单并查集的更多相关文章
- POJ 2524 独一无二的宗教(裸并查集)
题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- [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: 23997 Accepted: ...
- 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: 23090 Accepted: ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- 【原创】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: ...
随机推荐
- poi 技术动态更新 Excel模板内容,动态更新内容
1.控制器方法 private URL base = this.getClass().getResource(""); /** * 流拍之后,可以下载询价单 * * @param ...
- Python学习(一)安装、环境配置及IDE推荐
Python的安装.环境配置及IDE推荐 官网:https://www.python.org/ 版本:2.x 和 3.x 差别较大:python3是不向下兼容:版本区别可参考网官网介绍 至于选择 Py ...
- nose的测试报告
有时候我们要让报告整洁美观点,以html展示测试结果,我们可以借助pip install nosehtmloutput插件输出html格式报告 from nose.plugins.plugintest ...
- CSS: CSS常用的文本样式属性
介绍:CSS常用的文本样式属性 color: 颜色 font-size: 字体大小 font-style (normal.italic.oblique): 字体样式(正常.斜体) font-wei ...
- iOS:多个单元格的删除(方法二):
前面介绍了万无一失的方法一,这里介绍删除单元格的第二种方式,通过删除单元格中的内容的方式进行操作:(但是这种情况有一个小的弊端,由于单元格重用机制,如果单元格内容一样时,标记的存在会造成误删) 删除前 ...
- Java实现图片的裁剪
需求: 有一张位置大小的图片,现在需要根据这张原图得到指定尺寸的图片,且得到的图片要符合原先图片的比例,就是在原图的基础上等比例缩放得到图片后,在进行剪裁,这样保证得到的图片是原图的一部分,而不是将原 ...
- java学习笔记10--泛型总结
java学习笔记系列: java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Object类 java学习笔记5-- ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- rtsp摘要认证协议(Response计算方法)
rtsp摘要认证协议(Response计算方法) 说明: 例如:OPTIONS rtsp://192.168.123.158:554/11RTSP/1.0 RTSP客户端应该使用username ...
- 安全的远程登录(SSH)
Secure Shell(缩写为SSH),由IETF的网络工作小组(Network Working Group)所制定:SSH为一项创建在应用层和传输层基础上的安全协议,为计算机上的Shell(壳层) ...