POJ 3177 Redundant Paths(边双连通分量)
【题目链接】 http://poj.org/problem?id=3177
【题目大意】
给出一张图,问增加几条边,使得整张图构成双连通分量
【题解】
首先我们对图进行双连通分量缩点,
那么问题就转化为给出一棵树,加边使得其成为边双连通分量的最小边数,
只要从叶节点连一条边到任意节点,那么就可以使得这个叶节点加入到双连通分量中,
那么优先叶节点和叶节点连接,所以其答案为(叶节点+1)/2
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=5010,M=10010;
int e[M][2],cut[M],g[N],v[M<<1],nxt[M<<1],ed=1;
int f[N],dfn[N],low[N],num,cnt,from[N],d[N];
void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
void tarjan(int x){
dfn[x]=low[x]=++num;
for(int i=g[x];i;i=nxt[i])if(!dfn[v[i]]){
f[v[i]]=i>>1,tarjan(v[i]);
if(low[x]>low[v[i]])low[x]=low[v[i]];
}else if(f[x]!=(i>>1)&&low[x]>dfn[v[i]])low[x]=dfn[v[i]];
if(f[x]&&low[x]==dfn[x])cut[f[x]]=1;
}
void dfs(int x,int y){
from[x]=y;
for(int i=g[x];i;i=nxt[i])if(!from[v[i]]&&!cut[i>>1])dfs(v[i],y);
}
int n,m;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(g,0,sizeof(g));
memset(d,0,sizeof(d));
memset(from,0,sizeof(from));
memset(f,0,sizeof(f));
memset(cut,0,sizeof(cut));
num=0; ed=1; // 求边双连通分量时,ed一定要为1
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
e[i][0]=u; e[i][1]=v;
add(u,v);add(v,u);
}tarjan(1); cnt=0;
for(int i=1;i<=n;i++)if(!from[i])dfs(i,++cnt);
for(int i=1;i<=m;i++){
if(from[e[i][0]]!=from[e[i][1]]){
d[from[e[i][0]]]++;
d[from[e[i][1]]]++;
}
}int res=0;
//for(int i=1;i<=n;i++)printf("%d %d\n",from[i],d[i]);
for(int i=1;i<=n;i++)if(d[i]==1)res++;
printf("%d\n",(res+1)/2);
}return 0;
}
POJ 3177 Redundant Paths(边双连通分量)的更多相关文章
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
- POJ 3177 Redundant Paths (边双连通+缩点)
<题目链接> <转载于 >>> > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
- POJ 3177 Redundant Paths 边双(重边)缩点
分析:边双缩点后,消环变树,然后答案就是所有叶子结点(即度为1的点)相连,为(sum+1)/2; 注:此题有坑,踩踩更健康,普通边双缩短默认没有无向图没有重边,但是这道题是有的 我们看,low数组是我 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ 3177 Redundant Paths (tarjan边双连通分量)
题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...
随机推荐
- [bzoj 2844]线性基+高斯消元
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2844 又用到线性基+高斯消元的套路题了,因为经过高斯消元以后的线性基有非常好的序关系,所以 ...
- [fzu 2271]不改变任意两点最短路至多删的边数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2271 题目中说每条边的边权都是[1,10]之间的整数,这个条件非常关键!以后一定要好好读题啊…… 做10次循环 ...
- es6快速排序
let qsort = fn =>([x,...xn]) => x == null ? [] : [ ...qsort(fn)(xn.filter(a=>fn(a,x))), x, ...
- 2、Distributed Optimization
一.目录: Distributed dynamic programming (as applied to path-planning problems). Distributed solutions ...
- java replace方法
一:前言 replace自己老是忘记参数是那个替换那个,自己就把replace方法全部给弄了一遍 二:内容 package org.replaceDemo; public class ReplaceD ...
- C#三层中的分页
最近写了一个winform的管理系统,里面的分页同学推荐了几种,感觉都不好用,比较麻烦,自己就找了一个比较简单的分页,利用数据存储过程来分页. reate proc usp_User@pageInde ...
- [POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP
POJ1180 Batch Scheduling Description There is a sequence of N jobs to be processed on one machine. T ...
- BZOJ 1598 牛跑步
牛跑步 [问题描述] BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M ...
- 为什么VS没有提供平win64程序编写项
最近在学习C++和MFC编程,突然有个疑问,为什么每次新建项目时,都只有win32 console application,从来没见过win64的选项,于是去网上查了查,下面是我找到的几个答案: 作者 ...
- loggin
# 参考:https://www.cnblogs.com/DI-DIAO/p/8793136.html BASE_LOG_DIR = os.path.join(BASE_DIR, "log& ...