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分 ...
随机推荐
- sscanf输入总结
2017-08-21 15:09:47 writer:pprp sscanf很好用的,一般配合gets()来使用 /* theme: usage of sscanf writer:pprp date: ...
- Maven 一段时间知识小结
二种打包命令生成后的jar包比较 1.clean install -P dev 2.clean package -Dmaven.test.skip=true -P dev //clean packa ...
- brew 与 nvm
brew 与 nvm 是两个管理软件工具 今天更新了brew结果brew下安装的软件都找不着了.得重新安装,据说brew已经不再更新了.应该是通过github的吧. 结果得重装node与npm,这两 ...
- 分布式系统中的幂等性-zookeeper与dubbo
现如今我们的系统大多拆分为分布式SOA,或者微服务,一套系统中包含了多个子系统服务,而一个子系统服务往往会去调用另一个服务,而服务调用服务无非就是使用RPC通信或者restful,既然是通信,那么就有 ...
- 你可能不知道的mouseover/mouseout mouseenter/mouseleave
mouseover与mouseenter 1. 触发时机 mouseover在被监听的节点与子节点上都会触发 mouseenter只在被监听的节点上触发 本质上是因为mouseenter不能冒泡 2. ...
- Angular----安置物流项目前端框架经验总结
一.架构方面 (一) Angular框架有service .controller层: 在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器) ...
- zookeeper常见错误
1.在注册中心找不到对应的服务 这种错误是服务层代码没有成功注册到注册中心导致,请检查一下你的服务层代码是否添加了@service注解,并且该注解的包一定是com.alibaba.dubbo.conf ...
- SPOJ-394-ACODE - Alphacode / dp
ACODE - Alphacode #dynamic-programming Alice and Bob need to send secret messages to each other and ...
- 字符串方法之-indexOf、lastIndexOf、等等一些方法
1.indexOf():方法可返回某个指定的字符串值在字符串中首次出现的位置(从左往右找). 语法:stringObject.indexOf(searchvalue,fromindex) <sc ...
- Highcharts 树状图(Treemap)
Highcharts 树状图(Treemap) 树状图 series 配置 设置 series 的 type 属性为 treemap ,series.type 描述了数据列类型.默认值为 " ...