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 ...
随机推荐
- Dynamics CRM GBK编码
Dynamics CRM用文本汉字为条件去请求数据时,传过去的竟然是GBK编码⊙_⊙ 下面这段代码解决了我的问题,这是拷贝自网上一位大神的博客: http://qq100002656.blog.163 ...
- May 11th 2017 Week 19th Thursday
If you fell down yesterday, stand up today. 昨天跌倒了,今天仍然要站起来. From Herbert George Wells. If you fell d ...
- LightOJ-1028 Trailing Zeroes (I)---因子数目
题目链接: https://cn.vjudge.net/problem/LightOJ-1028 题目大意: 一个十进制数1≤n≤1012,现在用base进制来表示,问有多少种表示方法使得最后一位上的 ...
- Codeforces 225E 梅森素数
注:梅森素数,数组表示的是2^n-1的n,指数. #include <stdio.h> #include <math.h> ; ; typedef long long ll; ...
- 【洛谷P4568】[JLOI2011]飞行路线
飞行路线 题目链接 今天上午模拟考试考了原题,然而数组开小了,爆了4个点. 据王♂强dalao说这是一道分层图SPFA的裸题 dis[i][j]表示到点i用k个医疗包的最小消耗,dis[u][j]+e ...
- 【luogu P3385 负环】 模板
题目链接:https://www.luogu.org/problemnew/show/P3385 SPFA判负环. 这个题必须卡一卡才过得去. 按理说对于一个负环点应当是入队 > n次. 但是这 ...
- CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...
- MapFile
MapFile是排序后的SequenceFile, 这个排序是由开发者来保证的, 不是内存实现. 相当于对key作了一个分块索引, 只针对key. 缺点 1.文件不支持复写操作,不能向已存在的Seq ...
- 11java基础继承
一. 继承相关 18.实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hry.test; public class Instrument { ...
- reactJs 基础
react不是一个完整的mvc,mvvm框架. react跟web components 不冲突 背景原理:基于React进行开发时所有的DOM构造都是通过虚拟DOM进行,每当数据变化时,React ...