设为首页 加入收藏

TOP

HDU 4612 Warm up(边双联通求树的直径)
2015-07-20 17:22:11 来源: 作者: 【 】 浏览:3
Tags:HDU 4612 Warm 边双 联通 直径
Problem 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

Source 2013 Multi-University Training Contest 2 题意:加入一条边后的最小桥数。 边双联通缩点求树的直径,将直径两端连接就是减少的最多桥数。 重边问题:注意重边。又有就是要扩栈!!
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
           #include
           
             #include
            
              using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair
             
              pil; const int maxn=200000+100; const int maxm=3000000+100; struct node{ int to,next; bool col;//为桥 }e[maxm],e1[maxm]; int head[maxn],cnt,cntE,cnte; int DFN[maxn],low[maxn]; int s[maxn],instack[maxn]; int idex,top,bridge; int belong[maxn],h[maxn]; int d[maxn],vis[maxn];//为割;删除点后增加的联通快 int n,m; void init() { cnt=top=idex=0; cnte=bridge=cntE=0; CLEAR(head,-1); CLEAR(DFN,0); CLEAR(low,0); CLEAR(instack,0); CLEAR(belong,0); CLEAR(h,-1); CLEAR(vis,0); CLEAR(d,0); } void addedge(int u,int v) { e[cntE].to=v;e[cntE].next=head[u]; e[cntE].col=false;head[u]=cntE++; } void addedge1(int u,int v) { e1[cnte].to=v;e1[cnte].next=h[u]; h[u]=cnte++; } void Tarjan(int u,int pre) { int v; low[u]=DFN[u]=++idex; s[top++]=u; instack[u]=1; int pre_num=0; for(int i=head[u];i!=-1;i=e[i].next) { v=e[i].to; if(v==pre&&!pre_num) { pre_num++; continue; } if(!DFN[v]) { Tarjan(v,u); if(low[u]>low[v]) low[u]=low[v]; if(low[v]>DFN[u])//桥 { bridge++; e[i].col=true; e[i^1].col=true; } } else if(instack[v]&&low[u]>DFN[v]) low[u]=DFN[v]; } if(low[u]==DFN[u]) { cnt++; do{ v=s[--top]; instack[v]=0; belong[v]=cnt; }while(v!=u); } } void dfs(int u,int depth) { d[u]=depth; vis[u]=1; for(int i=h[u];i!=-1;i=e1[i].next) { int v=e1[i].to; if(!vis[v]) dfs(v,depth+1); } } void work() { REPF(i,1,n) if(!DFN[i]) Tarjan(i,-1); REPF(u,1,n) for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(belong[u]!=belong[v]) { addedge1(belong[u],belong[v]); addedge1(belong[v],belong[u]); } } dfs(1,0); int pos=1; for(int i=1;i<=cnt;i++) if(d[i]>d[pos]) pos=i; CLEAR(vis,0); CLEAR(d,0); dfs(pos,0); int ans=0; REPF(i,1,cnt) ans=max(ans,d[i]); printf("%d\n",bridge-ans); } int main() { int u,v; while(~scanf("%d%d",&n,&m)&&(n+m)) { init(); REPF(i,1,m) { scanf("%d%d",&u,&v); if(u==v) continue; addedge(u,v); addedge(v,u); } work(); } return 0; } 
             
            
           
         
        
       
      
     
    
   
  



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇LeetCode―Compare Version Numb.. 下一篇CodeForces 151 B 结构体排序。

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)