POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352
题意:
给出一个图,求最少要加多少条边,能把该图变成边—双连通。
思路:
双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相等,说明属于同一个双连通分量。
接下来把连通分量缩点,然后把这些点连边。
对于一棵无向树,我们要使得其变成边双连通图,需要添加的边数 == (树中度数为1的点的个数+1)/2。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std; const int maxn=+; int n,m;
int pre[maxn],isbridge[maxn],bccno[maxn],bcc_cnt,dfs_clock;
int low[maxn]; //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
int degree[maxn];
vector<int> G[maxn],bcc[maxn]; int tarjan(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
int lowv=tarjan(v,u);
lowu=min(lowu,lowv);
/*
if(lowv>pre[u])
isbridge[G[u][i]]=isbridge[G[u][i]^1]=1; //桥
*/
}
else if(pre[v]<pre[u] && v!=fa)
lowu=min(lowu,pre[v]);
}
return low[u]=lowu;
} /*
void dfs(int u)
{
pre[u]=1;
bccno[u]=bcc_cnt;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(isbridge[v]) continue;
if(!pre[v]) dfs(v);
}
}
*/ /*
void find_ebbc()
{
bcc_cnt=dfs_clock=0;
memset(pre,0,sizeof(pre));
memset(isbridge,0,sizeof(isbridge));
memset(bcc,0,sizeof(bcc));
memset(bccno,0,sizeof(bccno));
for(int i=1;i<=n;i++)
if(!pre[i]) tarjan(i,-1); memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++) //计算边—双连通分量
if(!pre[i])
{
bcc_cnt++;
dfs(i);
}
}
*/ int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n && m)
{
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
//find_ebbc();
tarjan(,-);
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
int v=G[i][j];
if(low[i]!=low[v])
degree[low[v]]++;
}
}
int cnt=;
for(int i=;i<=n;i++)
if(degree[i]==) cnt++;
printf("%d\n",(cnt+)/);
}
return ;
}
POJ 3352 Road Construction(边—双连通分量)的更多相关文章
- 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 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- POJ 3352 Road Construction (边双连通分量)
题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...
- poj 3352 Road Construction(边双连通分量+缩点)
题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- POJ 3352 Road Construction 双联通分量 难度:1
http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- poj 3352 : Road Construction 【ebcc】
题目链接 题意:给出一个连通图,求最少加入多少条边可使图变成一个 边-双连通分量 模板题,熟悉一下边连通分量的定义.最后ans=(leaf+1)/2.leaf为原图中size为1的边-双连通分量 #i ...
随机推荐
- 牛B三人组-快速排序-堆排序-归并排序
快速排序 随便取个数,作为标志值,这里就默认为索引位置为0的值 记录左索引和右索引,从右往左找比标志值小的,小值和左索引值交换,右索引变化,然后从左往右找比标志值大的,大值和右索引值交换,左索引变化 ...
- 100个常用的linux命令(转)
原文:http://blogread.cn/it/article/6368?f=wb 1,echo “aa” > test.txt 和 echo “bb” >> test.txt / ...
- 004-Maven的安装与配置
1.在Windows上安装Maven 1.1.检查jdk安装 命令行:echo %JAVA_HOME% java -version 1.2.下载Maven 地址:http://maven.apache ...
- PAT 1051 Pop Sequence[栈][难]
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- java通过URL获取文本内容
原文地址https://www.cnblogs.com/myadmin/p/7634262.html public static String readFileByUrl(String urlStr) ...
- 运维自动化之salt笔记
1:saltstack的基本介绍 2:salt的安装 1:服务端1:安装2:配置文件3:运行4:注意事项2:客户端1:安装2:配置文件3:运行4:注意事项 3:salt的使用: 1:基础知识1:tar ...
- sgu 102 Coprimes 解题报告及测试数据
102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求一个1-10000之间的数 N 的互质 ...
- 微服务—分布式服务追踪sleuth和zipkin
随着业务的发展,系统规模也会越来越大,各微服务间的调用关系也越来越错综复杂. 通常一个客户端发起的请求在后端系统中会经过多个不同的微服务调用来协同产生最后的请求结果, 在复杂的微服务架构系统中,几乎每 ...
- Atom插件无法下载安装解决办法,Atom使用教程,Atom常用插件
使用教程http://wiki.jikexueyuan.com/project/atom/plug-in.html atom通过setting中无法下载插件,通过apm也无法下载插件,可能是网络.co ...
- elasticsearch搜索集群基础架构
1. elasticsearch cluster搭建 http://www.cnblogs.com/kisf/p/7326980.html 为了配套spring boot,elasticsear ...