题目链接

题意:给出一个连通图,求最少加入多少条边可使图变成一个 边-双连通分量

模板题,熟悉一下边连通分量的定义。最后ans=(leaf+1)/2。leaf为原图中size为1的边-双连通分量

#include<cstdio>
#include<vector>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std; const int maxn=;
int n,m;
vector<int> adj[maxn];
int dfn[maxn],low[maxn];
int time_tag;
int ebccno[maxn],ebcc_cnt;
int size[maxn]; //size[i]用来记录编号为i的ebcc的size
stack<int> st; void init()
{
memset(dfn,,sizeof(dfn));
memset(size,,sizeof(size));
memset(ebccno,,sizeof(ebccno));
time_tag=ebcc_cnt=;
for(int i=;i<=n;i++)
adj[i].clear();
} void dfs(int u,int pre)
{
dfn[u]=low[u]=++time_tag;
st.push(u);
// for(int v:adj[u])
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i];
if(v==pre) continue;
if(!dfn[v])
{
dfs(v,u);
low[u]=min(low[u],low[v]);
}
else
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++ebcc_cnt;
while()
{
int x=st.top();st.pop();
ebccno[x]=ebcc_cnt;
if(x==u) break;
}
}
}
void find_ebcc()
{
for(int i=;i<=n;i++)
if(!dfn[i]) dfs(i,-);
} int solve()
{
int ret=;
for(int u=;u<=n;u++)
// for(int v:adj[u])
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i];
if(ebccno[u]!=ebccno[v]) //边(u,v)为bridge
size[ebccno[u]]++,size[ebccno[v]]++;
}
for(int i=;i<=ebcc_cnt;i++)
if(size[i]==) ret++;
return ret;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
find_ebcc();
int leaf=solve();
printf("%d\n",(leaf+)/);
}
}

poj 3352 : Road Construction 【ebcc】的更多相关文章

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

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

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

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

  3. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

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

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

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

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

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

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

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

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

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

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

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

随机推荐

  1. script标签defer与async的区别

    总结: async 会在文件下载完毕后立即执行 会阻止html parser defer 会下载完以后等html parser结束后执行,保证顺序

  2. python2和python3中split的坑

    执行同样的split,python2和python3截取的行数内容却不一样 我想要截取Dalvik Heap行,使用split('\n')的方法 import os cpu='adb shell du ...

  3. vim加脚本注释和文本加密

    vim /etc/vimrc 一.李导版本 autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()" fun ...

  4. Linux window 安装tomcat各版本下载地址

    1.地址 https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/

  5. DB.JDBC_jar_下载

    1.Download Microsoft JDBC Driver for SQL Server - SQL Server _ Microsoft Docs.html(https://docs.micr ...

  6. IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa

    inet_pton NAME     inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h> in ...

  7. 如何在centos7中显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意字符的文件或目录

    ls /etc |grep "^[^[:alpha:]][[:alpha:]].*"

  8. laravel框架源码分析(一)自动加载

    一.前言 使用php已有好几年,laravel的使用也是有好长时间,但是一直对于框架源码的理解不深,原因很多,归根到底还是php基础不扎实,所以源码看起来也比较吃力.最近有时间,所以开启第5.6遍的框 ...

  9. word定义多级列表

    1.单击开始选项卡里的多级列表按钮,在下拉列表中选择定义新的多级列表 2.先设置第一级编号,选择阿拉伯数字1,2,3,...,并在自动编号“1”的左右分别输入“第”“章”,级别链接到样式选择标题一 3 ...

  10. 【6.10校内test】T1 FBI树

    FBI树[题目链接] 感觉我超废. MY SOLUTION:     我的想法其实也是很简单的,递归的去做,因为最后要求输出FBI的后序遍历,也就是左右头,我的方法是递归存字符数组,(按照与后序遍历完 ...