【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: ...
随机推荐
- Features (OCMock 2)
This page describes the features present in OCMock 2.x, using the traditional syntax. All these feat ...
- 关于http和https淘宝支付宝跨域解决方法研究
关于http和http跨域淘宝解决方式研究: http://buyer.trade.taobao.com/trade/pay.htm?spm=a1z01.2.3.4.0.wZAGp9&bizO ...
- 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- iOS小技巧 - 如何使UIView可以绑定点击事件
让我们这次直接进入正题,有时候我们想做以下这种界面: 目前我就想到三种方案: 做一个tableview,然后组织cell的界面如上图所示 做一个button子类,使得button的界面能如上图所示 做 ...
- notepad++ 在每一行最后加上逗号
1.全选缩进对齐 2.替换功能,入下全部替换 3.在入下替换 4.结果 完成!
- 系统重装 JUJUMAO VHD安装WIN7解析
1 把原有系统分区一个删除(使用第三方分区工具如PM或者PQ)并新建一个系统分区(也可以同时新建其他分区如D盘,E盘,但是一定要把C盘设置为NTFS格式并且活动的!) 2 将JUJUMAO_VHD ...
- Odoo12 重大改变
Table of Contents 重构的功能 ORM 数据导入 库存 库存规则 MRP 多步路线 新功能 IoT Odoo12 预计 2018/10 在 Odoo experience 20 ...
- 谁动了我的cpu——oprofile使用札记(转)
引言 cpu无端占用高?应用程序响应慢?苦于没有分析的工具? oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面 ...
- 读EXCEL
import xlrdbook=xlrd.open_workbook('app_student.xls')sheet=book.sheet_by_index(0)#根据(索引)顺序获取到sheet页# ...
- NSTimer注意内存泄露(真该死)
NSTimer可以用来执行一些定时任务,比较常用的方法就是: + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTar ...