POJ 3352 无向图边双连通分量,缩点,无重边
为什么写这道题还是因为昨天多校的第二题,是道图论,HDU 4612。
当时拿到题目的时候就知道是道模版题,但是苦于图论太弱。模版都太水,居然找不到。
虽然比赛的时候最后水过了,但是那个模版看的还是一知半解,主要还是对于无向图缩点不了解。
所以今天特意找了道求无向图边双连通分量,然后缩点的题学习一下,这道题的缩点和昨天那道差不多,唯一的区别就是这是无重边的,那题是有重边的。
先搞掉这个,下午把有重边的缩点搞一下。
这里给出一些概念。具体可以到神牛博客看一下。
边连通度:使一个子图不连通的需要删除掉的最小边数,就是该图的边连通度。
桥(割边) :删除某条边时,该图不再连通,那么这条边就是该图的桥(割边)。
边双连通分量:边连通度大于等于2的子图称为边连通分量。
一个边连通分量里面的任意两点,都有2条或者2条以上的路可以互相到达。
这道题的题意,给出N个点M条边,都是无向的。
然后叫你求,最少增加多少条边,可以是的整个图成为一个边双联通分量 。
思路:求出所有的边连通分量,设数量为cnt,然后将一个边连通分量中的点缩成一个块,然后重新建图,这样我们就得到了一棵节点数为cnt ,边数为cnt - 1,的树。
该树上的所有边都是桥。
然后要使得这个图成为一个边连通分量,那么只需将所有的叶子节点连起来即可。
所有最后的答案就是(叶子节点的个数+ 1) / 2。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2505
#define inf 1<<28
#define LL(x) ( x << 1 )
#define RR(x) ( x << 1 | 1 )
#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define PII pair<int,int>
using namespace std; inline void RD(int &ret) {
char c;
do {
c = getchar();
} while(c < '0' || c > '9') ;
ret = c - '0';
while((c=getchar()) >= '0' && c <= '9')
ret = ret * 10 + ( c - '0' );
}
int n , m ;
struct kdq{
int e , next ;
}ed[111111] ,bridge[1111] ,reed[11111] ;
int head[1111] ,num ,rehead[11111] ,renum ;
int low[1111] ,dfn[1111] ;
int st[11111] ;
int fa[1111] ;
bool vis[1111] ;
int dp ; //tarjan的层数
int top ;//栈顶
int bridgenum ;//桥的数量
int cnt ;//缩点后联通块的数量
//可以知道,cnt = bridge + 1
//缩点后,重新建图,所有节点都是一个联通块,所有的边都是桥。故有上述结论。
void init(){
mem(rehead , -1) ;
renum = 0 ;
mem(head , -1) ;
num = 0 ;
dp = 0 ;
top = 0 ;
bridgenum = 0 ;
cnt = 0 ;
mem(low ,0) ;
mem(dfn ,0) ;
mem(fa,-1) ;
mem(vis, 0 ) ;
}
void add(int s ,int e){
ed[num].e = e ;
ed[num].next = head[s] ;
head[s] = num ++ ;
}
void readd(int s ,int e){
reed[renum].e = e ;
reed[renum].next = rehead[s] ;
rehead[s] = renum ++ ;
}
/***模版求无向图的双联通分量,缩点,求出桥(无重边)***/
void tarjan(int now ,int faa){
dfn[now] = low[now] = dp ++ ;
st[++ top] = now ;
for (int i = head[now] ; ~i ;i = ed[i].next ){
int e = ed[i].e ;
if(e == faa)continue ;
if(dfn[e] == 0){
tarjan(e ,now) ;
if(low[e] < low[now])low[now] = low[e] ;
if(low[e] > dfn[now]){
bridge[bridgenum].e = now ;//桥
bridge[bridgenum ++].next = e ;
cnt ++ ;
do{
fa[st[top]] = cnt ;//缩点
}while(st[top --] != e) ;
}
}else if(low[now] > dfn[e])low[now] = dfn[e] ;
}
}
/***重新建图***/ void rebuild(){
for (int i = 1 ; i <= n ; i ++ ){
for (int j = head[i] ; ~j ; j = ed[j].next){
readd(fa[i], fa[ed[j].e]) ;
readd(fa[ed[j].e] ,fa[i]) ;
}
}
}
int ans = 0 ;
int root = -1 ;
void dfs(int now ,int faa){
vis[now] = 1 ;
int sum = 0 ;
for(int i = rehead[now] ; ~i ;i = reed[i].next){
int e = reed[i].e ;
if(e == faa)continue ;
if(vis[e])continue ;
sum ++ ;
dfs(e,now) ; }
if(!sum)ans ++ ;
}
void solve(){
ans = 0 ;
rebuild() ;
dfs(root ,-1) ;
if(cnt == 1)puts("0") ;
else
printf("%d\n",(ans + 1) / 2) ;
}
int main() {
while(cin >> n >> m){
init() ;
while(m -- ){
int a , b ;
RD(a) ;RD(b) ;
add(a , b) ;
add(b , a) ;
} for (int i = 1 ;i <= n ;i ++ ){//可以处理不连通的图,如果连通的话,这个循环只进行一次。
if(dfn[i] == 0){
top = dp = 1 ;
tarjan(i , -1) ;
++ cnt ;
for (int j = 1 ; j <= n ;j ++ ){//特殊处理顶点的连通块
if(dfn[j] && fa[j] == -1)fa[j] = cnt ,root = cnt;
}
}
}
solve() ; }
return 0 ;
}
POJ 3352 无向图边双连通分量,缩点,无重边的更多相关文章
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- Expm 9_3 无向图的双连通分量问题
[问题描述] 给定一个无向图,设计一个算法,判断该图中是否存在关节点,并划分双连通分量. package org.xiu68.exp.exp9; import java.util.Stack; p ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
- 图论-桥/割点/双连通分量/缩点/LCA
基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
随机推荐
- 02.Lua的数据类型
简单认识Lua 百度了一下(偷哈懒就不自己写了) Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janei ...
- linux 下编译安装php
系统环境: CentOS 6.5 x86_64 下载 PHP 源码包 # wget http://cn2.php.net/distributions/php-5.5.9.tar.bz2 # tar x ...
- python修改txt文件内容
①以r模式打开文件并用readlines方法读入列表l中 ②修改相关行,直接用l[n]形式即可 ③关闭文件 ④以w方式打开文件,用writelines方法写入文件(覆盖文件内容) ⑤关闭文件 需要注意 ...
- laravel实现第三方登录(qq登录)
首先composer安装依赖: composer require socialiteproviders/qq 注册服务提供者(同时注释掉原有的Socialite提供者): 'providers' =& ...
- 模拟Hibernate动态生成SQL语句
这里有一个xml配置文件,也就是Hibernate框架中会用到的POJO和数据库的映射文件 <?xml version="1.0" encoding="utf-8& ...
- python 3.5 格式化字符串输出
#!/usr/bin/env python #encoding: utf-8 #.strip('里面可以去掉字符串中两边的字符') name = input('name :').strip(' ') ...
- GCD 单例
+ (ThemeManager *)sharedInstance { static dispatch_once_t once; //只执行一次 static ThemeManager ...
- ubuntu apt 命令参数(转)
apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. apt-get update 在修改/etc/apt/sou ...
- QEventLoop 的使用两例
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- 哪些产品不用开发原生APP,微信公众号就够了?
最近一阶段H5技术被推到高峰,很多人认为借助H5就能利用微信公众号取代APP原生应用了,而事实是怎么样的?这里我从产品层做一个客观分析. 一,原生APP总体趋势 要谈APP是否会被微信取代,那么必须回 ...