题意:

有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,题目分析简略,见谅,转载请注明出处。。。。。

Redundant Paths POJ - 3177(tarjan+边双连通分量)相关推荐

  1. (连通图 ) Redundant Paths --POJ --3177

    链接: http://poj.org/problem?id=3177 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#probl ...

  2. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  3. POJ 3694 Network ★(边双连通分量+并查集缩点+LCA)

    [题意]一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(每次回答是在上一次连边的基础之上) [分析]好题,做完后涨了很多姿势~ 普通做法当然就是每加 ...

  4. 图论 —— 图的连通性 —— Tarjan 求双连通分量

    [概念] 1.双连通分量:对于一个无向图,其边/点连通度大于1,满足任意两点之间,能通过两条或两条以上没有任何重复边的路到达的图,即删掉任意边/点后,图仍是连通的 2.分类: 1)点双连通图:点连通度 ...

  5. POJ 3352 无向图边双连通分量,缩点,无重边

    为什么写这道题还是因为昨天多校的第二题,是道图论,HDU 4612.当时拿到题目的时候就知道是道模版题,但是苦于图论太弱.模版都太水,居然找不到.虽然比赛的时候最后水过了,但是那个模版看的还是一知半解 ...

  6. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

  7. 图论学习-无向图双连通分量

    文章目录 无向图双连通分量 1.基本术语与概念 1.1.割点 1.2.桥 1.3.边双连通分量 (e-DCC) 1.4 点双连通分量 (v-DCC) 1.5 时间戳 2.求解 2.1 边双连通分量 2 ...

  8. 【POJ - 3177】Redundant Paths(边双连通分量,去重边)

    题干: In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1.. ...

  9. POJ 3177 Redundant Paths (边双连通+缩点)

    <题目链接> <转载于 >>>  > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...

最新文章

  1. Codeforces Round #323 (Div. 2)
  2. tomcat:Could not publish to the server. java.lang.IndexOutOfBoundsException
  3. IT人的十八般武艺-操作系统
  4. Python动态变量名定义与调用
  5. nginx location 匹配 多个规则_你需要知道的Nginx配置二三事
  6. Yii的scenarios
  7. C语言程序练习-L1-019 谁先倒 (15分)
  8. 更新npm至最新版本
  9. C语言 __DATE__ - C语言零基础入门教程
  10. 社交评论插件简单对比
  11. 记一次服务器执行MySQL耗时问题
  12. jenkins组权限_Jenkins 中基于角色的权限管理
  13. FL Studio20.8中文版界面下载更新内容介绍
  14. MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message
  15. Mysql监控工具介绍-Monyog
  16. 储氢合金/金属氢化物吸放氢动力学模型——Chou模型
  17. 日本全新超级计算机ABCI向“全球最快”目标冲击
  18. HTML 打印table分页
  19. 重构实践:基于腾讯云Elasticsearch搭建QQ邮箱全文检索
  20. freopen函数详解

热门文章

  1. Android之让手机能识别当前app为浏览器类型的APP
  2. linux c之用fwrite和fread实现文件的复制
  3. linux 下删除文件夹(文件夹不为空时)
  4. Android之Android Studio 快捷键整理分享
  5. 无代码iVX编程实现简单 小蜜蜂 经典游戏
  6. [python opencv 计算机视觉零基础到实战] 三、numpy与图像编辑
  7. matlab的循环语句裁图,[MATLAB图像处理] 多幅图片处理的循环语句
  8. 机械史上最复杂的巅峰之作,这才是最强大脑!
  9. 数学和物理太难?这些动图让你秒懂抽象概念
  10. 这才是真正的,坐上来,自己动!| 今日趣图