HDU 1232 (13.10.31)
畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24266 Accepted Submission(s): 12597
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
0
2
998
Hint
Huge input, scanf is recommended.
题意: 闲来无事就重新温故并查集, 题目意思是给出N个城镇, 输入M条已存在的路, 除此之外还要修多少条路才能使所有的城镇可以互达?(不直接通路, 间接通也行)
做法: 找连通分支有多少个, 然后连通分支数减一, 就是至少要修的路, 原理想想就知道了...
AC代码:
#include<stdio.h> int n, m;
int parent[1005]; void init() {
for(int i = 1; i <= n; i++)
parent[i] = i;
} int find(int k) {
if(parent[k] == k)
return k;
else
return find(parent[k]);
} void unit(int x, int y) {
int t1 = find(x);
int t2 = find(y);
if(t1 != t2)
parent[t1] = t2;
} int main() {
while(scanf("%d", &n), n != 0) {
scanf("%d", &m);
init();
int a, b;
int i;
for(i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
unit(a, b);
}
int road = 0;
for(i = 1; i <= n; i++) {
if(parent[i] == i)
road++;
}
printf("%d\n", road-1);
}
return 0;
}
HDU 1232 (13.10.31)的更多相关文章
- HDU 3910 (13.10.31)
Description Maybe you know “San Guo Sha”, but I guess you didn’t hear the game: “Liang Guo Sha”! Let ...
- TMemo.Text 回车键会变成#$D#$A,而非#13#10
mmoComplain: TMemo;//cxmComplain.Text 会造成回车键 转换成十六进制的字符串 #$D#$A,而非#13#10 //cxmComplain.Text范例:'风发的是' ...
- 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton
[源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...
- ubuntu 13.10 mono asp.net服务 安装
ubuntu 13.10 从官方文档http://www.mono-project.com/Mod_mono 可看到 Mod_Mono is an Apache 2.0/2.2/2.4.3 modul ...
- ubuntu 13.10 monodevelop3 安装
版本 ubuntu 13.10 桌面模式默认:unity :文件管理器:nautilus
- ubuntu 13.10 svn工具 rabbitvcs 安装
ubuntu 版本:13.10:桌面模式默认:unity :文件管理器:nautilus
- Fix catalyst driver in Ubuntu 13.04 / 13.10
Fix catalyst driver in Ubuntu 13.04 / 13.10(墙外文章备份) 1. Introduction I found lots of people strugglin ...
- ubuntu 13.10 skype登不上问题
首先打开sources.list sudo gedit /etc/apt/sources.list 如果是13.10添加源: deb http://archive.canonical.com/ubun ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
随机推荐
- 【LOJ】#2012. 「SCOI2016」背单词
题解 我们发现第一种操作肯定不可取,每个节点里它最近的点是它最长出现过的后缀,发现这就是AC自动机的fail节点,根据fail的关系这会是一棵树,而一个单词的前一个序号最大的后缀必定是它的父亲 然后我 ...
- QString 与中文问题
原文请看:http://www.cnblogs.com/phoenixlaozhu/articles/2553180.html (更新:本文的姊妹篇Qt5与中文问题) 首先呢,声明一下,QString ...
- 洛谷P2296 寻找道路 [拓扑排序,最短路]
题目传送门 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- Codeforces Round 548 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- java短信接口调用
java短信接口调用 之前一直在一个传统的单位上班好多听容易的技术都没接触过,即使有时候想搞一搞类似于支付宝支付,短信接口调用,微信公众号,小程序之类等功能,一直有心无力终于跳槽了,估计是氛围的原因吧 ...
- export导出.xls时,在火狐的情况下出现表名乱码的情况的解决方案
response.setContentType("application/octet-stream; charset=utf-8");fileName = exportDataDt ...
- ARP监测工具Arpwatch
ARP监测工具Arpwatch ARP协议是网络的基础协议.基于ARP协议的ARP攻击是局域网最为常见和有效的攻击方式.ARP攻击可以通过发送伪造的ARP包实施欺骗,实现各种中间人攻击.Arpwa ...
- 【BZOJ 2656】2656: [Zjoi2012]数列(sequence) (高精度)
2656: [Zjoi2012]数列(sequence) Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 1499 Solved: 786 Descri ...
- 实战-130W表增加字段耗时
工作需要对130W的表增加字段,因为是操作线上数据库,所以提前在本地调查下耗时. 首先建表: CREATE TABLE `alter_cloumn_test` ( `id` int(11) unsig ...
- 函数中的 arguments 对象
JavaScript函数具有像数组一样的对象,这些对象称为arguments,与传递给函数的参数相对应.传递给JavaScript函数的所有参数都可以使用arguments对象来引用. 现在我们开始学 ...