poj3352 Road Construction

2023-11-07 21:32
文章标签 construction road poj3352

本文主要是介绍poj3352 Road Construction,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Description

It’s almost summer time, and that means that it’s almost summer
construction time! This year, the good people who are in charge of the
roads on the tropical island paradise of Remote Island would like to
repair and upgrade the various roads that lead between the various
tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange
customs of the island, the roads are arranged so that they never meet
at intersections, but rather pass over or under each other using
bridges and tunnels. In this way, each road runs between two specific
tourist attractions, so that the tourists do not become irreparably
lost.

Unfortunately, given the nature of the repairs and upgrades needed on
each road, when the construction company works on a particular road,
it is unusable in either direction. This could cause a problem if it
becomes impossible to travel between two tourist attractions, even if
the construction company works on only one road at any particular
time.

So, the Road Department of Remote Island has decided to call upon your
consulting services to help remedy this problem. It has been decided
that new roads will have to be built between the various attractions
in such a way that in the final configuration, if any one road is
undergoing construction, it would still be possible to travel between
any two tourist attractions using the remaining roads. Your task is to
find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r,
separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist
attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads.
The tourist attractions are conveniently labelled from 1 to n. Each of
the following r lines will consist of two integers, v and w, separated
by a space, indicating that a road exists between the attractions
labelled v and w. Note that you may travel in either direction down
each road, and any pair of tourist attractions will have at most one
road directly between them. Also, you are assured that in the current
configuration, it is possible to travel between any two tourist
attractions.

Output

One line, consisting of an integer, which gives the minimum number of
roads that we need to add.

首先用tarjan将边双连通分量缩点,然后得到的是一棵树。
在树上找到找到所有度为1的点,将它们之间相互连边,则可以消除掉所有桥。边数为(度为1的点数+1)/2。

#include<cstring>
#include<cstdio>
int fir[1010],ne[3010],to[3010],bel[1010],dfn[1010],low[1010],tot,cnt,sta[1010],top,du[1010];
void dfs(int p,int fa)
{int i,j,k,x,y,z;dfn[p]=low[p]=++cnt;sta[++top]=p;for (i=fir[p];i;i=ne[i])if (!dfn[to[i]]){dfs(to[i],p);if (low[to[i]]<low[p]) low[p]=low[to[i]];}elseif (to[i]!=fa&&dfn[to[i]]<low[p]) low[p]=dfn[to[i]];if (dfn[p]==low[p]){tot++;do{x=sta[top--];bel[x]=tot;}while (x!=p);}
}
int main()
{int i,j,k,m,n,p,q,x,y,z,ans;scanf("%d%d",&n,&m);for (i=1;i<=m;i++){scanf("%d%d",&x,&y);ne[2*i]=fir[x];fir[x]=2*i;to[2*i]=y;ne[2*i+1]=fir[y];fir[y]=i*2+1;to[2*i+1]=x; }dfs(1,-1);for (i=1;i<=n;i++)for (j=fir[i];j;j=ne[j])if (bel[i]!=bel[to[j]])du[bel[i]]++;ans=0;for (i=1;i<=tot;i++)if (du[i]==1) ans++;printf("%d\n",(ans+1)/2);
} 

这篇关于poj3352 Road Construction的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/366395

相关文章

数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

Tree Construction  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定n个数,按照构造Binary Search Tree的方式来构造BST树,按顺序输出每一个非root结点的父节点的值。 analyse

【UVALive】5713 Qin Shi Huang's National Road System 最小生成树

传送门:【UVALive】5713 Qin Shi Huang's National Road System 题目大意:秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通。道士徐福声称他可以用法术修路,不花钱,也不用劳动力,但只能修一条路,因此需要慎重选择用法术修哪一条路。秦始皇不仅希望其他道路的总长度B尽量短(这样可以节省劳动力),还希望法术连接的两个城市的人口之和A尽量大,因此下

【网络流】- LA5905-Pool construction

链接:我是链接啊啊啊哈哈哈 题意:  泳池最外一圈必须全搞成# 对其他的点有三种处理方式 1.放着不动 2.花费 d 把 # 变成 . 3.花费 f 把 . 变成 # 最后 # 和 . 不能相邻,要在他们之间修围墙 围墙单位边造价是b 问最小总花费 图最后变成什么样都行,只要能让花费最小  思路: 一看是修围墙,那就

hdu 1598 find the most comfortable road (并查集 + 枚举)

题目:         链接:点击打开链接 思路:         对边排序,再枚举每条边,如果出现通路(findset(x) == findset(y))就结束。 代码: #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define

poj 3352 Road Construction(边连通+tarjan+缩点)

http://poj.org/problem?id=3352 题意:简化一下原题题意,意思就是给定一个连通图,问至少要加入几条边使得整个图变成一个边连通图,即图中任意两点都有两条以上的路径(不一定直接相连)。 思路:tarjan算法,设置一个low数组,在建立深搜树的过程中,我们会得到每个节点的low值,对于low值相等的节点在同一个双连通分量中。由于在同一个边连通分量中的点的“地

建筑业AI的崛起The Rise of AI and Machine Learning in Construction

Apr 3, 2024 16 min read NSDT工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - AI模型在线查看 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

Road-SLAM:基于道路标线车道级精度SLAM

文章:Road-SLAM : Road Marking based SLAM with Lane-level Accuracy 作者:Jinyong Jeong, Younggun Cho, and Ayoung Kim1 编译:点云PCL 本文仅做学术分享,如有侵权,请联系删除。欢迎各位加入免费知识星球,获取PDF论文,欢迎转发朋友圈。内容如有错误欢迎评论留言,未经允许请勿转载! 对本文以及俯

uva10720 - Graph Construction(Havel-Hakimi定理)

题目:uva10720 - Graph Construction(Havel-Hakimi定理) 题目大意:给出N个点,并且给出每个点的度,问能否形成简单图。 解题思路:一开始自己写想了些形成简单图的条件,例如度数之和是偶数,度数的一半也就是简单图的边不能超过n * (n - 1) / 2,每个顶点的度数都应该小于总的顶点个数,但后面发现这些只是必要的条件。后来看了题解发现大神们都

道路 Road(百练,POJ)

题目链接: 1724 -- ROADS (poj.org) 题目描述: 思路: 这题目乍一看,是一个含有2个标尺的单源最短路问题,挺难处理的。 既然没法直接用最短路处理,那我们就“记录信息”,将花费的时间也记录进dp数组,然后跑“状态最短路”。 用f[i][j] 表示到达点i 且 总花费时间为j的最短距离,然后跑堆优化的dijkstra算法就好。由于不含有负边权,因此可以搞一个vis数

【语义分割】——又快又强:Deep Dual-resolution Networks for Real-time and Accurate Semantic Segmentation of Road

出处:哈尔滨工业大学 论文 code:暂未开源 关键词: 实时语义分割 语义分割是自动驾驶汽车了解周围场景的关键技术,对于实际的自动驾驶汽车来说,为了获得高精度的分割结果而花费大量的推理时间是不可取的。使用轻量级架构(编码器解码器或two-pathway)或推理在低分辨率图像。本文提出的模型在单张2080ti上DDRNet-slim能打到77.4% mIoU和230FPS,DDRNet