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 ...
随机推荐
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- Spring中Resource接口的前缀书写格式
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt"); //这个 ...
- git学习,哇瑟说实话我想要的
1.Git 简介及安装Git是目前世界上最先进的分布式版本控制系统(没有之一).它的诞生也颇具传奇,Linux创始人Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!有兴趣的话, ...
- CSS3学习之radial-gradient(径向渐变)
转自:http://www.cnblogs.com/rainman/p/5133685.html 1.语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“ ...
- Android推送使用--文章集锦
Android之基于百度云推送IM Android实现推送方式解决方案 Android消息推送(一)--AndroidPn(XMPP协议)Demo版到正式上线 采用XMPP协议实现Android推送 ...
- 【poj3415-长度不小于k的公共子串个数】后缀数组+单调栈
这题曾经用sam打过,现在学sa再来做一遍. 基本思路:计算A所有的后缀和B所有后缀之间的最长公共前缀. 分组之后,假设现在是做B的后缀.前面的串能和当前的B后缀产生的公共前缀必定是从前往后单调递增的 ...
- 51nod K 汽油补给 大根堆+小根堆....
题目传送门 用优先队列瞎搞... 想着在每个地方 先算上一个点到这一个点要花费多少钱 这个用小根堆算就好 然后在这个地方加油 把油钱比自己多的替代掉 这个用大根堆维护一下 然后两个堆之间信息要保持互通 ...
- (转)Vim 脚本语言
2012 年 10 月 20 日 by name5566 Categories: Computer Science, Tools 参考文献列表: http://vimdoc.sourceforge.n ...
- Manipulating Files
http://linuxcommand.org/lc3_lts0050.php This lesson will introduce you to the following commands: cp ...
- 【转】针对Android上的ROP攻击剖析
引言 ROP(Return-oriented programming),即“返回导向编程技术”.其核心思想是在整个进程空间内现存的函数中寻找适合指令片断(gadget),并通过精心设计返回 ...