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

题干:

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.

题目大意:

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

解题报告:

在POJ - 3352的基础上,加个去重边可以过。

但是对于这组样例就过不了

2 2
1 2
2 1

按照题意答案应该是0,但是AC代码1的答案输出是1.

所以再次证明了不能只看low数组是否相同来判断是否属于一个边双连通分量

AC代码1:(但是其实是错误代码只不过是数据若)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int n,m;
bool mm[5555][5555];
int dfn[MAX],low[MAX],out[MAX];
int stk[MAX],index,clk,scc;
void init() {for(int i = 1; i<=n; i++) {vv[i].clear();dfn[i]=low[i]=out[i]=0;for(int j = 1; j<=n; j++) mm[i][j]=0;}clk = index = scc = 0;
}
void tarjan(int x,int rt) {dfn[x] = low[x] = ++clk;int up = vv[x].size();for(int i = 0; i<up; i++) {int v = vv[x][i];if(v == rt) continue;if(dfn[v] == 0) {tarjan(v,x);low[x] = min(low[x],low[v]);}else low[x] = min(low[x],dfn[v]);}
}
int main()
{while(~scanf("%d",&n)) {scanf("%d",&m);init();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);if(mm[u][v]==1) continue;mm[u][v]=mm[v][u]=1;vv[u].pb(v);vv[v].pb(u);}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i,-1);//因为题目保证是连通图所以其实可以直接tarjan(1,-1)的 }for(int up,u = 1; u<=n; u++) {up = vv[u].size();for(int v,j = 0; j<up; j++) {v = vv[u][j];//图G中Low值相同的两个点必定在同一个边双连通分量(即同一个缩点)中if(low[u] == low[v]) continue;//检查i、j是否不在同一个缩点中out[low[u]]++;//out[low[v]]++;//注意是对缩点操作,不是对原图的点}}int ans = 0;//记录总度数=1(叶子)的缩点for(int i = 1; i<=n; i++) {if(out[i] == 1) ans++;//由于是无向图,因此每个缩点的度都重复计算了2次,因此度数==2才是叶子结点}printf("%d\n",(ans+1)/2);//将一棵树连成一个边双连通分量至少需要添加的边数=(叶子节点数+1)/2}return 0 ;
}

AC代码2:(注意对于重边的处理,这里采用kuangbin的写法,当然如果你是邻接表的写法也可以用下面附的那部分代码)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int n,m;
int dfn[MAX],low[MAX],out[MAX],col[MAX];
int stk[MAX],index,clk,bcc;
void init() {for(int i = 1; i<=n; i++) {vv[i].clear();dfn[i]=low[i]=out[i]=0;}clk = index = bcc = 0;
}
void tarjan(int x,int rt) {dfn[x] = low[x] = ++clk;stk[++index] = x;int up = vv[x].size(),cntrt=0;for(int i = 0; i<up; i++) {int v = vv[x][i];if(v == rt && cntrt == 0) {cntrt++;continue;}if(dfn[v] == 0) {tarjan(v,x);low[x] = min(low[x],low[v]);}else low[x] = min(low[x],dfn[v]);}if(dfn[x] == low[x]) {bcc++;while(1) {int tmp = stk[index];index--;col[tmp] = bcc;if(tmp == x) break;}}
}
int main()
{while(~scanf("%d",&n)) {scanf("%d",&m);init();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].pb(v);vv[v].pb(u);}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i,-1);//因为题目保证是连通图所以其实可以直接tarjan(1,-1)的 }for(int up,u = 1; u<=n; u++) {up = vv[u].size();for(int v,j = 0; j<up; j++) {v = vv[u][j];//图G中Low值相同的两个点必定在同一个边双连通分量(即同一个缩点)中if(col[u] == col[v]) continue;//检查i、j是否不在同一个缩点中out[col[u]]++;//out[low[v]]++;//注意是对缩点操作,不是对原图的点}}int ans = 0;//记录总度数=1(叶子)的缩点for(int i = 1; i<=n; i++) {if(out[i] == 1) ans++;//由于是无向图,因此每个缩点的度都重复计算了2次,因此度数==2才是叶子结点}printf("%d\n",(ans+1)/2);//将一棵树连成一个边双连通分量至少需要添加的边数=(叶子节点数+1)/2}return 0 ;
}

附代码: 

void tarjan(int x,int rti) {dfn[x] = low[x] = ++clk;stk[++index] = x;for(int i = head[x]; ~i; i=e[i].ne) {int v = e[i].to;if(i == rti^1) continue;if(dfn[v] == 0) {tarjan(v,i);low[x] = min(low[x],low[v]);}else low[x] = min(low[x],dfn[v]);}if(dfn[x] == low[x]) {bcc++;while(1) {int tmp = stk[index];index--;col[tmp] = bcc;if(tmp == x) break;}}
}

 

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

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

相关文章

#pragma 详解

#pragma 求助编辑 pragma - 必应词典美[prɡmə]英[prɡmə]n.〔计〕杂注网络编译指示&#xff1b;显示编译指示&#xff1b;特殊指令 百科名片 在所有的预处理指令中&#xff0c;#Pragma 指令可能是最复杂的了&#xff0c;它的作用是设定编译器的状态或者是指示编译器完成一些…

1.绪论

目录 &#xff08;1&#xff09;C语言传值与传地址变量 &#xff08;2&#xff09;算法效率的度量 &#xff08;3&#xff09;基本操作 &#xff08;4&#xff09;主函数 主要由实现基本操作和算法的程序构成。这些程序有6类&#xff1a; 数据存储结构&#xff0c;文件名第…

