1. 程式人生 > >hdu 4612 Warm up (tarjan+dfs)

hdu 4612 Warm up (tarjan+dfs)

  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

題意:給出一個圖,給你修一條路,求修完這條路後的最小橋數。

思路:先跑一邊tarjan進行縮點,縮完點後的線一定都是橋,這些點連成的一定是一顆樹。想要去掉最多的橋數,也就是連線樹的直徑。也就是(總橋數-樹的直徑)=答案。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
#define M 200010
using namespace std;
struct path
{
    int to,nextt;
} A[2000010];
struct mp
{
    int to,nextt;
} B[2000010];
stack<int>q;
int DFN[M],LOW[M],head[M],book[M],re[M],heab[M],ans[M];
int n,tot,indox,carry,m,x,y,bob,sum;
void add(int u,int v)
{
    A[tot].to=v;
    A[tot].nextt=head[u];
    head[u]=tot++;
}
void abb(int u,int v)
{
    B[bob].to=v;
    B[bob].nextt=heab[u];
    heab[u]=bob++;
}
void init()
{
    indox=carry=tot=bob=sum=0;
    memset(head,-1,sizeof(head));
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    memset(book,0,sizeof(book));
    memset(heab,-1,sizeof(heab));
    memset(ans,-1,sizeof(ans));
}
void tarjan(int u,int p)
{
    int tem;
    DFN[u]=LOW[u]=++indox;
    q.push(u);
    book[u]=1;
    for(int i=head[u]; i!=-1; i=A[i].nextt)
    {
        if(i==p) continue;
        tem=A[i].to;
        if(DFN[tem]==-1)
        {
            tarjan(tem,i^1);
            LOW[u]=min(LOW[tem],LOW[u]);
        }
        else if(book[tem]==1)
        {
            LOW[u]=min(LOW[u],DFN[tem]);
        }
    }
    if(DFN[u]==LOW[u])
    {
        ++carry;
        do
        {
            tem=q.top();
            book[tem]=0;
            re[tem]=carry;
            q.pop();
        }
        while(tem!=u);
    }
    return ;
}
int dfs(int u)
{
    int v;
    for(int i=heab[u]; i!=-1; i=B[i].nextt)
    {
        v=B[i].to;
        if(ans[v]!=-1) continue;
        ans[v]=ans[u]+1;
        dfs(v);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m),n||m)
    {
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        tarjan(1,-1);
        int v;
        for(int i=1; i<=n; i++)//縮點
        {
            for(int j=head[i]; j!=-1; j=A[j].nextt)
            {
                v=A[j].to;
                if(re[i]!=re[v])
                {
                    sum++;
                    abb(re[i],re[v]);
                }
            }
        }
        sum/=2;
        //*******求求樹的直徑*********
        ans[1]=0;
        dfs(1);
        int pan=0,k=0;
        for(int i=1; i<=carry; i++)
        {
            if(ans[i]>pan)
            {
                pan=ans[i];
                k=i;
            }
        }
        memset(ans,-1,sizeof(ans));
        ans[k]=0;
        dfs(k);
        pan=0,k=0;
        for(int i=1; i<=carry; i++)
        {
            if(ans[i]>pan)
            {
                pan=ans[i];
                k=i;
            }
        }
        //*****************************
        printf("%d\n",sum-pan);
    }
}