当前位置:网站首页>HDU1232 畅通工程(并查集)
HDU1232 畅通工程(并查集)
2022-07-05 07:15:00 【Woodenman杜】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1232
Question
Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
Solve
很明显可以看出来这就是一个最小生成树的变形,变得简单了^_^
可以先看一下这篇文章:最小生成树的两种算法
其中的Kruskal算法是比较常用的,不过这道题没那么麻烦,单纯并查集就行~
我们可以知道要打通 n 个节点,至少要 n-1 条边,所以定义res = n - 1
根据这一条件,再利用并查集判断每一条读入的边是否已经连接:
如果原来已经连接了,就不需要做任何事情;
如果没有连接,那读入该边之后就连接了,所需建设的道路数也会减一(res--)
读边结束后得到的 res 就是结果了
AC Code
#include <iostream>
#include <cstring>
#define N 1000
using namespace std;
int fa[N+1], n, m;
//查找根节点
int find(int x)
{
if(fa[x] == x)
return x;
else
return fa[x] = find(fa[x]);
}
int main(void)
{
while(cin >>n && n != 0)
{
cin >>m;
//初始化并查集
for(int i = 1; i <= n; i++) fa[i] = i;
int res = n - 1;
for(int i = 1; i <= m; i++){
int x, y; cin >>x >>y;
int dx = find(x); int dy = find(y);
//已经连接 直接跳过,未连接 连接并记录
if(dx == dy) continue;
fa[dx] = dy;
res--;
}
cout <<res <<endl;
}
return 0;
}
边栏推荐
- npm install -g/--save/--save-dev的区别
- Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
- Docker installs MySQL and uses Navicat to connect
- Build a microservice cluster environment locally and learn to deploy automatically
- PHY drive commissioning - phy controller drive (II)
- Ros2 - function package (VI)
- ROS2——node节点(七)
- About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
- GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
- Unity 之 ExecuteAlways正在取代ExecuteInEditMode
猜你喜欢
PHY drive commissioning - phy controller drive (II)
ROS2——初识ROS2(一)
Spinningup drawing curve
Ros2 topic (VIII)
ROS2——topic话题(八)
Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
并发编程 — 死锁排查及处理
Three body goal management notes
【软件测试】04 -- 软件测试与软件开发
【无标题】
随机推荐
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
[software testing] 02 -- software defect management
Binary search (half search)
数学分析_笔记_第8章:重积分
Initialization of global and static variables
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
IPage can display data normally, but total is always equal to 0
The SQL implementation has multiple records with the same ID, and the latest one is taken
The difference between new and malloc
About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
Typescript get timestamp
[solved] there is something wrong with the image
Build a microservice cluster environment locally and learn to deploy automatically
Mipi interface, DVP interface and CSI interface of camera
2022.06.27_每日一题
The differences and connections among cookies, sessions, JWT, and tokens
In C language, int a= 'R'
docker安装mysql并使用navicat连接
SOC_SD_DATA_FSM