POJ3177 Redundant Paths 双连通分量
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.
题解:把F个农场看作点、路看作边构造一个无向图G时,图G不存在桥。
也就是问给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
把每一个双连通分量(内部满足条件)缩为一个点,形成一棵树,加(n+1)/2条边就是双连通了(度为1的点个数为n)
注意:判断两个点是不是同一个双连通分量
1.无重边:low值相等就是同一个双连通分量
2.有重边:bfs结束时出栈的就是同一连通分量,好像有点麻烦
这里加了一个判断,不加重边
#include<stdio.h>
#include <algorithm>
#include <string.h>
#define N 5005
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
struct ed{
int to, next;
}edge[N*];
int head[N], top=;
bool mp[N][N];
int pre[N], low[N], dfs_time, out[N];
void addedge(int u,int v){
edge[top].to = v;
edge[top].next = head[u];
head[u] = top++;
}
void dfs(int u,int father){
low[u] = pre[u] = dfs_time++;
for(int i=head[u];i!=-;i=edge[i].next){
int v = edge[i].to;
if(v == father) continue;
if(!pre[v]){
dfs(v, u);
low[u] = min(low[v], low[u]);
}
else low[u] = min(low[u], pre[v]);
}
}
int main(){
int n, m, t, i, j, a, b;
while(~scanf("%d%d", &n, &m)){
memset(head, -, sizeof(head));
memset(pre,,sizeof(pre));
memset(low, , sizeof(low));
memset(out, , sizeof(out));
memset(mp, false, sizeof(mp));
dfs_time = ;top = ;
for(i=;i<=m;i++){
scanf("%d%d", &a, &b);
if(!mp[a][b]){
mp[a][b] = mp[b][a] = ;
addedge(a, b);
addedge(b, a);
}
}
dfs(,-);
t = ;
for(i=;i<=n;i++)
for(j=head[i];j!=-;j=edge[j].next){
int v = edge[j].to;
if(low[v] != low[i])
out[low[i]]++;
}
for(i=;i<=n;i++)
if(out[i] == )
t++;
printf("%d\n", (t+)/);
}
}
POJ3177 Redundant Paths 双连通分量的更多相关文章
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- poj3177 Redundant Paths 边双连通分量
给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...
- POJ3177 Redundant Paths 图的边双连通分量
题目大意:问一个图至少加多少边能使该图的边双连通分量成为它本身. 图的边双连通分量为极大的不存在割边的子图.图的边双连通分量之间由割边连接.求法如下: 求出图的割边 在每个边双连通分量内Dfs,标记每 ...
- POJ3177 Redundant Paths —— 边双联通分量 + 缩点
题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1. 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
随机推荐
- swift 笔记 (二十) —— 泛型
泛型 泛型是为了解决在针对不同数据类型.而做了同一种功能的操作导致的每一个类型我们都要写一份代码的问题. 有了泛型,我们能够仅仅写一份逻辑代码,而适应于不同的数据类型. func swapInt(in ...
- 【SSRS】入门篇(三) -- 为报表定义数据集
原文:[SSRS]入门篇(三) -- 为报表定义数据集 通过前两篇文件 [SSRS]入门篇(一) -- 创建SSRS项目 和 [SSRS]入门篇(二) -- 建立数据源 后, 我们建立了一个SSRS项 ...
- 把《C语言接口与实现》读薄之第一章:引言
1.1文学程序 文学程序(literate program):接口及其实现的代码与对其进行解释的正文交织在一起.文学程序由英文正文和带标签的程序代码块组成.例如, 〈compute x * y〉≡ s ...
- iOS基础 - 控件属性
一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...
- get 新技能
找usaco各种月赛的数据戳这里:ace.delos.com/NOV06 这里表2006.11的数据,其余的数据同上搜索 月赛题目http://pan.baidu.com/share/link?sha ...
- Vnix项目正式启动
历经3年的学习时间,我从Puppy Linux到各种常见的Linux发行版,从Gentoo Linux再到LFS,期间学会了LiveCD.中文化定制.服务器搭建.Google Key Search.C ...
- iOS关于RunLoop和Timer
RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在.RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop.永远不要手动创建一个run ...
- python study
python django restul webservice返回json数据 2013-09-27 23:14 by lixingle, 249 visits, 网摘, 收藏, 编辑 摘要:做这个d ...
- 使用axis2访问webservice(webserivice基于.net平台实现)
webservice url=http://10.90.11.240:8081/ExceptionWebService.asmx?WSDL: 下载axis2组件,解压,进入bin目录,通过命令wsdl ...
- 支持异步同步的分布式CommandBus MSMQ实现
支持异步同步的分布式CommandBus MSMQ实现 先上一张本文所描述的适用场景图 分布式场景,共3台server: 前端Server Order App Server Warehouse App ...