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值,如果相 ...
随机推荐
- CentOS7_装机软件推荐
目录 目录 前言 CentOS7中文支持 Install Yum Fastest Mirror Plugin Install Flash Player Install NTFS-3G Install ...
- HTTP学习记录:二、请求方法
学习资源主要为:@小坦克HTTP相关博客 最基础的是4种,GET.POST.PUT.DELETE对应着对资源的查.改.增.删.最常用的是GET和POST. GET一般用于获取/查询资源信息: POST ...
- php7.2 下安装swoole扩展
git clone git@github.com:swoole/swoole-src.git phpize ./configure make && make test make ins ...
- C# Hook 方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- linux 正则表达式 元字符
\b 单词边界 \bcool\b 只匹配cool字符串 [root@MongoDB ~]# cat test.txt i am mike1 i am mike i am mike12 匹配有mike ...
- Java IO NIO详细讲解
1.IO Java IO概述 2.NIO Java NIO浅析
- Spring框架学习总结
一.Spring概述 1.什么是Spring? Spring是一个优秀轻量级的框架,是Java中使用最多的框架,Spring框架具有轻量.控制反转.面向切面.容器.框架.MVC的特点. 2.Sprin ...
- 通过编写串口助手工具学习MFC过程——(六)添加Edit编辑框控件
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- CPU与GPU,我们应该使用哪个?
CPU与GPU,我们应该使用哪个? CPU与GPU CPU即中央处理器,GPU即图形处理器. 两者的相同之处:两者都有总线和外界联系,有自己的缓存体系,以及数字和逻辑运算单元 两者的区别之处:在于存在 ...
- nmblookup - 基于TCP/IP上的NetBIOS客户用于查询NetBIOS名字的程序
总览 SYNOPSIS nmblookup [-M] [-R] [-S] [-r] [-A] [-h] [-B <broadcast address>] [-U <unicast a ...