• HDU 4612 Warm up tarjan 树的直径


    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
    " , bridge - maxv );
    	}
    	return 0;
    }
  • 相关阅读:
    基于百度翻译API开发属于自己的翻译工具
    .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比
    基于uploadify.js实现多文件上传和上传进度条的显示
    浅析Ajax跨域原理及JQuery中的实现分析
    认识SQLServer索引以及单列索引和多列索引的不同
    用VS添加引用dll也会出错?你遇到过吗?
    .NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy
    免费打造自己的个人网站,免费域名、免费空间、FTP、数据库什么的,一个不能少,没钱,也可以这么任性
    【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-6底层驱动之多路开关选择器
    【6集iCore3_ADP触摸屏驱动讲解视频】6-4 底层驱动之SDRAM读写(上)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5373581.html
Copyright © 2020-2023  润新知