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. 挂载xfs磁盘

    # 磁盘初始化 [root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain ...

  2. git学习——<三>git操作

    一.创建仓库 创建一个目录 mkdir repository cd到该目录下,初始化该版本库 git init 至此,版本库创建成功,可以在该文件夹下看到.git文件夹,ls -ah可以看到该文件夹. ...

  3. python重建二叉树

    # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None ...

  4. python定义函数时默认参数注意事项

    如果在调用一个函数时,没有传递默认参数,则函数内的默认参数是对函数的默认参数属性__defaults__的引用, 如 def func(arg1=[]): arg1.append(2) 调用func时 ...

  5. python3.6.1 安装PyQt5,以及配置QTDesigner,PyUIC

    本人主机win10 64,python版本是3.6.1 64 注意python版本一定得是3.6.1 64位的,我原来电脑是安装的32位的,浪费了好长时间 (MMP) 第一步:安装python,自己官 ...

  6. 004-Maven的安装与配置

    1.在Windows上安装Maven 1.1.检查jdk安装 命令行:echo %JAVA_HOME% java -version 1.2.下载Maven 地址:http://maven.apache ...

  7. MFC程序执行过程剖析

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  8. python全栈开发从入门到放弃之面向对象的三大特性

    组合 class Course: def __init__(self,name,period,price): self.name = name self.period = period self.pr ...

  9. __all__方法的作用

    在__all__里面写了谁,到时候就只能用谁,其他的用不了,from 模块 import *时就只能用__all__里的 __all__=['test1','Test'] def test1(): p ...

  10. 关于js中的取值问题

    像这样是获取不到值的,弹出的消息是 underfined:<html><style type="text/css">input { border: 1px ...