【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: ...
随机推荐
- C# 委托、事件,lamda表达式
参考文章 1. 委托Delegate C#中的Delegate对应于C中的指针,但是又有所不同C中的指针既可以指向方法,又可以指向变量,并且可以进行类型转换, C中的指针实际上就是内存地址变量,他是可 ...
- SpringBoot 框架整合webservice
spring boot集成web service框架 题记: 本篇博客讲的spring boot如何集成 spring web service,如果您想用Apache CXF集成,那么可能不适合您.为 ...
- 1019(C++)
计算n个数的最小公倍数,可用欧几里得算法计算两个数字的最大公约数,再计算两个数最小公倍数 有了2个数最小公倍数算法就简单了,即为:计算第一和第二个数得到最小公倍数lc,再计算lc和第三个数最小公倍数. ...
- Hive JDBC——深入浅出学Hive
第一部分:搭建Hive JDBC开发环境 搭建:Steps •新建工程hiveTest •导入Hive依赖的包 •Hive 命令行启动Thrift服务 •hive --service hiveser ...
- Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle
ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle DatabaseName:PetShop(宠物商店) Model:宠物商店网 ...
- Nginx auto_index和auth_basic
Nginx auto_index和auth_basic 1.nginx auto_index nginx站点目录浏览功能,默认情况下为关闭 启用或禁用目录列表输出 开启这个功能的前提是站点目录下没有首 ...
- Oracle基础 索引
一.索引 索引是一种快速访问数据的途径,可提高数据库性能.索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需的数据,就像书的目录,可以快速查找所需的信息,无须阅读整本书. (一)索引的分类 逻 ...
- Python3的bytes/str之别
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str ...
- vue-router 动态路由匹配
export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld, }, { / ...
- STL学习笔记(第二章 C++及其标准程序库简介)
本章重点是介绍与C++标准程序库相关的几个最重要的语言新特性 template(模板) 程序库中几乎所有东西都被设计成template形式.所谓templates,是针对“一个或多个尚未明确的型别”所 ...