poj 2524 Ubiquitous Religions(并查集)
|
Ubiquitous Religions
Description
There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.
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
The 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
For 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 Sample Output Case 1: 1 Hint
Huge input, scanf is recommended.
Source |
又一并查集水题 直接套用模板 不多做解释
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int father[50005];
int find(int x)//找与x相关联的人
{
while(x!=father[x])//找到人
x=father[x];
return x;
}
void getfind(int a,int b)
{
int roota=find(a);
int rootb=find(b);
if(roota!=rootb)当roota和rootb不在同一集合时将其合并
father[rootb]=roota;
}
int main()
{
int n,m,i,j,a,b;
int t=1;
while(cin>>n>>m,n&&m)
{ for(i=1;i<=n;i++)
father[i]=i;
for(i=0;i<m;i++)
{
cin>>a>>b;
getfind(a,b);
} int ans=0;
for(i=1;i<=n;i++)
//cout<<father[i]<<" "<<endl;
if(father[i]==i)
ans++;
cout<<"Case "<<t<<": "<<ans<<endl;
t++;
} }
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: 23997 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: ...
随机推荐
- 【spring boot】15.spring boot项目 采用Druid数据库连接池,并启用druid监控功能
在http://www.cnblogs.com/sxdcgaq8080/p/9039442.html的基础上,来看看spring boot项目中采用Druid连接池. GitHub地址:示例代码 == ...
- CSS3:transition过渡效果
之前的transform 可以实现转换,但是一下子就放大缩小视觉上不太好看,要想渐变该如何呢?可以使用transition transition主要包含四个属性值: transition: prope ...
- 解决413 Request Entity Too Large
修改nginx配置 这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好. 1.打开nginx主配置文件nginx.conf,一般在/u ...
- 设计模式之工厂模式之抽象工厂(php实现)
github地址: git@github.com:ZQCard/design_pattern.git 抽象工厂模式: 先来认识下什么是产品族: 位于不同产品等级结构中,功能相关联的产品组成的家族.图中 ...
- ES6方面重点摘要
1.变量声明(1)内层变量覆盖外层变量(即后面的覆盖前面的)(2)循环变量的声明,i值在全局范围内有效,所以最后输出的都是最后一轮i的值(3)let.const的引入,为JS增加了块级作用域的概念(c ...
- mybatis 3简介
http://www.mybatis.org/mybatis-3/zh/index.html
- 【Hadoop】MR 切片机制 & MR全流程
1.概念 2.Split机制 3.MR Shuffle过程 4.MR中REDUCE与MAP写作过程 5.MR全貌
- windows 10 超级优化提速 附系统服务列表纯净
如图,本机安装了vs2017 office2016 迅雷.谷歌浏览器,不建议安装其它任何软件.vs2017为开发软件,用于编程,一般用户用不到. 如果想安装其它的软件,建议优先使用绿色版本的. 下载服 ...
- Solr 配置文件之schema.xml
schema.xml这个配置文件的根本目的是为了通过配置告诉Solr怎样建立索引. solr的数据结构例如以下: document:一个文档.一条记录 field:域.属性 solr通过搜索某个或某些 ...
- 【菜鸟学注入】之MySQL报错注入详解
本文转自:http://bbs.blackbap.org/forum.php?mod=viewthread&tid=6483&highlight=mysql%2B报错注入 用SQL注入 ...