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

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/84070098
今日推荐