Warm up

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=4612

Description

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

Hint

题意

让你加一条边,使得这个图的桥数量最小

输出桥的数量

题解:

先缩点,然后求一个直径,然后输出树边数减去直径就好了

这样显然最小

代码

#include <bits/stdc++.h>

using namespace std;
const int maxn = 200000 + 500;
struct edge{
int v , nxt;
}e[2005000];
int head[maxn] , tot , n , m , dfn[maxn] , low[maxn] , dfs_clock , bridge , dis[maxn] , vis[maxn] , Ftp , belong[maxn] ;
stack < int > sp;
vector < int > G[maxn];
void link(int u , int v){ e[tot].v=v,e[tot].nxt=head[u],head[u]=tot++;} void dfs(int x , int pre){
dfn[x] = low[x] = ++ dfs_clock; sp.push( x );
for(int i = head[x] ; ~i ; i = e[i].nxt ){
if( (i ^ 1) == pre ) continue;
int v = e[i].v;
if(!dfn[v]){
dfs( v , i );
low[x] = min( low[x] , low[ v ] );
if(low[v] > dfn[x]) bridge ++ ;
}else low[x]=min(low[x],dfn[v]);
}
if( low[x] == dfn[x] ){
++ Ftp;
while(1){
int u = sp.top() ; sp.pop();
belong[u] = Ftp;
if( u == x ) break;
}
}
} queue < int > Q; int solve(){
vis[1] = 1 ;
Q.push( 1 );
while(!Q.empty()){
int s = Q.front() ; Q.pop();
for(auto it : G[s]){
if( vis[it] == 0 ){
vis[it] = 1 ;
dis[it] = dis[s] + 1;
Q.push( it );
}
}
}
int s = -1 , index = 0;
for( int it = 1 ; it <= Ftp ; ++ it ){
if(dis[it] > s){
s = dis[it];
index = it;
}
}
for(int i = 1 ; i <= Ftp ; ++ i) dis[i] = vis[i] = 0;
Q.push( index );
vis[index] = 1;
while(!Q.empty()){
int s = Q.front() ; Q.pop();
for(auto it : G[s]){
if( vis[it] == 0 ){
vis[it] = 1 ;
dis[it] = dis[s] + 1;
Q.push( it );
}
}
}
s = 0;
for( int it = 1 ; it <= Ftp ; ++ it ){
if(dis[it] > s){
s = dis[it];
}
}
return s;
} int main(int argc,char *argv[]){
while(scanf("%d%d",&n,&m)){
if( n == 0 && m == 0 ) break;
for(int i = 1 ; i <= n ; ++ i) head[i] = -1 , dfn[i] = low[i] = vis[i] = dis[i] = 0 , G[i].clear();
tot = dfs_clock = bridge = Ftp = 0 ;
for(int i = 1 ; i <= m ; ++ i){
int u , v ;
scanf("%d%d",&u,&v);
link( u , v );
link( v , u );
}
for(int i = 1 ; i <= n ; ++ i) if(!dfn[i]) dfs( i , 1 << 30 );
for(int i = 1 ; i <= n ; ++ i)
for(int j = head[i] ; ~j ; j = e[j].nxt){
int v = e[j].v;
if(belong[i] == belong[v]) continue;
G[belong[i]].push_back( belong[v] );
G[belong[v]].push_back( belong[i] );
}
int maxv = solve();
printf("%d\n" , bridge - maxv );
}
return 0;
}

HDU 4612 Warm up tarjan 树的直径的更多相关文章

  1. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

  2. F - Warm up - hdu 4612(缩点+求树的直径)

    题意:有一个无向连通图,现在问添加一条边后最少还有几个桥 分析:先把图缩点,然后重构图为一棵树,求出来树的直径即可,不过注意会有重边,构树的时候注意一下 *********************** ...

  3. HDU 4612 Warm up tarjan缩环+求最长链

    Warm up Problem Description   N planets are connected by M bidirectional channels that allow instant ...

  4. HDU 4612 Warm up(Tarjan)

    果断对Tarjan不熟啊,Tarjan后缩点,求树上的最长路,注意重边的处理,借鉴宝哥的做法,开标记数组,标记自己的反向边. #pragma comment(linker, "/STACK: ...

  5. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  6. 【HDU 4612 Warm up】BCC 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...

  7. hdu 4612 Warm up 有重边缩点+树的直径

    题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tot ...

  8. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  9. HDU 4612 Warm up(双连通分量缩点+求树的直径)

    思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...

随机推荐

  1. Jsoup爬取带登录验证码的网站

    今天学完爬虫之后想的爬一下我们学校的教务系统,可是发现登录的时候有验证码.因此研究了Jsoup爬取带验证码的网站: 大体的思路是:(需要注意的是__VIEWSTATE一直变化,所以我们每个页面都需要重 ...

  2. ubuntu14.04安装使用NviDIA显卡驱动

    想给自己的ubuntu换N卡驱动的原因: 一方面,由于自己电脑在编译源代码8线程全开(make -j8)时,CPU温度呼呼涨到八九十度,从而常常导致系统保护自动关机,让人有点不爽.网上有说ubuntu ...

  3. 设计模式之笔记--命令模式(Command)

    命令模式(Command) 定义 命令模式(Command),将一个请求封闭为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排除或记录请求日志,以及支持可撤销的操作. 类图 描述 Comm ...

  4. C/C++——[03] 注释

    C/C++源程序中被注释的内容不能被编译,被认为是不属于程序的一部分. C/C++的注释有两种写法: 多行注释:以 “ /*”开头,以“ */”结尾: #include <stdio.h> ...

  5. uWSGI+Nginx+Flask在Linux下的部署

    搞了一天多,终于搞通了uWSGI的部署原理,下面总结一下遇到的一些坑,希望给读者能够少走弯路.        简单来说,uWSGI是一个web服务器,Nginx进行反向代理的其实跟这些服务器可以说没有 ...

  6. Myeclipse编辑jsp文件很卡是什么原因?

    可能是配置问题,配置的时候不要把myeclipse连接到网络.否则每次编辑的时候要在网上查找,所以照成很卡.window->perferences->java->Installed ...

  7. ireport报表,打印时,报表加载失败的解决方法

    1.报表加载失败图示 2.解决方法 原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:http://www.cnblogs.com/dsh ...

  8. Kiggle:Digit Recognizer

    题目链接:Kiggle:Digit Recognizer Each image is 28 pixels in height and 28 pixels in width, for a total o ...

  9. numpy,看不懂,但很利害

    慢慢有感觉.. 截几个图看看. import numpy as np np.random.seed(42) complex_numbers = np.random.random(5) + 1j * n ...

  10. netty 基础知识

    http://my.oschina.net/bieber/blog/406799 线程模型 http://hongweiyi.com/2014/01/netty-4-x-thread-model/ h ...