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< ...
随机推荐
- Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...
- Codeforces Round #492 (Div. 2) [Thanks, uDebug!]
这次的题好奇怪哦... C - Tesla 思路:先把跟停车位相邻的车停进去,然后开始转圈... #include<bits/stdc++.h> #define LL long long ...
- bzoj 1176 CDQ分治
思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成4的这样的询问,然后 进行CDQ 分治,回溯的时候按x轴排序,然后用树状数组维护y的值. #include<bi ...
- Linux与其它类Unix内核的比较
单块结构的内核:由几个逻辑上独立的成分构成,单块结构,大多数据商用Unix变体也是单块结构: 编译并静态连接的传统Unix内核:Linux能自动按需动态地装载和卸载部分内核代码(模块),而传统Unix ...
- 最短路——spfa
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...
- Redis学习篇(十一)之发布订阅
PUBLISH/SUBSCRIBE 发布订阅的原理 包含两个角色,一个是发布者, 一个是订阅者 订阅者可以订阅一个或者多个频道(channel) 发布者可以向指定的频道发布信息 通过SUBSCRIBE ...
- 【BZOJ 3160】 3160: 万径人踪灭 (FFT)
3160: 万径人踪灭 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1440 Solved: 799 Description Input Outp ...
- apk安装 卸载 原理
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 复制 apk到 /data/app目录下,解压并扫描 安装包, 把 dex文件,保存到 ...
- hdu 3681 压缩dp+搜索
题意:一个机器人想越狱,他只能带一定电量的电池,'S'表示道路可行,'G'表示充电器, 只可充电一次,但是可以经过很多次.'F'表示起点,'Y'表示要破坏的机关,也是只能破坏一次,但是可以经过无数次. ...
- bzoj 1029 贪心
贪心的一种,维护一种尽可能优的状态(即不会比最优解差),将这种状态保持到最后. /*********************************************************** ...