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,不过 ...
随机推荐
- Laravel 报500错误
Laravel报500错误 发生情境: 使用Composer安装Laravel5.1版本到本地wamp环境,可以成功访问框架首页,然后上传到服务器上,报500错误. 解决: (1)在首页public/ ...
- 精通python网络爬虫之自动爬取网页的爬虫 代码记录
items的编写 # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentati ...
- jsp/servlet实现简单上传和下载
使用JSP/Servlet简单实现文件上传与下载 jsp上传页面代码: <%@ page language="java" import="java.util.*&q ...
- 高效的MySQL的批插入 BULK INSERT
原文:http://www.open-open.com/code/view/1453702496573 MySQL的批插入 BULK INSERT和load data的速度差不多,并且可靠. 语法如下 ...
- js -- 侧边悬浮栏特效
github: https://github.com/mybee/float-scroll-page #menu{width: 120px;height: auto; position: fixed; ...
- Go -- 漫谈IM通信架构
前前后后做的IM和推送系统已经有好几个了,一直都想好好总结下,因此就有了这篇文章.在我刚学编程的那会儿,觉得网络通信是一个很牛逼和门槛很高的一门技术,但是随着开源技术的发展和互联网知识的共享,现在要写 ...
- flask生成环境不要使用其自身低性能的服务器
flask自带一个服务器,主要用在开发环境.默认情况下一次只能处理一个请求,当然你也可以设置为多进程或者多线程的情况. 但是其自带服务器的处理能力比较有限.生成环境下应该使用其他的服务器,参照:htt ...
- 边看chromium的代码,边想骂人...
这一年一直在看chromium for android的代码,边看边想骂,谷歌这帮人..一开始搞了个牛逼的架构,在安卓4.4上把以前android webkit团队的简单版替换掉了,结果发现性能大不如 ...
- [转]Go基础之锁的初识
当我们的程序就一个线程的时候是不需要用到锁的,但是通常我们实际的代码不会是单个线程的,所有这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? 当我们多个线程在读相同的数据的时候则是需要加锁 ...
- 【自用】OI计划安排表一轮
网络流√ 上下界最大流√ 线性规划转费用流√ RMQ优化建图√ 单纯形√ 字符串相关 hash√ 扩展KMP 回文自己主动机 数据结构 平衡树 启示式合并 替罪羊树 LCT 树套树 KD-Tree 二 ...