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: ...
随机推荐
- The Art of Mocking
One of the challenges developers face when writing unit tests is how to handle external dependencies ...
- 2017.6.8 spring-ldap基本使用总结
之前学习过spring-ldap的官方文档:2017.4.10 spring-ldap官方文档学习 现在是对实际使用的spring-ldap及使用过程中遇到的问题,进行总结. 1.spring-lda ...
- Mybaits的特点及优点
1.SQL语句和代码分离 便于统一管理和维护,不必再Java代码中调式SQL语句.但是当SQL语句出错时,控制台不会打印Log, 解决办法--引用log4j 2.用标签拼接SQL语句 用标签代替JAV ...
- Mysql性能测试诊断
mysql> showglobal status; 可以列出mysql服务器运行各种状态值,另外,查询mysql服务器配置信息语句: mysql> showvariables; 一.慢查询 ...
- Java 实例
Java 实例 本章节我们将为大家介绍 Java 常用的实例,通过实例学习我们可以更快的掌握 Java 的应用. Java 环境设置实例 Java 实例 – 如何编译一个Java 文件? Java 实 ...
- eclipse 内存配置
-server -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m -XX:+UseG1GC
- 【VBA】复制单元格数据有效性
单元格内设置了有效性,通过VBA怎么去复制呢?代码如下: Public Sub 复制单元格批注() Dim range1 As range Dim range2 As range '清除G列 Colu ...
- jQuery的$.fn使用
jquery中文网为您提供jQuery的$.fn使用等资源,欢迎您收藏本站,我们将为您提供最新的jQuery的$.fn使用资源 $.fn是指jquery的命名空间,加上fn上的方法及属性,会对jque ...
- firewalld实现网关功能
用ip a查看自己的路由服务器接口,一个外网接口 wan ,还有内网接口: 我这里是一块网卡,配了一个虚拟ip,eth0: wan口 eth0:1 lan口 注意:要注意顺序,先将接口加到zone ...
- mysql临时表空间暴涨
(此文刚好遇到转载记录) 一.内部临时表使用 在某些情况下,服务器在处理语句时创建内部临时表,而用户无法直接控制临时表何时发生,完全有MySQL内部自行决定. MySQL在以下几种情况会创建临时表: ...