HDU 4612 Warm up

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

这个题会爆栈要加

#pragma comment(linker,"/STACK:102400000,102400000")

思路:强连通+DP , 有了NETWORK那个题的经验,我们知道,
这里写图片描述
比如说这两个图是缩点之后的一棵树,那么对第一个图一定是连接DF,这个会使得割边减少的最多,不难看出,我们减少的割边是某个根下两个较长树边的和,同理第二个是EC;

dp[i][0] 表示 节点I到他的叶子节点的最长深度
dp[i][1] 表示节点I到他的叶子节点的第二长深度

#pragma comment(linker,"/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<stack>

using namespace std;
#define MAXN 200010
#define MAXE 2000010
struct edge
{
    int v, next;
}e[MAXE];

int dfn[MAXN], low[MAXN], vis[MAXN], dp[MAXN][2];
int head[MAXE], cnt, dep, res;

void init( )
{
    memset(vis, 0, sizeof(vis));
    memset(dfn, 0, sizeof(dfn));

    memset(head, -1, sizeof(head));
    cnt = dep = res = 0;
}

void Addedge( int uu, int vv)
{
    e[cnt].v = vv;
    e[cnt].next = head[uu], head[uu] = cnt++;
}

void dfs( int u ,int pre)
{
    vis[u] = 1;
    dfn[u] = low[u] = ++dep;
    dp[u][0] = dp[u][1] = 0;
    for( int i = head[u]; i != -1; i = e[i].next)
    {
        if(i == (pre^1)) continue;
        int v = e[i].v;
        if(!dfn[v])
        {
            dfs(v, i);
             low[u] = min(low[u], low[v]);
            res += (low[v] > dfn[u]);
            //注意下面是用V退出U,利用回溯的特性去更新DP数组
            int tem = dp[v][1] + (low[v] > dfn[u]);
            //tem代表割边的条数,它会把图分成tem+1个块
            if(tem > dp[u][1])
            {
                dp[u][0] = dp[u][1];
                dp[u][1] = tem;
            }
            else if(tem > dp[u][0])
                dp[u][0] = tem;
        }
        else if(vis[v] == 1)
            low[u] = min(low[u], dfn[v]);
    }
    vis[u] = 2;
}
int main()
{
    int n, m, u, v;
    while(scanf("%d %d",&n, &m) != EOF && ( n + m))
    {
        init();
        for( int i = 0; i < m ; i++)
        {
            scanf("%d %d",&u, &v);
            if( u == v) continue;
            Addedge(u, v);
            Addedge(v, u);
        }
        dfs(1, -1);
        int maxn = 0;
         for( int i = 1; i <= n; i++)
            maxn = max(maxn, dp[i][1] + dp[i][0]);

        printf("%d\n",res - maxn);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值