poj2524
说来惭愧啊。。现在才会并查集。我竟然给我妈妈讲明白并查集怎么回事了- -
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #define maxx 50010 int set[maxx]; int find(int x)
{
return x == set[x] ? x : (set[x] = find(set[x]));
} int main()
{
int n, m;
int kase = 0;
while (cin >> n >> m && (n || m))
{
for (int i = 1; i <= n; i++)
{
set[i] = i;
}
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b); int c = find(a), d = find(b);
if (c != d){ set[c] = d; } }
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (set[i] == i) cnt++;
}
cout << "Case " << ++kase << ": " << cnt << endl;
}
}
poj2524的更多相关文章
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...
- [原]poj-2524(裸并查集)
题目链接: http://poj.org/problem?id=2524 题意: n个人,m对人宗教相同,输出一共有多少个不同的宗教. 代码如下: #include<iostream> # ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524+并查集
题意简单. 询问n个人的宗教关系. #include<stdio.h> ; int fa[ maxn ]; int vis[ maxn ]; void init( int n ){ ;i& ...
- POJ2524——Ubiquitous Religions
Ubiquitous Religions Description There are so many different religions in the world today that it is ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- POJ2524:Ubiquitous Religions (并查集模板)
Description There are so many different religions in the world today that it is difficult to keep tr ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
随机推荐
- 《Node.js In Action》笔记之流程控制
转向Javascript后,开始学习了node.js,选择的第一本书是<Node.js in Action> 将近一个月时间,断断续续看完,选几个点做下笔记 1.实现串行化流程控制 var ...
- 静默安装MSSQL
原文地址:http://www.cnblogs.com/lyhabc/p/3511788.html 介绍 假如你有50台服务器需要安装SQLSERVER,如果你用下一步下一步的方式,用远程桌面不停切换 ...
- Linux下hp打印机驱动hplip分析
Hplip分析 版本号是2.14,源代码位置:http://hplipopensource.com. 图的来源:http://hplipopensource.com/node/128. 实践中使用的打 ...
- hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)
题目链接 第一次写nlogn复杂度的LIS,纪念一下. 题目意思是说.有两条平行线.两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市.输入n.接下来有n行,p,r ...
- 努比亚 Z5 mini刷机包 omni4.4.2改动V4.0 自用版 精简 MIUI特效
ROM介绍: 第一版: 1.基于lwang适配的omni4.4.2第二版改动,少量精简改动 2.设置加入"自启项管理",体验更快.更顺滑 3.替换特效为XUI特效 4.改动host ...
- Java学习之路:详细解释Java解析XML四种方法
XML如今已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便. 对于XML本身的语法知识与技术细节,须要阅读相关的技术文献,这里面包含的内容有DO ...
- RedGate 工具SQLMultiScript1.1
原文:RedGate 工具SQLMultiScript1.1 RedGate 工具SQLMultiScript1.1 SQLMultiScript是一个脚本分发工具,当你写好了一个SQL脚本之后,你需 ...
- Ubuntu 13.10 安装 Unity 8 试用截图
关于 Unity 8 有很多报道,Unity 8 相对于 Unity 7 变化相当大,这是 Ubuntu 团队打造的一款通用于 PC.手机.平板的桌面. Unity 8 已经登陆 ubuntu 13. ...
- rsync 只是测试,请看下一篇
实现从客户服务器去同步资源服务器 1.解压 # tar -xzpvf rsync-2.5.6.tar.gz 编译安装 # cd rsync-2.5.6/ # ./configure --pref ...
- 採訪The Molasses Flood:BioShock Infinite 游戏之后又一大作
Xsolla有幸与Flame in the Flood游戏的开发人员之中的一个-----Forrest Dowling进行了採訪,Flame in the Flood这款非常棒的游戏在Kickstar ...