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值,如果相 ...
随机推荐
- k8s架构
master节点 k8s的集群由master和node组成,节点上运行着若干k8s服务. master节点之上运行着的后台服务有kube-apiserver .kube-scheduler.kube- ...
- 【Spring】---属性注入
一.Spring注入属性(有参构造和[set方法]) 注意:在Spring框架中只支持set方法.有参构造方法这两种方法. 使用有参数构造方法注入属性(用的不多,但需要知道): 实体类 package ...
- c# 匿名委托递归
Func<List<int>, int> GetVirtualCode = null; // 递归不能直接=,要赋初值.微软得优化啊,这语法糖不够甜 GetVirtualCod ...
- TS学习笔记----(一)基础类型
布尔值: boolean let isDone: boolean = false; 数字: number 和JavaScript一样,TS里的所有数字都是浮点数. 支持十进制和十六进制字面量,TS还支 ...
- 在线cron表达式生成工具
http://cron.qqe2.com/ 名称 是否必须 允许值 特殊字符 秒 是 0-59 , - * / 分 是 0-59 , - * / 时 是 0-23 , - * / 日 是 1-31 , ...
- argparse命令行传参
import argparse parser = argparse.ArgumentParser(description='manual to this script') # 创建解析器,及其描述 p ...
- python 并发编程 多进程 Process对象的其他属性方法 join 方法
一 Process对象的join方法 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况 情况一: 在主进程的任务与子进程的任务彼此独立的情况下 ...
- [Web 前端] 033 Vue 的简单使用
目录 0. 方便起见,定个轮廓 1. v-model 举例 2. v-for 举例 3. v-if 举例 4. 事件绑定 举例 5. v-show 举例 0. 方便起见,定个轮廓 不妨记下方的程序为 ...
- 【烦人的字符集】linux字符集问题,中文乱码
[1]快速修改命令 [2]locale 查看现在服务器的字符 [root@Master ~]# localeLANG=en_US.UTF-8LC_CTYPE="zh_CN.UTF-8&quo ...
- STM32 晶振 系统时钟8MHZ和72Mhz的原因
首先问题描述: 1.自己画的板子和淘宝买的最小系统板 系统时钟不一致,自己画的是8Mhz,HSE失败:最小系统板72Mhz 2.最小系统板在程序1运行仿真的时候,查看peripherals->P ...