Ubiquitous Religions(并查集)
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
Output
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=;
int pre[maxn];
int find(int v)
{
return pre[v]==v?v:find(pre[v]);
cout<<pre[v]<<endl;
}
void join(int a,int b)
{
int f1=find(a),f2=find(b);
if(f1!=f2)
pre[f1]=f2;
}
int main()
{
int n,m;
int count;
for(count=;; count++)
{
cin>>n>>m;
if(n==&&m==)
break;
for(int i=; i<=n; i++)
pre[i]=i;
int max=n;
for(int i=; i<=m; i++)
{
int a,b;
cin>>a>>b;
if(find(a)!=find(b))
max=max-;
join(a,b);
}
printf("Case %d: ",count);
printf("%d\n",max);
}
return ;
}
Ubiquitous Religions(并查集)的更多相关文章
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- 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 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 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(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
随机推荐
- OfficePickers
OfficePickers http://www.codeproject.com/Articles/12327/Office-2003-Color-Picker 来自为知笔记(Wiz) 附件列 ...
- [RxJS] Filtering operator: single, race
Single, race both get only one emit value from the stream. Single(fn): const source = Rx.Observable. ...
- libpcap使用
libpcap是一个网络数据包捕获函数库,功能非常强大,Linux下著名的tcpdump就是以它为基础的.今天我们利用它来完成一个我们自己的网络嗅探器(sniffer) 首先先介绍一下本次实验的环境: ...
- [转] 一个资深iOS开发者对于React Native的看法
当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...
- 移动端 设置 小于12px 字体 初探
1.移动端字号规范 2. 百度字号调研 3. 绕过12px 限制 4. 缩放 5. chrome 字号
- Oracle修改被占用的临时表结构
这两天在修改临时表的类型时,提示”attempt to create,alter or drop an index on temporary table already in use“的错误,由于临时 ...
- JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
//把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_par ...
- Java-Android 之短信发送
file:///F:/workspace3/Android_ver2.5/src/cn/szy/com/MainActivity.java package cn.szy.com; import jav ...
- Building Local Unit Tests
If your unit test has no dependencies or only has simple dependencies on Android, you should run you ...
- mysql 远程访问 配置
sudo vi /etc/mysql/my.cnf 找到bind-address = 127.0.0.1 注释掉这行:#bind-address = 127.0.0.1 或者改为: bind-addr ...