POJ-3352 Redundant Paths
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
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)/2,加边方法为每次取两个LCA最远的叶节点,在他们两个中间连一条边,重复取直到加完。
注意:这道题两点之间会有多条边,不能简单的判断 To != father ,必须判断是否为同一条边。因为边为无向边,所以不知道 i+1 和 i-1 那一条和 i 是同一条边。这里介绍一种妙不可言的方法,把每条无向边的编号赋值为这条边的权,所以只需判断两条边权是否相同即可。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define file(a) freopen(a".in","r",stdin); freopen(a".out","w",stdout); inline int gi()
{
bool b=; int r=; char c=getchar();
while(c<'' || c>'') { if(c=='-') b=!b; c=getchar(); }
while(c>='' && c<='') { r=r*+c-''; c=getchar(); }
if(b) return -r; return r;
} const int inf = 1e9+, N = , M = ;
int n,m,num,Deep,f[N],dfn[N],low[N],cd[N];
bool b[N];
stack <int> s;
struct data
{
int nx,to,ds;
}da[M]; inline void add (int fr,int to,int ds)
{
da[++num].to=to, da[num].nx=f[fr], da[num].ds=ds, f[fr]=num;
} inline void tarjan (int o,int fa)
{
int i,to;
dfn[o]=low[o]=++Deep; b[o]=;
for (i=f[o]; i; i=da[i].nx)
{
to=da[i].to;
if (da[i].ds == da[fa].ds) continue;
if (!dfn[to]) tarjan (to,i), low[o]=min(low[o],low[to]);
else if (b[to]) low[o]=min(low[o],dfn[to]);
}
b[o]=;
} int main()
{
// file("POJ-3177");
n=gi(), m=gi();
int i,j,x,y;
for (i=; i<=m; i++)
{
x=gi(), y=gi();
add (x,y,i), add (y,x,i);
}
for (i=; i<=n; i++) if (!dfn[i]) tarjan (i,);
for (i=; i<=n; i++)
for (j=f[i]; j; j=da[j].nx)
{
x=low[da[j].to], y=low[i];
if (x != y) cd[y]++;
}
x=;
for (i=; i<=n; i++) if (cd[i] == ) x++;
printf ("%d\n",(x+)/);
return ;
}
欢迎在评论区提问质疑!
POJ-3352 Redundant Paths的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
随机推荐
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- shell的case脚本的简单入门
shell的case脚本的简单入门 示例1: #/bin/bash a=$ case "$a" in ") echo 'hell 2';; ") echo 'h ...
- 测试开发系列之Python开发mock接口(二)
上一篇咱们已经把开发前的环境准备好了,还需要再做一点准备,你的账户信息是存在哪的呢,当然是存在数据库里的,咱们在去支付,扣钱的时候,肯定是从数据库里面操作的,去更新账户表里面的数据,所以咱们先要把数据 ...
- [bzoj1345][Baltic2007]序列问题_单调栈
bzoj-1345 Baltic-2007 序列问题 题目大意:对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai ...
- python3.x之print()
1.print内容 #!/usr/bin/python print('hello world') //print("hello world") 2.print变量 #!/us ...
- KS103超声波测距模块
max232:电平转换芯片,将电脑的RS-232标准串口(高+12V,低-12V)转换为(高+5V,低0V). 电脑串口(RS -232) => 单片机串口(TTL串口) SIPEX SP323 ...
- 升级python2至python3解决依赖关系
1.最小化安装centos7,在升级python3的时候会出现很多包未安装,为解决依赖关系: yum -y install gcc gcc-c++ zlib zlib-devel libffi-dev ...
- PythonCookbook读书笔记
第一章 数据结构和算法 1.1 将序列分解为单独的变量 适用于元组.列表.字符串等.只要是可迭代的对象,都可以执行分解操作.唯一的要求是变量的总数和结构要与序列相同. 1.2 从任意长度的可迭代对象中 ...
- iOS10获得系统权限
iOS 10 对系统隐私权限的管理更加严格,如果你不设置就会直接崩溃,一般解决办法都是在info.plist文件添加对应的Key-Value就可以了. <!-- 相册 --> <ke ...
- ftp服务器调用出错
因工作需要,需从FTP服务器上下载文件,本地测试OK,服务器上在FTP服务器连接之后卡住,没有异常和提示信息,在打印FTP命令之后,显示:150 Opening data channel for fi ...