POJ3177 Redundant Paths —— 边双联通分量 + 缩点
题目链接:http://poj.org/problem?id=3177
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15852 | Accepted: 6649 |
Description
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
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.
Source
题解:
1. 已知题目给出的图是联通图,那么我们可以用Tarjan算法求出每个边双联通分量。为什么是边双联通分量而不是点双联通分量或者其他?答:因为题目要求每个点之间至少有两条路径(并且路径中不能有重叠的边),位于同一个边双联通分量的点之间都至少有两条路径,但是位于不同边双联通分量的点只有一条路径。所以需要根据边双联通对原图进行划分。
2.对每个边双连通分量进行缩点,得到的是一棵无根树。那么无根树怎样才能变成边双联通图呢?至少需要添加几条边呢?答:只需要把所有叶子结点都“消灭”掉就可以了。那么最少需要添加:(叶子结点数+1)/2 条边。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; struct Edge
{
int to, next;
bool cut;
}edge[MAXN<<];
int head[MAXN], tot; int index, dfn[MAXN], low[MAXN];
int block, belong[MAXN];
int top, Stack[MAXN], instack[MAXN];
int degree[MAXN]; void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].cut = false;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u, int pre)
{
low[u] = dfn[u] = ++index;
Stack[top++] = u;
instack[u] = true;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(v==pre) continue;
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v]>dfn[u])
{
edge[i].cut = true;
edge[i^].cut = true;
}
}
else if(instack[v])
low[u] = min(low[u], dfn[v]);
} if(low[u]==dfn[u])
{
block++;
int v;
do
{
v = Stack[--top];
instack[v] = false;
belong[v] = block;
}while(v!=u);
}
} void init()
{
tot = ;
memset(head,-,sizeof(head)); index = ;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low)); block = top = ;
memset(instack,,sizeof(instack)); memset(degree,,sizeof(degree));
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v,u);
} Tarjan(, );
for(int u = ; u<=n; u++)
for(int i = head[u]; i!=-; i = edge[i].next)
if(edge[i].cut) //不需要两端都加,因为一条割边被标记了两次。一次正好对应一个端点。
degree[belong[u]]++; int leaf = ;
for(int i = ; i<=block; i++)
if(degree[i]==) leaf++; printf("%d\n", (leaf+)/);
}
}
POJ3177 Redundant Paths —— 边双联通分量 + 缩点的更多相关文章
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)
传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...
- HDU5409---CRB and Graph 2015多校 双联通分量缩点
题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...
- POJ3694 Network —— 边双联通分量 + 缩点 + LCA + 并查集
题目链接:https://vjudge.net/problem/POJ-3694 A network administrator manages a large network. The networ ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
- poj3177 Redundant Paths 边双连通分量
给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...
- POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)
[题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...
- POJ3177 Redundant Paths【双连通分量】
题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1. 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
随机推荐
- SpringCloud源码地址
SpringCloud实战源代码 https://github.com/springcloud/spring-cloud-code.git
- *lucene索引_的删除和更新
[删除] [恢复删除] [强制删除] [优化和合并] [更新索引] 附: 代码: IndexUtil.java: package cn.hk.index; import java.io.File; i ...
- iptables之FORWARD转发链
注意:本机路由转发的时候,才配置FORWARD转发链! #iptables –A FORWARD –s 192.168.0.0/24 –j ACCEPT #iptables –A FORWARD –d ...
- reactNative 介绍
React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是Facebook早先开源的UI框架 React 在原生移动应用平台的衍生产物,目前支持iOS和安 ...
- Python+fiddler(基于Cookie绕过验证码自动登录)
案例:使用Cookie绕过百度验证码自动登录账户 步骤: 1.浏览器进入百度首页,点击登录按钮,输入相关信息(注意:暂时不要点击登录按钮) 2.进入fiddler,首先获取证书,Tools--> ...
- zoj 2736 Daffodil number
Daffodil number Time Limit: 2 Seconds Memory Limit: 65536 KB The daffodil number is one of the ...
- 【Java 理论篇 1】Java2平台的三个版本介绍
导读:关于java的三种分类J2SE.J2EE.J2ME,在网上有很多资料,然后自己写的,也大多是从各个网站上搜罗里的.算是自己的一种笔记,或者明白的说,就是把别人的东西抄一遍.但是,这对于我来说,也 ...
- 两行代码搞定UI主流框架
XCNavTab XCNavTab适用于快速搭建NavigationController和TabBarController相结合的框架 https://github.com/xiaocaiabc/XC ...
- [NOIP2001] 提高组 洛谷P1026 统计单词个数
题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保 证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的 ...
- Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device
Linux MTD (Memory Technology Device) subsystem analysis For Atheros char device 读了Linux MTD 源代码分析 对这 ...