N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels. 
  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缩点的更多相关文章

  1. poj3352Road Construction 边双连通+伪缩点

    /* 对于边双连通分支,求法更为简单. 仅仅需在求出全部的桥以后,把桥边删除.\ 原图变成了多个连通块,则每一个连通块就是一个边双连通分支. 桥不属于不论什么 一个边双连通分支,其余的边和每一个顶点都 ...

  2. 图论--边双连通V-DCC缩点

    // tarjan算法求无向图的割点.点双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...

  3. 图论--双连通E-DCC缩点模板

    // tarjan算法求无向图的桥.边双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...

  4. HDU4612(Warm up)2013多校2-图的边双连通问题(Tarjan算法+树形DP)

    /** 题目大意: 给你一个无向连通图,问加上一条边后得到的图的最少的割边数; 算法思想: 图的边双连通Tarjan算法+树形DP; 即通过Tarjan算法对边双连通缩图,构成一棵树,然后用树形DP求 ...

  5. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  6. hdu 4612 Warm up 双连通缩点+树的直径

    首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...

  7. POJ 3177 Redundant Paths (边双连通+缩点)

    <题目链接> <转载于 >>>  > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...

  8. POJ - 3177 Redundant Paths (边双连通缩点)

    题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...

  9. tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)

    基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...

随机推荐

  1. C#之多线程

    多线程在C#中使用得非常频繁,线程之间的充分利用显得尤为重要,一般的写法都是得不到充分利用资源,本人针对多线程写了一种方法,可以充分利用资源,保证每次同时启动10条线程,现在执行完马上再启动一条,总之 ...

  2. Session存储

    session其实分为服务器端Session和客户端Session. 当用户首次与Web服务器建立连接的时候,服务器会给用户分发一个sessionid作为标识.用户每次提交页面,浏览器都会把这个ses ...

  3. gcc,gdb,make学习

    实例学习gcc+gdb+make程序编译.链接.运行时头文件或动态链接库的查找 分四步: 预处理.编译.汇编.链接4steps:preprocess,compile,assemble,link ​

  4. 利用python 模块读取csv文件信息

    还有一个比较简单的方法 # -*- coding=utf-8 -*- import pandas as pddf = pd.read_csv("20170320094630.csv" ...

  5. centos7里没有ifcfg-eth0只有 ifcfg-ens33(没有Eth0网卡)

    https://www.cnblogs.com/feixiangtk/p/6819118.html CentOS7系统安装完毕之后,输入ifconfig命令发现没有eth0,不符合我们的习惯.而且也无 ...

  6. 明确出需求 然后开会评审 要什么接口 接口参数、返回json内容、格式 协定好 在做

     明确出需求 然后开会评审 要什么接口 接口参数.返回json内容.格式 协定好 在做 

  7. 【Demo】jQuery 设置简单的分页

    功能描述: (1)设定起始页位置和每页的大小: (2)绑定自定义的分页事件,在事件中隐藏当前页不需要显示的行: (3)为表格添加页链接: (4)绑定链接的单击事件,在事件中触发表格分页事件. 效果图: ...

  8. 转:HDFS运行原理

    简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...

  9. jq中的$.post中方法

    jQuery.post( url, [data], [callback], [type] ) : 使用POST方式来进行异步请求 参数: url (String) : 发送请求的URL地址. data ...

  10. CF Round #456 (Div. 2)

    这时我第一次打CF 然后一看t1 哇好水 然后秒A了 看B 哇好像也很水 然后A了 看了C 不会... 然后去看D 似乎概率 然后推了一下,退出来了 然后看E 不会... 接着问了半个小时怎么hack ...