题目链接

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

模板题,熟悉一下边连通分量的定义。最后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. 阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门

    H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...

  2. centos7安装java JDK

    Java环境 1.下载jdk(用FileZilla或xshell工具连接服务器后上传到需要安装的目录) 在 /opt/deploy 下新建 java 文件夹: # mkdir / opt/deploy ...

  3. 社工 - By源码托管平台|云盘 - GitHub - 汇总

    搜索规则 ()默认搜索是从master分支搜索代码 ()只有小于384k的代码才是可以搜索到的 ()搜索语句不能有特殊字符如. , : ; / \ ` ' " = * ! ? # $ &am ...

  4. 【Qt开发】事件循环与线程 一

    事件循环与线程 一 初次读到这篇文章,译者感觉如沐春风,深刻体会到原文作者是花了很大功夫来写这篇文章的,文章深入浅出,相信仔细读完原文或下面译文的读者一定会有收获. 由于原文很长,原文作者的行文思路是 ...

  5. iScroll使用参考

    分享是传播.学习知识最好的方法 以下这篇文章是iScroll.js官网的中文翻译,尽管自己英文不好,但觉得原作者们翻译的这个资料还是可以的,基本用法介绍清楚了.如果你英文比较好的话,可以看看官网的资料 ...

  6. CentOS7 源进源出

    echo 200 ct >> /etc/iproute2/rt_tablesecho 201 cu >> /etc/iproute2/rt_tables ip route ad ...

  7. linux之网卡绑定

    1 什么是网卡绑定 将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡.直接给两块网卡设置同一IP地址是不可以的.通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的 ...

  8. JAVA获取当前系统时间System.currentTimeMillis()以及获取运行时间

    System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...

  9. 区间gcd

    http://codeforces.com/problemset/problem/914/D 题意:给你n个数,两种操作:1.询问区间[l,r]在至多一次修改一个数的条件下区间gcd是否等于x. 2. ...

  10. HNUSTOJ-1698 送外卖(TSP问题 + 状态压缩DP)

    1698: 送外卖 时间限制: 1 Sec  内存限制: 128 MB提交: 123  解决: 28[提交][状态][讨论版] 题目描述 在美团和饿了么大行其道的今天,囊中羞涩的小周和小美,也随大流加 ...