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 ...
随机推荐
- log4cpp简单示例
log4cpp简单示例 下载地址 Sample.cpp #include <iostream> #include <log4cpp/FileAppender.hh> #incl ...
- 观 浅谈HTTP中Get与Post的区别
看完解决了一些模糊不清的.错误的理解.
- MapReduce学习笔记
一.MapReduce概述 MapReduce 是 Hadoop 的核心组成, 是专用于进行数据计算的,是一种分布式计算模型.由Google提出,主要用于搜索领域,解决海量数据的计算问题. MapRe ...
- Data striping
条带化是把连续的数据分割成相同大小的数据块,把每段数据分别写入到阵列中的不同磁盘上的方法. 当多个进程同时访问一个磁盘时,可能会出现磁盘冲突.大多数磁盘系统都对访问次数(每秒的 I/O 操作,IOPS ...
- centos下apache安装
./configure --prefix=/usr/local/apache2 --enable-so --enable-proxy --enable-proxy-connect --enable-p ...
- Django orm 中 python manage.py makemigrations 和 python manage.py migrate 这两条命令用途
生成一个临时文件 python manage.py makemigrations 这时其实是在该app下建立 migrations目录,并记录下你所有的关于modes.py的改动,比如0001_ini ...
- mysql监控优化(二)主从复制
复制解决的基本问题是让一台服务器的数据和其他服务器保持同步.一台主服务器的数据可以同步到多台从服务器上.并且从服务器也可以被配置为另外一台服务器的主库.主库和从库之间可以有多种不同的组合方式. MyS ...
- Linux系统的SVN客户端使用技巧详解
忽略文件/文件夹 假设想忽略文件temp,cd到temp所在的目录下: $ svn propedit svn:ignore . [注意:请别漏掉最后的点(.表示当前目录),如果报错请看后面] 打开的文 ...
- php微信支付回调验证
//字典排序拼接字符串 function getWxPaySignature($arr){ ksort($arr); $str = ''; foreach ($arr as $k=>$a){ $ ...
- NAND flash阵营ToggleDDR和ONFI
NAND 闪存:目前闪存制造厂主要分为三星与东芝.海力士联合的ToggleDDR阵营和英特尔与美光为首的ONFI阵营 IM Flash Technologies(IMFT):由Intel和Micron ...