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时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
随机推荐
- SpringMVC修改功能
articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...
- 洛谷 P1156 垃圾陷阱 谈论剪枝,非满分
这是一个91分的非dp代码(是我太弱) 剪枝八五个(实际上根本没那么多,主要是上课装逼,没想到他们dp水过去了),不过我的思路与dp不同: 1.层数到达i+1,return 这个必须有 2.当前剩余生 ...
- jmeter给cookie设置sessionId避免其他脚本多次登录
1.相关知识: http头部可以设置:浏览器显示内容类型,如content-type:text/html http头部可以存放:浏览器的cookie信息——cookie是对用户身份进行判断的内容 ht ...
- Python内置函数—bytearray
英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...
- php基础语句 变量 符号
中心主题 标记与注释 // /* */ 输出语句 echo输出 echo可以输出多个字符串,逗号隔开 print输出 print只能输出一个字符串,返回true或false print_r() 可以把 ...
- server中intersect的用法
intersect 就像数学中的交集一样, select nam from tb_table1 intersect select name from tb_table2 查询的是两个数据集的交集 ...
- 【尺取】HDU Problem Killer
acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...
- 1027 stl
#include<stdio.h> #include<queue> using namespace std; int main() { int i,n,m,j,k,a[100 ...
- bzoj 1702 贪心,前缀和
[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 807 Solved: ...
- C# 通过HTTP代理访问Socket来获取邮件
C# 通过HTTP代理访问Socket来获取邮件 关键穿透代理的代码(通过HTTP代理获取TcpClent) public class ClientHelper { public static Tcp ...