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: ...
随机推荐
- LaTeX排版设置图表的位置 Positioning images and tables
Positioning images and tables LATEX is an editing tool that takes care of the format so you only hav ...
- 数字锁相环Octave仿真
clc; clear all; % 仿真数据长度 SimLens = 1000; % 载波信号 Fs = 2400; Ts = 1 / Fs; Fsig = 60; % 随机初相 Delta_Phas ...
- [Todo] Nodejs学习及Spider实验(包括php入门学习、React入门学习)
/Users/baidu/Documents/Data/Interview/Web-Server开发 深入浅出Node.js-f46c http://blog.csdn.net/u012273376/ ...
- 如何对exec sp_who2的结果进行选择和排序?
从网上找到了下面的两个脚本, 笔者试用过, 很好. 故记在这里. 方法一, 用临时表 CREATE TABLE #sp_who2 ( SPID INT, Status VARCHAR(255), ...
- js执行eval()抛出异常SyntaxError
try{ eval("("+data+")"); }catch(err) { location.href = window.location.href; }
- 数字证书相关技术 : Versign信任签章
资料网址: 淘宝网站可信服务 http://www.ert7.com/case/eb/1391.html Versign信任签章 http://www.ert7.com/verisign/ssl/29 ...
- Cocos2d-X中的动作特效
Cocos2d-X中提供了很丰富的动作特效 比如:网格动画 扭曲特效 3D瓷砖波动特效 程序代码: #include "ActionEffect.h" #include " ...
- 鼠标辅助点击器(MouseClickAidHelper)
鼠标辅助点击器(MouseClickAidHelper) 下载地址:http://www.endv.cn/product/view28.html 由天云信息开发,并已开源,功能无限制,软件解决了重复操 ...
- [Compose] 17. List comprehensions with Applicative Functors
We annihilate the need for the ol' nested for loop using Applicatives. For example we have this kind ...
- 删除android ScrollView边界阴影方法
XML文件中添加以下方法: android:fadingEdge=”none” 或者,代码中设置为false即可 ScrollView.setHorizontalFadingEdgeEna ...