poj 3352 : Road Construction 【ebcc】
题意:给出一个连通图,求最少加入多少条边可使图变成一个 边-双连通分量
模板题,熟悉一下边连通分量的定义。最后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】的更多相关文章
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- 【边双连通】poj 3352 Road Construction
http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- 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(边双连通分量+缩点)
题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...
- POJ 3352 Road Construction 双联通分量 难度:1
http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
随机推荐
- 测开之路一百三十九:会话管理之cookie写入、读取、和更新
机制:服务器端发送的小段文本信息存储在客户端硬盘 功能:记录用户偏好,请求.页面.站点间共享信息 特点:易丢失.安全隐患 添加cookie,需要用到make_respons.set_cookie @a ...
- 函数传参和firture传参数request
前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行.但 ...
- Python学习之==>第三方模块的安装、模块导入
一.模块&包 1.模块 模块实质上就是一个Python文件,它是用来组织代码的.意思就是把Python代码写在里面,文件名就是模块的名称.例如:random.py,random就是模块的名称. ...
- Django聚合数据
背景: 有些时候,光靠数据库中已有字段的数据,还不足以满足一些特殊场景的需求,例如显示一个作者的所有书籍数量. 这时候就需要在已有数据基础上,聚合出这些没有的数据. 为查询集生产聚合: Django ...
- Scratch少儿编程系列:(八)演奏简单音乐
一.程序说明 本程序,用来演奏简单音乐. 二.制作过程 1. 场景和角色的选择 场景选择“音乐和舞蹈”主题下的“party root”,角色沿用默认角色,如下图: 选择后效果如下图: 2. 切换到“脚 ...
- java正则匹配正则表达式
1.简单匹配小案例 public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order wa ...
- dbvisualizer安装
1. 下载DbVisualizer安装包. 2.解压. 无论是哪个版本的dbvisualizer破解版, 都可以找到安装程序(例dbvis_windows-x64_10_0_10.exe), dbvi ...
- centos7yum安装VirtualBox
cd 进入目录:/etc/yum.repos.d 新建一个文件virtualbox.repo, 输入如下内容: [virtualbox] name=Oracle Linux / RHEL / Cent ...
- 背包问题: HDU1114Piggy-Bank
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- SharePoint自己定义程序页面部署 不用重新启动IIS
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/dz45693/article/details/30840255 SharePoint的部署方式默认是 ...