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时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
随机推荐
- LayUI分页基于ASP.NET MVC
---恢复内容开始--- 今天写了挺久的分页,百度了很多都没有很好的.Net实例,今天我来更新一期关于layuiTable分页 首先你得理解layui的官方文档的Table分页部分,我在这里附上地址 ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- linux 在当前目录下查找一个,或者多个文件
1.find ./ -name "y*" 查找以y开头的文件. find ./ -name "*sql*" 查找包含 sql 的文件名 2.查找redis su ...
- jmespath库解析json
在测试过程中,经常会去JSON中的某个值,jmespath可以是除了jsonpath的另外一种选择. 下面通过几个例子来说明jmespath在python的使用 jmespath python安装 非 ...
- Bone Collector II(01背包kth)
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- bzoj4568 [Scoi2016]幸运数字 线性基+树链剖分
A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游览 A ...
- linux rdesktop远程Win7老是提示密码错误问题解决
最近使用rdesktop远程Win7老是提示密码错误,输了N次,无比确认密码是正确的. 在Win7系统本身登录也是正常的. 但rdesktop远程就是报密码错误. 开始怀疑更新了最新版本问题,但是使用 ...
- SpringBoot Beans管理和自动配置
原 SpringBoot Beans管理和自动配置 火推 02 2017年12月20日 21:37:01 阅读数:220 SpringBoot Beans管理和自动配置 @SpringBootAppl ...
- 【BZOJ1211】树的计数(Prufer编码)
题意:一个有n个结点的树,设它的结点分别为v1, v2, …, vn, 已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵. 其中1<=n<=150,输入数据保证满足条件的 ...