POJ2524 Ubiquitous Religions(并查集)
题目链接。
分析:
给定 n 个点和 m 条无项边,求连通分量的数量。用并查集很简单。
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <algorithm>
- #include <map>
- #include <queue>
- #include <cmath>
- using namespace std;
- const int maxn = + ;
- int p[maxn];
- int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
- int main(){
- int n, m, a, b, kase = ;
- while(scanf("%d%d", &n, &m) == ) {
- if(n == && m == ) break;
- for(int i=; i<=n; i++) p[i] = i;
- int cnt = ;
- for(int i=; i<m; i++) {
- scanf("%d %d", &a, &b);
- int x = find(a), y = find(b);
- if(x != y) {
- p[x] = y;
- }
- }
- for(int i=; i<=n; i++) {
- if(p[i] == i) cnt++;
- }
- printf("Case %d: ", ++kase);
- printf("%d\n", cnt);
- }
- return ;
- }
POJ2524 Ubiquitous Religions(并查集)的更多相关文章
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- [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 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524——Ubiquitous Religions
Ubiquitous Religions Description There are so many different religions in the world today that it is ...
- poj2524(简单并查集)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- 设计模式(二)The Observer Pattern 观察者模式
问题引入 生成一个公告板显示当时的天气状况,当天气状况发生改变的时候公告板能够实时的更新. 模式定义 定义对象之间的一对多的依赖.当一个对象改变状态时,它的全部依赖者都会自己主动收到通知并自己主动更新 ...
- ZOJ3329之经典概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- Flashback Query、Flashback Table(快速闪回查询、快速闪回表)
Flashback Query闪回查询 flashback query是基于undo表空间的闪回,与之相关的参数如下: SQL> show parameter undo NAME ...
- MYSQL 体系结构图-log (踏雪无痕) (UC技术博客)
http://www.cnblogs.com/chenpingzhao/category/690116.html http://www.cnblogs.com/chenpingzhao/p/51074 ...
- How To Cluster Rabbit-MQ--reference
Foreword This explanation of clustering Rabbit-MQ assumes that you’ve had some experience with Rabbi ...
- window7 远程连接 拒绝访问
windows7 远程连接 拒绝访问 ----------------------------- 找了很多网络文章,都没有解决问题. 然后突然: 用Administrator超级管理员修改了一下 想要 ...
- 学习《Spring 3.x 企业应用开发实战》Day-1
Day-1 记录自己学习spring的笔记 提要:根据<Spring 3.x 企业应用开发实战>开头一个用户登录的例子,按照上面敲的. 1.项目分层
- warning:1071 (42000) Specified key was too long;max key length is 1000 bytes
原因是mysql字段长度设置的太长了, 从而导致mysql在建立索引时,索引长度超过了mysql默认许可的长度 默认 Innodb 允许长度为 767 MyISAM 允许长度为 1000 官方说明 如 ...
- Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.
查看了进程, nginx, php-fpm都在运行, 排除程序错误, 那么就是配置的问题了. 一个可能的错误, 是由于配置中的 fastcgi_pass 配置错了 错误的配置如下 server { l ...
- Css四种样式
1. 2 3 4 5 6.