POJ3177 边双连通分量
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18580 | Accepted: 7711 |
Description
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
题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。
分析:在同一个边双连通分量中看做同一个点,缩点后,新图是一棵树,树的边就是原无向图的桥。
问题转化为:在树中至少添加多少条边能使图变为双连通图。
结论:添加边数=(树中度为1的节点数+1)/2
代码:
#include<cstdio>
#include<cstring>
#include "algorithm"
using namespace std;
const int N = + ;
const int M = + ;
struct P {
int to, nxt;
} e[M * ];
int head[N], low[N], dfn[N], beg[N], du[N], st[M], ins[M];
int cnt, id, top, num; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
} void tarjan(int u, int fa) {
low[u] = dfn[u] = ++id;
st[++top] = u;
ins[u] = ;
for (int i = head[u]; i != -; i = e[i].nxt) {
int v = e[i].to;
if (i == (fa ^ )) continue;
if (!dfn[v]) tarjan(v, i), low[u] = min(low[u], low[v]);
else if (ins[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
int v;
do {
v = st[top--];
ins[v] = ;
beg[v] = num;
} while (u != v);
num++;
}
} void init() {
cnt = id = top = num = ;
memset(head, -, sizeof(head));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
memset(du, , sizeof(du));
} int n, m;
int main() {
scanf("%d%d", &n, &m);
init();
for (int i = ; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i, -);
for (int i = ; i <= n; i++) {
for (int j = head[i]; j != -; j = e[j].nxt){
int v = e[j].to;
if (beg[i] != beg[v]) du[beg[i]]++;
}
}
int ans = ;
for (int i = ; i < num; i++)
if (du[i] == ) ans++;
printf("%d\n", (ans + ) / );
return ;
}
POJ3177 边双连通分量的更多相关文章
- poj3177边-双连通分量
题意和poj3352一样..唯一区别就是有重边,预先判断一下就好了 #include<map> #include<set> #include<list> #incl ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- POJ3177 Redundant Paths 双连通分量
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- poj3177 Redundant Paths 边双连通分量
给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...
- POJ3177 Redundant Paths 图的边双连通分量
题目大意:问一个图至少加多少边能使该图的边双连通分量成为它本身. 图的边双连通分量为极大的不存在割边的子图.图的边双连通分量之间由割边连接.求法如下: 求出图的割边 在每个边双连通分量内Dfs,标记每 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
随机推荐
- May 23rd 2017 Week 21st Tuesday
Winners are not those who never fail but those who never quit. 成功者不是从不失败,而是从不放弃. Nothing is impossib ...
- HXXXES 高可用双机RMAN异地备份 Notes
一.总览 大致上的逻辑如上图,简化细节来归纳,便是 用一个bat脚本来驱动整个备份过程. 二.一些准备工作 1.为备份所需的脚本,以及最终备份生成的文件创建目录 开始=>运行=> ...
- 让ADO.NET Entity Framework 支持ACCESS数据库
如写的不好请见谅,本人水平有限. 个人简历及水平:. http://www.cnblogs.com/hackdragon/p/3662599.html 接到一个程序和网页交互的项目,用ADO.NET ...
- 创建maven项目后缺少jar包下载失败等问题
transfer.......fail.........等问题 The container 'Maven Dependencies' references non existing library ' ...
- [19/03/24-星期日] 容器_Collection(集合、容器)之List(表,有顺序可重复)
一. 概念&方法 Collection 表示一组对象,它是集中.收集的意思.Collection接口的两个子接口是List.Set接口. 由于List.Set是Collection的子接口,意 ...
- JDK下载
1.进入Java官网,方式不限,如百度“Java 官网”,www.oracle.com,找到Java SE -> download.链接如下: http://www.oracle.com/tec ...
- XCode项目配置可访问 非 https 接口的方法
打开项目的info.plist文件,右键- open as sourceCode .在代码中添加: <key>NSAppTransportSecurity</key> < ...
- 旧文备份:硬盘MBR引导记录损坏的一种修复方法
硬盘MBR信息损坏原因:硬盘上安装了windows XP和linux双系统,在windows下安装一套软件,破解的时候修改了硬盘的序列号,结果导致引导系统的grub无法完成linux的引导,只能进到w ...
- Tomcat 启动速度优化
创建一个web项目 选择发布到 汤姆猫 的下面 deploy path: 表示发布到的文件名称 把项目添加到 tomcat 里,运行,我们可以在 tomcat里找到我们发布的项目: 现在启动时间: 现 ...
- JavaScript:验证输入
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...