HDU4612Warm up 边双连通 Tarjan缩点
If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
Note that there could be more than one channel between two planets.
Input The input contains multiple cases.
Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
(2<=N<=200000, 1<=M<=1000000)
Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
A line with two integers '0' terminates the input.Output For each case, output the minimal number of bridges after building a new channel in a line.Sample Input
4 4
1 2
1 3
1 4
2 3
0 0
Sample Output
0
要求:树的直径+缩点
和上一道题纠结了好久,到底怎么加入一个块。
首先要明确边双连通分量和点双连通分量的区别与联系
1.二者都是基于无向图
2.边双连通分量是删边后还连通,而后者是删点
3.点双连通分量一定是边双连通分量(除两点一线的特殊情况),反之不一定,见HDU3749
4.点双连通分量可以有公共点,而边双连通分量不能有公共边
最后补充:
点的双连通存桥(边),每访问一条边操作一次。
边的双连通存割点(点),访问完所有边后操作。
此题是边双连通,所以属于后者。yeah~~·
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
const int maxm=;
int Laxt[maxn],Next[maxm],To[maxm],cnt,vis[maxn];
int dfn[maxn],low[maxn];
int times,ans,cut_cnt,n,m;
int scc[maxn],scc_cnt;
int dis[maxn],S;
int stk[maxn],top,Maxdis,Maxpos;
vector<int>G[maxn];
void _init()
{
memset(Laxt,,sizeof(Laxt));
memset(dfn,,sizeof(dfn));
memset(scc,,sizeof(scc));
memset(vis,,sizeof(vis));
ans=cut_cnt=top=scc_cnt=cnt=times=;
}
void _add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void _tarjan(int u,int v){
dfn[u]=low[u]=++times;
int num_v=;
stk[top++]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==v){
num_v++;
if(num_v==) continue;//保证重边
}
if(!dfn[To[i]]){
_tarjan(To[i],u);
if(low[u]>low[To[i]]) low[u]=low[To[i]];
if(dfn[u]<low[To[i]]) cut_cnt++;//割边,对应缩点后是边
}
else if(dfn[To[i]]<low[u]) low[u]=dfn[To[i]];
}
if(dfn[u]<=low[u]){//割点或者环里面第一个访问到的点(点连通缩点)
G[++scc_cnt].clear();
for(;;){
int tmp=stk[--top];
scc[tmp]=scc_cnt;
if(tmp==u) break;
}
}
}
void _rebuild()//重建无环图
{
for(int i=;i<=n;i++)
for(int j=Laxt[i];j;j=Next[j])
if(scc[i]!=scc[To[j]])
G[scc[i]].push_back(scc[To[j]]);
}
void _dfs(int u)
{
int i,L=G[u].size();
for(int i=;i<L;i++)
if(!dis[G[u][i]]){
dis[G[u][i]]=dis[u]+;
_dfs(G[u][i]);
}
}
void _findR()
{
memset(dis,,sizeof(dis));
dis[]=;S=;Maxdis=;
_dfs();
for(int i=;i<=scc_cnt;i++)
if(dis[i]>dis[S]) S=i;
memset(dis,,sizeof(dis));
dis[S]=;
_dfs(S);
for(int i=;i<=scc_cnt;i++)
if(dis[i]>Maxdis) Maxdis=dis[i];
Maxdis--;
}
int main()
{
int i,j,k,u,v;
while(~scanf("%d%d",&n,&m)){
if(n==&&m==) return ;
_init();
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
_add(u,v);
_add(v,u);
}
for(i=;i<=n;i++)
if(!dfn[i]) _tarjan(i,-);
_rebuild();
_findR();
printf("%d\n",cut_cnt-Maxdis);
}
return ;
}
HDU4612Warm up 边双连通 Tarjan缩点的更多相关文章
- poj3352Road Construction 边双连通+伪缩点
/* 对于边双连通分支,求法更为简单. 仅仅需在求出全部的桥以后,把桥边删除.\ 原图变成了多个连通块,则每一个连通块就是一个边双连通分支. 桥不属于不论什么 一个边双连通分支,其余的边和每一个顶点都 ...
- 图论--边双连通V-DCC缩点
// tarjan算法求无向图的割点.点双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...
- 图论--双连通E-DCC缩点模板
// tarjan算法求无向图的桥.边双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...
- HDU4612(Warm up)2013多校2-图的边双连通问题(Tarjan算法+树形DP)
/** 题目大意: 给你一个无向连通图,问加上一条边后得到的图的最少的割边数; 算法思想: 图的边双连通Tarjan算法+树形DP; 即通过Tarjan算法对边双连通缩图,构成一棵树,然后用树形DP求 ...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- POJ 3177 Redundant Paths (边双连通+缩点)
<题目链接> <转载于 >>> > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
- tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)
基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...
随机推荐
- 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem
2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...
- HighCharts 在IE8下饼图不显示的问题
HighCharts饼图用来做数据统计时,在IE8下发现某些图形不能正确显示出来. 在IE8下面会报 'this.renderer.gradients' 为空或不是对象 这样的错误.. 解决方法: ...
- mac 下如何建立vue-cli项目
command+space : terminal //检测是否存在node node -v //检测是否带有npm安装管理工具 npm -v //具备进行下一步 //在documnets下建立vue文 ...
- (转)浅谈SQL Server 对于内存的管理
简介 理解SQL Server对于内存的管理是对于SQL Server问题处理和性能调优的基本,本篇文章讲述SQL Server对于内存管理的内存原理. 二级存储(secondary storage) ...
- Nginx 启动报错 “/var/run/nginx/nginx.pid" failed”
问题: 重启虚拟机后,再次重启nginx会报错: open() "/var/run/nginx/nginx.pid" failed (2: No such file or dire ...
- guava的事件发布订阅功能
事件的重要性,不用说很重要,在很多时候我们做完一个操作的时候,需要告知另外一个对象让他执行相应操作,比如当用户注册成功的时候,需要抛出一个注册成功的事件,那么有监听器捕获到这个事件,完成后续用户信息初 ...
- 我的json
{ "firstName":[ "xMan" ], "members":[ { "name":"X教授&quo ...
- Java读写文化总结
Java读文件 package 天才白痴梦; import java.io.BufferedReader; import java.io.File; import java.io.FileInputS ...
- springboo 添加logback日志
springboot默认引入logback相关的jar包 1.在 Application.properties里添加 logging.config=classpath:logback-spring.x ...
- Neutron之OVS
OVS即开放虚拟交换标准,不仅仅是为了支持OpenFlow协议,而是为了给虚拟化平台上运行的虚拟机实例提供一套纯软件实现的路由交换协议栈.具体点说,Open vSwitch是在开源的Apache2.0 ...