Redundant Paths POJ - 3177(tarjan+边双连通分量)

题意:

有n个牧场,要求从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。(此题默认为连通图)

题目:

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1…F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2…R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample:

One visualization of the paths is:
1 2 3
±–±--+
| |
| |
6 ±–±--+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3
±–±--+
: | |
: | |
6 ±–±--+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.

It’s possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

分析:

1.此题为将原本的树经过加边的方式,使之成为一个边双连通图,双连通分量又分点双连通分量和边双连通分量两种。若一个无向图中的去掉任意一个节点(一条边)都不会改变此图的连通性,即不存在割点(桥),则称作点(边)双连通图。一个无向图中的每一个极大点(边)双连通子图称作此无向图的点(边)双连通分量。求双连通分量可用Tarjan算法。
2.首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=5e3+10;
int n,m,a,b,tot,ans;
bool book[M][M]/**标记该条边是否存在*/;
int dfn[M];///时间戳
int low[M];///每个点在这颗树中的,最小的子树的根
int dp[M];///计算入度;           
bool vis[M];/**标记该点是否遍历过*/
void tarjan(int x,int y/**x的父节点,为了防止双向图陷入死循环*/)
{dfn[x]=low[x]=++tot;vis[x]=true;for(int i=1; i<=n; i++){if(book[x][i])/**当该x~i边存在时*/{if(!vis[i])/**该点没有遍历过*/{tarjan(i,x);low[x]=min(low[x],low[i]);}else if(i!=y)/**因为是无向图,需要防止发生“往回走”的现象(即不能回到父节点)*/low[x]=min(low[x],dfn[i]);}}
}
int main()
{scanf("%d%d",&n,&m);for(int i=0; i<m; i++){scanf("%d%d",&a,&b);book[a][b]=book[b][a]=true;/**双向*/}tarjan(1,1);for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)if(book[i][j]){if(low[i]!=low[j])dp[low[j]]++;}ans=0;for(int i=1; i<=n; i++)if(dp[i]==1)ans++;printf("%d\n",(ans+1)/2);///找到叶节点,只要树不存在叶节点就可,一条边可以“消灭”两个叶节点,所以是(ans+)/2;return 0;
}
备战ing,题目分析简略,见谅,转载请注明出处。。。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309637.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Slice的本质

Slice的本质 我们先看下面的代码&#xff0c;看看它的输出是什么&#xff1a; package mainimport "fmt"type Slice []intfunc (A Slice) Append(value int) {A append(A, value) } func main() {mSlice : make(Slice, 10, 20)mSlice.Append(5)fmt.Println(mSlice…

你需要了解操作系统发展历程

本文我们大概回顾计算机操作系统发展历程&#xff0c;这里不会记录关于操作系统的完整历史记录&#xff0c;只是记录那些里程碑事件&#xff0c;看看各位接触计算机时&#xff0c;操作系统发展正处于哪个年代起初没有操作系统&#xff0c;没有编程语言或编译器&#xff0c;甚至…

[汇编语言]实验:更灵活的寻址方式 -应用si和di

实验内容: &#xff08;1&#xff09; 用寄存器SI和DI实现将字符串‘welcome to masm!’ 复制到它后面的数据区中。 &#xff08;2&#xff09; 用[bx(si或di)idata]的方式&#xff0c;来使程序变得简洁。 &#xff08;1&#xff09; 代码如下: assume ds:datasg,cs:code…

http.ListenAndServe()到底做了什么?

参考&#xff1a;https://studygolang.com/articles/25849?frsidebar ​ http://blog.csdn.net/gophers 实现一个最简短的hello world服务器 package mainimport "net/http"func main() {http.HandleFunc("/", func(w http.ResponseWriter, r *http.Reque…

Strongly connected HDU - 4635(tarjan+强连通分量)

题意&#xff1a; 给一个简单有向图&#xff0c;让你加最多的边&#xff0c;使他还是一个简单有向图。 题目&#xff1a; Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph is still a …

C++编程基础题训练

1.编写一个c风格的程序&#xff0c;用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中 1.代码如下 #include <iostream> using namespace std;int main() {int *a new int[25];a[0] 0;a[1] 1;for (int i 2; i < 20; i){a[i] a[i - 1] a…

基于 abp vNext 和 .NET Core 开发博客项目 - 使用Redis缓存数据

