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 ...
随机推荐
- redis 笔记(二)
在上一篇中提到了数据类型 ,在本章中就具体说说这几种数据类型: sting :set /get / del / append /strlen 简单的对key-->value 写入读取删除增减 i ...
- 环境变量PATH超长问题[转]
症状回放: 最近安装一个Delphi的控件,结果,在安装之后启动Delphi时出现了找不到相关文件的错误.一开始以为是Delphi内的Library路径没有添加,查看,一切正常.再次启动Delphi, ...
- C++ cin不支持录入空格
如果在C++中,用cin>>str;这种方法来接收字符串那么录入的str不能包含空格,否则它会按照空格将整个字符串切分成若干段.如果你要是想输入带空格的字符串那就要用到getline()这 ...
- HDU 4757 Tree(可持续化字典树,lca)
题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...
- Android进阶笔记10:ListView篇之ListView显示多种类型的条目(item)
ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似. 1. 这是MainActivity,MainActivity的布局就是一个ListView,太简单了这里就不写了,直接 ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- ACM-ICPC(10 / 10)——(完美世界2017秋招真题)
今天学了莫比乌斯反演,竟然破天荒的自己推出来了一个题目!有关莫比乌斯反演的题解,和上次的01分数规划的题解明天再写吧~~~ 学长们都在刷面试题,我也去试了试,120分钟,写出6题要有一点熟练度才行.先 ...
- SPOJ8093【JZPGYZ - Sevenk Love Oimaster】
怎么全是广义后缀自动机,我\(AC\)自动机不服 这道题可以使用的算法很多,\(SA\)或者\(SAM\)应该都可以 但是我都不会 但是这毕竟是一个多串匹配问题,\(AC\)自动机还是可以刚一刚的 我 ...
- kiwi installation
Mainly the installstion methods follow the url: https://github.com/emolch/kiwi/wiki/Installation the ...
- 【洛谷P2216】[HAOI2007]理想的正方形
理想的正方形 [题目描述] 一个a*b的矩阵,从中取一个n*n的子矩阵,使所选矩阵中的最大数与最小数的差最小. 思路: 二维的滑动窗口 对于每行:用一个单调队列维护,算出每个长度为n的区间的最大值和最 ...