畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24266    Accepted Submission(s): 12597

Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 

 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 

注意:两个城市之间可以有多条道路相通,也就是说

3 3

1 2

1 2

2 1

这种输入也是合法的

当N为0时,输入结束,该用例不被处理。 

 
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。 

 
Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
 
Sample Output
1
0
2
998

Hint

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)的更多相关文章

  1. 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 ...

  2. TMemo.Text 回车键会变成#$D#$A,而非#13#10

    mmoComplain: TMemo;//cxmComplain.Text 会造成回车键 转换成十六进制的字符串 #$D#$A,而非#13#10 //cxmComplain.Text范例:'风发的是' ...

  3. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  4. 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 ...

  5. ubuntu 13.10 monodevelop3 安装

    版本 ubuntu 13.10 桌面模式默认:unity :文件管理器:nautilus

  6. ubuntu 13.10 svn工具 rabbitvcs 安装

    ubuntu 版本:13.10:桌面模式默认:unity :文件管理器:nautilus

  7. 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 ...

  8. ubuntu 13.10 skype登不上问题

    首先打开sources.list sudo gedit /etc/apt/sources.list 如果是13.10添加源: deb http://archive.canonical.com/ubun ...

  9. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

随机推荐

  1. Good Bye 2015 F - New Year and Cleaning

    F - New Year and Cleaning 这题简直是丧心病狂折磨王.. 思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列. 为了优化找到下一个消去的点,我先 ...

  2. php中max_input_vars默认值为1000导致多表单提交失败

    公司内一个php的后台管理系统,之前运行在apache上,后来我给转到nginx+php上后,其他功能运行正常,有一个修改功能提交表单后没有提交成功,查了代码没查出来什么问题,后来看了下php err ...

  3. Python并发编程-队列

    队列 IPC = Inter-Process Communication 队列 先进先出 队列的几种方法 #put() #full() #get() #empty() #get-nowait() fr ...

  4. 实验 Unity Linear Color Space 发现结果不符合预期

    美术前上个礼拜找我问光照图总是烘焙过暗的问题,一时兴起我在 Gamma 和 Linear 两个颜色空间切换了下,发现一个 Shader 明暗不同,另一个 毫无变化,于是激发了我去研究下在 Unity ...

  5. Xamarin.Forms教程开发的Xcode的下载安装

    Xamarin.Forms教程开发的Xcode的下载安装 Xamarin.Forms教程开发的Xcode的下载安装,Xcode是开发iOS应用程序的图形化开发工具.本节将讲解Xamarin.Forms ...

  6. python邮件

    解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...

  7. Number lengths FZU - 1050

    N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead ...

  8. [BZOJ4373]算术天才⑨与等差数列(线段树)

    [l,r]中所有数排序后能构成公差为k的等差数列,当且仅当: 1.区间中最大数-最小数=k*(r-l) 2.k能整除区间中任意两个相邻数之差,即k | gcd(a[l+1]-a[l],a[l+2]-a ...

  9. Android UI设计规范之常用单位

    px :全称--pixel .像素.例如,480*800的屏幕在横向有320个象素,在纵向有480个象素. 屏幕的分辨率 : 屏幕的长宽方向上像素点的数量. dp(dip) : 全称--Density ...

  10. JDK源码(1.7) -- java.util.Queue<E>

    java.util.Queue<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...