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(边—双连通分量)的更多相关文章

  1. 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 ...

  2. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  3. POJ 3352 Road Construction (边双连通分量)

    题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...

  4. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

  5. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  6. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  7. POJ 3352 Road Construction 双联通分量 难度:1

    http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...

  8. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  9. poj 3352 : Road Construction 【ebcc】

    题目链接 题意:给出一个连通图,求最少加入多少条边可使图变成一个 边-双连通分量 模板题,熟悉一下边连通分量的定义.最后ans=(leaf+1)/2.leaf为原图中size为1的边-双连通分量 #i ...

随机推荐

  1. point in polygon algorithm

    Point-In-Polygon Algorithm — Determining Whether A Point Is Inside A Complex Polygon © 1998,2006,200 ...

  2. 深入理解Flink核心技术(转载)

    作者:李呈祥 Flink项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多的人关注Flink项目.本文将深入分析Flink一些关键的技术与特性,希望能够帮助读者 ...

  3. ITL(Interested Transaction List)理解

    一.ITL描述: ITL(Interested Transaction List)是Oracle数据块内部的一个组成部分,位于数据块头(block header),itl由xid,uba,flag,l ...

  4. 把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中

    把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中步骤如下: 把需要合并的文件放到同一个文件夹 在该文件夹中新建一个excel文件 打开新建的excel问价,把鼠标放到sh ...

  5. CSS 换行知多少: word-wrap && word-break && white-space && word-spacing

    word-wrap : 首先提一下,word-wrap 这个 CSS 属性在CSS3中已经被更名为 overflow-wrap,这样语义化也是为了避免与 word-break 混淆: Referenc ...

  6. win10怎样取消电脑自动锁屏

    笔记本经常用着用着就锁屏了 解决方法: 打开控制面板的电源选项,更改“使计算机进入睡眠状态”时间为“从不”.

  7. 【android】 如何把gif图片下载到本地

    以上图片大家可以看到,虽然是个jpg格式的文件,但是本质上是个动图. 但是发现在咱的图片模块下,本地存储的图片只有一帧,问题出在哪里呢? http获取到的byte[]数据是没问题的 断点跟踪了下,发现 ...

  8. spray-json

    spray-json是一个轻量级的,简介的和高效的使用Scala实现的json 它拥有以下特征: 一个简单不可变的模型的json语言元素 一个高效的json解析器 可选择既紧凑又漂亮的json到str ...

  9. Scala List 用法

    1.++[B]   在A元素后面追加B元素 scala> val a = List(1) a: List[Int] = List(1) scala> val b = List(2) b: ...

  10. Django 部署(Apache下)

    前言: 因为需要在服务器下运行python脚本,所以需要搭建Django服务器.所以将自己的学习过程也记录下来,方便日后查阅. 本文环境如下: Ubuntu 16.04  python2.7 Apac ...