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: ...
随机推荐
- centos7 安装LNMP(php7)之mysql安装,更改密码,远程授权
1.执行命令 yum install mysql mysql-server mysql-devel -y 知道出现complete!则安装mysql完成 当执行 service mysqld rest ...
- docker 安装nginx并挂载配置文件和www目录以及日志目录
---恢复内容开始--- 一 首先 docker pull nginx 二 docker run --name myNginx -d -p 80:80 -v e:/docker/nginx/www:/ ...
- 由jtable浅谈vector<vector<Object>>的用法(转自a718515028的专栏)
以前只用过vector<Object> ,但是在做从数据库导出数据放到jtable中时,发现还有个vector<vector<Object>>的用法. 先说jta ...
- python xml与字典的相互转换
def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...
- SWIG 多语言接口变换 【转】
一. SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...
- 大量数据更新导致fgc频繁引起jvm服务暂停。
线上跑的几台server突然出现大量fgc,因为在fgc过程的stop the world太久.引起其他应用訪问该server上的接口大量超时.(发生超时的时间点和fgc时间点一致) 先进行初步的优化 ...
- zabbix自动化监控之自动注册
自动注册与自动发现刚好相反,是zabbix agent主动联系zabbix server,最后由zabbix server将这些agent加到host里.活动的Zabbix agent可以自动注册到服 ...
- jQuery异步框架探究2:jQuery.Deferred方法
(本文针对jQuery1.6.1版本号)关于Deferred函数的描写叙述中有一个词是fledged,意为"羽翼丰满的",说明jQuery.Deferred函数应用应该更成熟. 这 ...
- java中获取文件路径的几种方式
http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...
- ASP.NET CORE RAZOR :初始化数据库
官方说法是:设定数据库种子https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/sql 应用背景:初次部署系统时,设定一 ...