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: ...
随机推荐
- 最新Connectify注冊码(序列号) Connectify3.7序列号 破解版
Connectify序列号.最新注冊码 今天给大家公布一个Connectify最新版的序列号(注冊码) 今天给大家公布一个Connectify最新版的序列号(注冊码) 经本人測试该注冊码为最新版Con ...
- GDB技巧整理
https://blog.atime.me/note/gdb-tricks.html 整理常用的gdb技巧. 常用命令 常用的gdb命令... 启动gdb 直接运行 gdb --args prog a ...
- The BINARY and VARBINARY Types
mysql> CREATE TABLE t (c BINARY()); Query OK, rows affected (0.21 sec) mysql> INSERT INTO t SE ...
- 虚拟化技术性能总结:Zones, KVM, Xen
[译]虚拟化技术性能总结:Zones, KVM, Xen 时间 2014-04-29 16:52:44 Babyfacer_陈晓炜 原文 http://blog.csdn.net/babyface ...
- Xcode常见报错及解决办法
报错一: 在iOS7的真机运行时,弹出错误:App installation failed. There was an internal API error. 如图 解决办法: 在Xcode -> ...
- 设置div中文字超出时自动换行
一.对于div强制换行1.(IE浏览器)white-space:normal; word-break:break-all;这里前者是遵循标准.#wrap{white-space:normal; wid ...
- codevs 2822爱在心中
不想吐槽题目.... /* K bulabula 算法(好像用哪个T bulabula更简单 然而我并不会 - -) 丑陋的处理cnt: Printf时 cnt中 ans[i][0]==1 的删掉 然 ...
- CSS Clip属性
Clip属性在W3C官网是这样进行描述的:“通过对元素进行剪切来控制元素的可显示区域,默认情况下,元素是不进行任何剪切的,但是也有可能剪切区域也显示的设置了clip属性”. .selector { c ...
- nodejs开发环境sublime配置
前端时间使用webstorm搭建一个node.js的学习环境,感觉非常强大.不过由于其加载的速度,每次让都让我抓狂.后来我找到了一个sublime.虽说3.0以上是收费的,2.0暂时免费.官方的不对s ...
- 那些年,我们一起学WCF--(8)Single实例行为
Single实例行为,类似于单件设计模式,所有可以客户端共享一个服务实例,这个服务实例是一个全局变量,该实例第一次被调用的时候初始化,到服务器关闭的时候停止. 设置服务为Single实例行为,只要设置 ...