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< ...
随机推荐
- bzoj 1116
思路:每个连通块都判是否有环. #include<bits/stdc++.h> #define LL long long #define fi first #define se secon ...
- AOSP编译
AOSP编译 1 编译环境Win10系统安装VMplayer14,主机16G内存,I3-4170 ,虚拟机ubuntu-16.04.1-desktop-amd64,12G内存. 2 软件安装sudo ...
- 哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)I - B-旅行
题目描述 小z放假了,准备到RRR城市旅行,其中这个城市有N个旅游景点.小z时间有限,只能在三个旅行景点进行游玩.小明租了辆车,司机很善良,说咱不计路程,只要你一次性缴费足够,我就带你走遍RRR城. ...
- python 计算md5
import hashlib src = "afnjanflkas" m2 = hashlib.md5() m2.update(src) print m2.hexdigest() ...
- java抽象类、多态、接口
抽象类 抽象类的产生 当编写一个类时,我们往往会为该类定义一些方法,这些方法是用来描述该类的功能具体实现方式,那么这些方法都有具体的方法体. 但是有的时候,某个父类只是知道子类应该包含怎么样的方法,但 ...
- Shiro切入Spring的方式
在springMVC中要使用shiro,一般都遵循下面的配置: applicationContext-shiro.xml <bean id="shiroFilter" cla ...
- vue数据立刻绑定到dom元素
今天遇到个令人发指的问题 使用vue修改一个数据之后,它不会立刻修改,而是在当前函数执行完毕之后才会进行渲染. 有个很简单的例子 <body> <div id="app&q ...
- 关于maven工程的几个BUG
换了个新的环境,重新导入的maven工程出现了2个BUG: 1.Could not calculate build plan: Plugin org.apache.maven.plugins:mave ...
- 【Python】单例模式Singleton
前两天一个面试被问到python中单例模式有几种实现方式,只答出了可以用元类实现...然后就想不起来了. 之后翻书,原来这些之前都见过的啊.... 1.手动实现真正创建实例的方法__new__()来实 ...
- 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 919 Solved: 664 Description 给出 ...