程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4612 Warm up(邊雙聯通求樹的直徑)

HDU 4612 Warm up(邊雙聯通求樹的直徑)

編輯:C++入門知識

HDU 4612 Warm up(邊雙聯通求樹的直徑)


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 pairpil;
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;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved