【POJ-2524】Ubiquitous Religions(并查集)
并查集。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 55555;
int fa[maxn];
int vis[maxn];
int n,m,t;
void init(){
for(int i = 0; i < n; i++) {fa[i] = i;}
memset(vis,0,sizeof(vis));
}
int find_father(int u){
return fa[u] == u ? u : fa[u] = find_father(fa[u]);
}
int main(){
int Case = 1;
while(scanf("%d%d",&n,&m)){
if(!m && !n) break;
init();
for(int i = 0; i < m; i++){
int x,y;
scanf("%d%d",&x,&y);
int fx = find_father(x);
int fy = find_father(y);
fa[fx] = fy;
}
int cnt = 0;
for(int i = 1; i <= n; i++){
int t = find_father(i);
if(!vis[t]){
vis[t] = 1;
cnt ++;
}
}
printf("Case %d: %d\n",Case++,cnt);
}
return 0;
}
【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(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 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: ...
随机推荐
- ADOX创建ACCESS数据库列名的数据类型
Type 属性 指示 Parameter.Field 或 Property 对象的操作类型或数据类型. 设置和返回值 设置或返回下列 DataTypeE ...
- util.select.js
ylbtech-JavaScript-util: util.select.js 筛选工具 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...
- Commenting and uncommenting XML via Python
转载: http://stackoverflow.com/questions/8764017/commenting-and-uncommenting-xml-via-python from xml.d ...
- 不是书评 :《我是一只IT小小鸟》
本文转自刘未鹏 博客,写的非常的好 就转回来了 设计你自己的进度条 进度条的设计是一个很多人都知道的故事:同样的耗时,如果不给任何进度提示,只是在完成之后才弹出一个完成消息,中间没有任何动态变化,那么 ...
- Asp.net Mvc使用PagedList分页
git:https://github.com/troygoode/PagedList 1. Nuget 安装package watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- react-native 组件默认属性(defaultProps) 及 属性类型验证(PropTypes)
1.所有的属性类型 2.代码 import PropTypes from 'prop-types'; type Props = {}; export default class App extends ...
- 訪问控制 protected, public, private 对照
OOP 3大特性:数据抽象,继承,动态绑定 3中訪问标号 protected, public, private 对照 用类进行数据抽象:用继承类继承基类的成员,实现继承.通过将基类对应函数声明为vir ...
- 【Excle数据透视表】如何为一个字段添加多种分类汇总方式
解决方案1 右键单击人员分类字段包含的任意单元格→右键→字段设置→自定义→(最大值.最小值) 解决方案2 单击人员分类→分析→字段设置
- 定制一个类似地址选择器的view
代码地址如下:http://www.demodashi.com/demo/12832.html 前言: 这几天也是闲来无事,看看有什么和Scroller相关的控件需要巩固下,原因很简单,前几天看到相关 ...
- Linux常见服务器——DHCP服务器的搭建
一.基础知识: 1.DHCP简介: DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理.分配IP ...