poj3177--Redundant Paths(边的双连通)
有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。
一个连通无向图最少加几条边变成双连通图
(缩点后叶子节点(即度数为1)的个数 + 1) / 2
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
/*
一个连通无向图最少加几条边变成双连通图
边的双连通
(缩点后叶子节点(即度数为1)的个数 + 1)/2
*/ const int N = 5005;
const int M = 200005; struct Edge {
int to, next;
bool cut;
} edge[M];
int cnt_edge;
int head[N];
void add_edge(int u, int v)
{
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
edge[cnt_edge].cut = false;
head[u] = cnt_edge++;
} int dfn[N]; int idx;
int low[N];
int stk[N]; int top;
int kind[N]; int cnt;
bool in[N]; int bridge; int n, m; void tarjan(int u, int pre)
{
low[u] = dfn[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -1; 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])
{
bridge++;
edge[i].cut = true;
edge[i ^ 1].cut = true;
}
}
else low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
cnt++;
int v;
do {
v = stk[top--];
in[v] = false;
kind[v] = cnt;
} while (u != v);
}
} int deg[N];
void solve()
{
for (int u = 1; u <= n; ++u)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
if (edge[i].cut) deg[kind[u]]++;
}
}
int leaf = 0;
for (int i = 1; i <= cnt; ++i)
if (deg[i] == 1) ++leaf;
printf("%d\n", (leaf + 1) / 2);
} void init()
{
memset(head, -1, sizeof head);
memset(dfn, 0, sizeof dfn);
memset(deg, 0, sizeof deg); top = idx = cnt = cnt_edge = 0;
} int main()
{
int a, b;
while (~scanf("%d%d", &n, &m))
{
init();
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &a, &b);
add_edge(a, b);
add_edge(b, a);
}
tarjan(1, -1);
solve();
}
return 0;
}
poj3177--Redundant Paths(边的双连通)的更多相关文章
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- POJ3177 Redundant Paths 双连通分量
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- POJ3177 Redundant Paths —— 边双联通分量 + 缩点
题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ3177 Redundant Paths【双连通分量】
题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...
- POJ3177:Redundant Paths(并查集+桥)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19316 Accepted: 8003 ...
- POJ3177 Redundant Paths 图的边双连通分量
题目大意:问一个图至少加多少边能使该图的边双连通分量成为它本身. 图的边双连通分量为极大的不存在割边的子图.图的边双连通分量之间由割边连接.求法如下: 求出图的割边 在每个边双连通分量内Dfs,标记每 ...
随机推荐
- Android全部权限详解(manifest.xml)
当我们在写android程序时有很多功能都要在androidmanifest.xml中加入权限申明才能正常使用,下面就把所有的权限介绍一下: android.permission.ACCESS_CHE ...
- 10 Best Responsive HTML5 Frameworks and Tools
http://designinstruct.com/roundups/html5-frameworks/
- BeeFramework
A semi-hybrid framework that allows you to create mobile apps using Objective-C and XML/CSS
- 使用PyInstaller打包Python程序
本文转载自: http://www.pycoding.com/2015/04/23/pyinstaller.html
- no identities are available for signing
原地址:http://www.cnblogs.com/imzzk/p/3501868.html 今天将做好的app提交到app store,结果就出现标题上的错误.“No identities are ...
- Python PIL库之Image注解(API)
http://blog.csdn.net/xiezhiyong3621/article/details/8499543 class Image Methods defined here: __geta ...
- Xplico
http://zhulinu.blog.51cto.com/539189/850909
- [JAVA]HDU 4919 Exclusive or
题意很简单, 就是给个n, 算下面这个式子的值. $\sum\limits_{i=1}^{n-1} i \otimes (n-i)$ 重点是n的范围:2≤n<10500 比赛的时候 OEIS一下 ...
- html5 动画精灵
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- IPv6 tutorial – Part 6: Site-local addresses and link-local addresses
https://4sysops.com/archives/ipv6-tutorial-part-6-site-local-addresses-and-link-local-addresses/ In ...