Java 新手习题()

循环 1.&#xff08;for 循环&#xff09;计算123…100 的和 package com.fxm.day03.test; public class day03test1{public static void main(String[] args){int sum 0;for(int i 1; i < 101; i){sum i;}System.out.println("1到100的和&#xff1a;"sum);…

(ECC)椭圆曲线加密算法原理和C++实现源码

目录 &#xff08;1&#xff09;ECC加密原理&#xff1a; &#xff08;2&#xff09;编译生成LibTommath静态库 &#xff08;3&#xff09;ECC源码 今天介绍一下利用LibTommath数学库实现椭圆曲线加密算法的原理和源码。 &#xff08;1&#xff09;ECC加密原理&#xff1a;…

【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

题干&#xff1a; We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product VV, its elements being called edges. Then G(…

c语言3种链接属性: 外部(external), 内部(internal),无设置(none)

c语言中&#xff0c;多个文件组合的时候&#xff0c;有可能标示名相同&#xff0c;那么这个时候编译器如何判别的呢&#xff1f; c语言中有3种链接属性: 外部&#xff08;external&#xff1a;可以被其他文件访问到&#xff09;, 内部(internal&#xff1a;无法被其他文件访问到…

java 数组习题

2.写一个函数&#xff0c;返回一个整数数组的平均值 package com.fxm.day05.test; public class Day05test{public static void main(String[] args){int[] a1 {2,9,5,7,4};int n average(a1);System.out.println(n);}public static int average(int[] a){int sum 0;for(in…

机器学习笔记(1):Introduction

目录 1&#xff09;welcome 2&#xff09;What is Machine Learning 3&#xff09;Supervised Learning 4&#xff09;Unsupervised Learning 1&#xff09;welcome 第一个视频主要介绍了机器学习目前的案例&#xff0c;主要有&#xff1a;数据库挖掘、医疗记录、生物工程…

【POJ - 3352】Road Construction(Tarjan,边双连通分量)

题干&#xff1a; Its almost summer time, and that means that its 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…

C++匿名命名空间

当定义一个命名空间时&#xff0c;可以忽略这个命名空间的名称&#xff1a; namespce {char c;int i;double d;}编译器在内部会为这个命名空间生成一个唯一的名字&#xff0c;而且还会为这个匿名的命名空间生成一条using指令。所以上面的代码在效果上等同于&#xff1a;namespa…

机器学习简易入门-附推荐学习资料

目录 &#xff08;1&#xff09;机器学习正规学习路线 &#xff08;2&#xff09;机器学习快速入门 &#xff08;3&#xff09;总结 感谢黄海广博士的分享 原创&#xff1a; 机器学习初学者 机器学习初学者 今天 机器学习如何入门&#xff1f;目前没有明确的答案。本站面向…

python 习题

使用蒙特-卡罗方法计算圆周率近似值 蒙特-卡罗方法是一种通过概率来得到问题近似解的方法。假设又一块边长为2的正方形木板&#xff0c;上面画一个单位圆&#xff0c;然后随意往木板上扔飞镖&#xff0c;落点坐标(x,y)必然在木板上(更多的是落在单位圆内)&#xff0c;如果扔的…

C++11中的std::function

原文地址&#xff1a;http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码&#xff1a; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; 这两…

【HDU - 3394】Railway(点双连通分量,Tarjan算法,思维tricks)

题干&#xff1a; There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one …

飞机大战(简易版)

一、游戏分析 飞机大战中的主要“角色”有&#xff1a; 1.英雄 2.敌方飞机 3.英雄发射的子弹 我们需要控制的有&#xff1a; 1.绘制屏幕内的角色 2.控制角色的逻辑&#xff0c;比如&#xff1a;敌方飞机与我方飞机的碰撞检测&#xff0c;我方飞机发射的子弹与敌方飞机之间的碰撞…

在Ubuntu上安装Keras深度学习框架

目录 1&#xff09;安装pip 2&#xff09;安装Python科学套件 3&#xff09;安装TensorFlow 4&#xff09;安装keras 5&#xff09;安装Jupyter Notebook 6&#xff09;运行Keras 本文介绍如何在Ubuntu上安装Keras深度学习框架。 1&#xff09;安装pip 安装pip包&#…

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)

题干&#xff1a; Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still…

C++11 FAQ中文版:std::function 和 std::bind

std::function 和 std::bind 标准库函数bind()和function()定义于头文件中&#xff08;该头文件还包括许多其他函数对象&#xff09;&#xff0c;用于处理函数及函数参数。bind()接受一个函数&#xff08;或者函数对象&#xff0c;或者任何你可以通过”(…)”符号调用的事物&am…

Java 习题(面向对象)

1.&#xff08;面向对象基础&#xff09;写一个Worker 类&#xff0c;并创建多个Worker 对象。 为Worker 类添加四个属性&#xff0c; <1>int 类型的id&#xff0c;表示工人的编号&#xff1b; <2>String 类型的name&#xff0c;表示工人的姓名&#xff1b; <3…

机器学习笔记(2):单变量线性回归

目录 1&#xff09;Model representation 2&#xff09;Cost function 3&#xff09;Cost function intuition 1 4&#xff09;Cost function intuition2 5&#xff09;Gradient descent 6&#xff09;Gradient descent intuition 7&#xff09;Gradient descent for li…