上一篇文章完成了项目的全局异常处理和日志记录。在日志记录中使用的静态方法有人指出写法不是很优雅&#xff0c;遂优化一下上一篇中日志记录的方法&#xff0c;具体操作如下&#xff1a;在.ToolKits层中新建扩展方法Log4NetExtensions.cs。//Log4NetExtensions.cs using log4…

G - 水陆距离 HihoCoder - 1478(广搜+队列先进先出性质)

题目&#xff1a; 给定一个N x M的01矩阵&#xff0c;其中1表示陆地&#xff0c;0表示水域。对于每一个位置&#xff0c;求出它距离最近的水域的距离是多少。 矩阵中每个位置与它上下左右相邻的格子距离为1。 Input 第一行包含两个整数&#xff0c;N和M。 以下N行每行M个0…

第一讲 工作区和GOPATH

此为 《极客时间&Go语言核心36讲》 个人笔记&#xff0c;具体课程详见极客时间官网。 Table of Contents generated with DocToc 第一讲 工作区和GOPATH 1. 环境变量配置2. 配置GOPATH的意义 2.1 Go语言源码的组织方式2.2 源码安装后的结果&#xff08;归档文件、可执行文…

C++重载运算符小结与注意点

重载运算符需注意: 1.重载运算符时容易忘记写返回值。 2.重载赋值运算符时&#xff0c;记得加const&#xff0c;因为赋值操作必须是固定的右值。 3重载时&#xff0c;写在类中的只能有一个参数(实际有两个参数&#xff0c;另外一个是this指针&#xff0c;我们看不见而已)&am…

开发大会上,前微软CEO放出的狠话!.NET开发随时起飞,你准备好了吗?

“开发者&#xff0c;开发者&#xff0c;开发者&#xff0c;开发者”&#xff0c;微软前任CEO史蒂夫鲍尔默(Steve Ballmer)用这种略带疯狂、又唱又跳的方式表达他对开发者的热爱。不夸张的说&#xff0c;相比二十年前那个如日中天的巨无霸微软&#xff0c;现在的微软比以往任何…

Balanced Lineup POJ - 3264(线段树模板+查询比大小+建树)

题意&#xff1a; 给你n个数&#xff0c;然后问一段区间的最大的差值是多少。 题目&#xff1a; For the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbe…

第二讲 命令源码文件

此为 《极客时间&Go语言核心36讲》 个人笔记&#xff0c;具体课程详见极客时间官网。 Table of Contents generated with DocToc 第二讲 命令源码文件 1. 什么是命令源码文件&#xff1f;2. 命令参数的接收和解析 2.1 命令源码文件怎么接收参数?2.2 怎样在运行源代码文件…

程序员过关斩将--为微服务撸一个简约而不简单的配置中心

点击上方蓝字 关注我们毫不犹豫的说&#xff0c;现代高速发展的互联网造就了一批又一批的网络红人&#xff0c;这一批批网红又极大的催生了特定平台的一大波流量&#xff0c;但是留给了程序员却是一地鸡毛&#xff0c;无论是运维还是开发&#xff0c;每天都会担心服务器崩溃&a…

Just a Hook HDU - 1698(查询区间求和+最基础模板)

题意&#xff1a; 给你一个1~n的区间&#xff0c;起始区间内均为1&#xff0c;然后对子区间进行值更新&#xff0c;最后求区间和。 题目&#xff1a; In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is ma…

DDIA笔记——数据复制

Table of Contents generated with DocToc 此篇为《数据密集型应用系统设计》&#xff08;DDIA&#xff09;读书笔记&#xff0c;笔记可能存在遗漏&#xff0c;建议直接阅读原书。 第五章 数据复制 主从复制 复制滞后复制滞后带来的问题 多主节点复制 适用场景处理写冲突拓扑结…

基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理

上一篇文章成功使用了Redis缓存数据&#xff0c;大大提高博客的响应性能。接下来&#xff0c;将完成一个任务调度中心&#xff0c;关于定时任务有多种处理方式&#xff0c;如果你的需求比较简单&#xff0c;比如就是单纯的过多少时间循环执行某个操作&#xff0c;可以直接使用.…

Docker基本组成 和 基本命令

此篇为Docker笔记&#xff0c;文章可能存在疏忽&#xff0c;建议直接观看原视频。 视频地址&#xff1a;https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from333.999.0.0 Docker基本组成 和 基本命令 镜像 image&#xff1a;就好比一个模板&#xff0c;可以通过这个模板…