HDU4612 Warm up 边双联通分量+树的直径

本文探讨了在连接多个行星的运输系统中,通过构建新通道来最小化桥梁数量的问题。通过使用深度优先搜索(DFS)算法两次遍历,解决了如何找到树的直径并据此计算最少桥梁数。

模板题

桥的数量-树的直径

输的直径两次DFS即可


Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2506    Accepted Submission(s): 586


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
 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define MAXN 200100
#define MAXM 9200100

struct node
{
    int from,to,next;
    bool vis;
}edge[MAXM];

int head[MAXN],en;
int n,m,dfnCnt,dfn[MAXN],low[MAXN];
bool jud[MAXM],vis[MAXN];
int stack[MAXN],top,color[MAXN],col;
int bridgeU[MAXN],bridgeV[MAXN],bri;

void add(int a,int b)
{
    edge[en].to=b;
    edge[en].from=a;
    edge[en].next=head[a];
    edge[en].vis=0;
    head[a]=en++;
    edge[en].to=a;
    edge[en].from=b;
    edge[en].next=head[b];
    edge[en].vis=0;
    head[b]=en++;
}

void dfs(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=++dfnCnt;
    stack[++top]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(edge[i].vis) continue;
        edge[i].vis=edge[i^1].vis=1;
        if(!vis[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])
            {
                int ID=i/2+1;
                jud[ID]=1;
                bridgeU[bri]=u;
                bridgeV[bri++]=v;
            }
        }
        else
           low[u]=min(low[u],dfn[v]);
    }

    if(low[u]==dfn[u])
    {
        col++;
        do
        {
            color[stack[top]]=col;
        }
        while(stack[top--]!=u);
    }
}

void tarjan()
{
    bri=0;col=0;
    memset(vis,0,sizeof(vis));
    memset(jud,0,sizeof(jud));
    memset(color,-1,sizeof(color));
    for(int i=1;i<=n;i++)
    {
        dfnCnt=0;top=0;
        if(!vis[i])
        {
            dfs(i);
        }
    }
}


int dist[MAXN];
int head2[MAXN];

void add2(int a,int b)
{
    edge[en].to=b;
    edge[en].from=a;
    edge[en].next=head2[a];
    head2[a]=en++;
    edge[en].to=a;
    edge[en].from=b;
    edge[en].next=head2[b];
    head2[b]=en++;
}


void DFS(int len,int fa,int now)
{
    dist[now]=len;
    for(int i=head2[now];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v!=fa) DFS(len+1,now,v);
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0) break;
        memset(head,-1,sizeof(head));en=0;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        tarjan();
        if(bri==0)
        {
            printf("0\n");
            continue;
        }
        memset(head2,-1,sizeof(head2));
        for(int i=0;i<bri;i++)
        {
            int u=bridgeU[i];
            int v=bridgeV[i];
            if(color[u]==color[v]) continue;
            add2(color[u],color[v]);
        }
        DFS(0,-1,1);
        int maxn=0;int kk;
        for(int i=1;i<=col;i++)
        {
            if(dist[i]>maxn)
            {
                maxn=dist[i];
                kk=i;
            }
        }
        DFS(0,-1,kk);
        maxn=0;
        for(int i=1;i<=col;i++)
        {
            if(dist[i]>maxn)
            {
                maxn=dist[i];
            }
        }
        printf("%d\n",bri-maxn);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值