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: ...
随机推荐
- Android 常驻与很驻型广播的差别,及ListView优化,Android新手基本知识巩固
1.常驻型广播 常驻型广播,当你的应用程序关闭了,假设有广播信息来,你写的广播接收器相同的能接受到. 他的注冊方式就是在你的应用程序中的AndroidManifast.xml进行注冊. 通常说这样 ...
- 【AS3 Coder】任务五:Flash 2D游戏的第二春(上)
在上一节中,我们基本上已经讲完了游戏中最主要的逻辑部分,不过为了更加全面地运用Starling中的一些特性,在本节中我们将一起来看看如何实现多面板切换以及粒子效果,这两个玩意儿可是比较频繁会出现于St ...
- Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(4)
Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(4) 一.安装CRS补丁 在安装CRS之前,须要安装补丁p6718715_10203_AIX64-5L,否则在安装时会出现 ...
- scp拷贝本地文件到服务器
拷贝远程服务器的文件到本地:scp -r -P 端口号 用户名@IP地址:/usr/local/tomcat_airc/webapps/ /tmp/kyj/ 拷贝本地文件到远程服务 ...
- devops流程
学习资源: https://www.youtube.com/watch?v=JBtWxj9l7zM&list=PLoYCgNOIyGAAzevEST2qm2Xbe3aeLFvLc&t= ...
- Linux SSH和SFTP服务分离
Linux SSH和SFTP服务分离 学习了:https://www.cnblogs.com/zihanxing/articles/5665383.html 都是监听22端口:
- 倍福TwinCAT(贝福Beckhoff)基础教程2.0 TwinCAT常用快捷键
F5:运行程序 CTRL+F5:停止运行当前程序 F12:登出 F11:登录 CTRL+F7:强制写入数值 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.you ...
- Win7安装了Visual Studio 2008没有快捷方式怎么办
在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE,就是这个devenv.exe了
- 使用LBS(地理位置)
一.使用之前的简单封装的OkHttpUtil http://www.cnblogs.com/itfenqing/p/6758909.html 二.关键代码: public class MainActi ...
- Linux组件封装(二)中条件变量Condition的封装
条件变量主要用于实现线程之间的协作关系. pthread_cond_t常用的操作有: int pthread_cond_init(pthread_cond_t *cond, pthread_conda ...