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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值