当前位置:网站首页>Hdu1232 unimpeded project (and collection)
Hdu1232 unimpeded project (and collection)
2022-07-05 07:17:00 【Woodenman Du】
Topic link
http://acm.hdu.edu.cn/showproblem.php?pid=1232
Question
Problem Description
A province investigates the traffic condition of the town , Get the statistics of existing urban roads , The towns directly connected by each road are listed in the table . provincial government “ Unimpeded works ” The goal is to make transportation possible between any two towns in the province ( But there's not necessarily a direct link , As long as they can reach each other indirectly through the road ). Ask how many roads need to be built at least ?
Input
The test input contains several test cases . The... Of each test case 1 Line gives two positive integers , They are the number of towns N ( < 1000 ) And the number of roads M; And then M Row correspondence M road , Each line gives a pair of positive integers , They are the numbers of the two towns directly connected by the road . For the sake of simplicity , Town from 1 To N Number .
Be careful : There can be many roads between the two cities , in other words
3 3
1 2
1 2
2 1
This input is also legal
When N by 0 when , End of input , The use case is not processed .
Output
For each test case , stay 1 The output of the line is the minimum number of roads to be built .
Solve
Obviously, it can be seen that this is a deformation of the minimum spanning tree , It's easier ^_^
Take a look at this article first : Two algorithms of minimum spanning tree
Among them Kruskal Algorithms are commonly used , But this problem is not so troublesome , Simply combine and search sets ~
We can know to get through n Nodes , At the very least n-1 side , So define res = n - 1
According to this condition , Then use and search the set to judge whether each read edge has been connected :
If it is already connected , You don't need to do anything ;
If there is no connection , Then read in the side and connect , The number of roads to be built will also be reduced by one (res--)
I got it after reading res That's the result
AC Code
#include <iostream>
#include <cstring>
#define N 1000
using namespace std;
int fa[N+1], n, m;
// Find the root node
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;
// Initialize and query set
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);
// Already connected Just skip , Unconnected Connect and record
if(dx == dy) continue;
fa[dx] = dy;
res--;
}
cout <<res <<endl;
}
return 0;
}
边栏推荐
猜你喜欢
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
DelayQueue延迟队列的使用和场景
SD_ CMD_ SEND_ SHIFT_ REGISTER
目标检测系列——Faster R-CNN原理详解
【软件测试】04 -- 软件测试与软件开发
Ros2 - common command line (IV)
【软件测试】02 -- 软件缺陷管理
[untitled]
一文揭开,测试外包公司的真实情况
Three body goal management notes
随机推荐
Energy conservation and creating energy gap
Oracle code use
SOC_SD_CMD_FSM
What if the DataGrid cannot see the table after connecting to the database
Initialization of global and static variables
ROS2——ROS2对比ROS1(二)
2022年PMP项目管理考试敏捷知识点(7)
解读最早的草图-图像翻译工作SketchyGAN
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
Mathematical analysis_ Notes_ Chapter 8: multiple integral
Negative number storage and type conversion in programs
Concurrent programming - deadlock troubleshooting and handling
[framework] multi learner
[software testing] 06 -- basic process of software testing
What is soda?
M2DGR 多源多场景 地面机器人SLAM数据集
目标检测系列——Faster R-CNN原理详解
Ros2 topic (VIII)
PowerManagerService(一)— 初始化