hdu 4612 Warm up 双连通+树形dp思想

Warm up

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


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

         题意:求出给定图中所有桥的数量,减去缩点后的最长链,即为题中所求答案(实际不用缩点也可求)

        思路参考于:http://blog.csdn.net/qq172108805/article/details/9564705

#include "stdio.h"   //用到双连通分量和树形dp的思想
#include "string.h"
#pragma comment(linker,"/STACK:102400000,102400000")   //手动扩大栈区(不扩栈会运行错误)

#define N 201000
#define M 1001000

struct node
{
    int x,y;
    bool visit;  //标记该边是否走过
    int next;
}edge[4*M];
int idx,head[N];

inline int MIN(int a,int b){ return a<b?a:b; }

void Init()
{
    idx=0;
    memset(head,-1,sizeof(head));
}
void Add(int x,int y)
{
    edge[idx].x = x;
    edge[idx].y = y;
    edge[idx].visit = false; //开始时所有边都未走过
    edge[idx].next = head[x];
    head[x] = idx++;
}

int n,m;
int sum,temp;
int low[N],dfn[N],time;
int dp1[N],dp2[N];

void DFS(int x)
{
    int i,y;
    dp1[x] = dp2[x] = 0;
    low[x] = dfn[x] = ++time;
    for(i=head[x]; i!=-1; i=edge[i].next)
    {
        if(edge[i].visit) continue;
        y = edge[i].y;
        edge[i].visit = edge[i^1].visit = true;
        if(!dfn[y]) //点y未被访问过
        {
            DFS(y);
            low[x] = MIN(low[x],low[y]);
            if(low[y] > dfn[x])
                sum++;  //当前边为桥,sum++
            temp = dp1[y];
            if(low[y] > dfn[x])
                temp++;
            if(temp > dp1[x])
            {
                dp2[x] = dp1[x];
                dp1[x] = temp;
            }
            else if(temp > dp2[x])
                dp2[x] = temp;
        }
        else
            low[x] = MIN(low[x],dfn[y]);
    }
}

int main()
{
    int i;
    int x,y;
    while(scanf("%d %d",&n,&m),n||m)
    {
        Init();
        for(i=0; i<m; ++i)
        {
            scanf("%d %d",&x,&y);
            Add(x,y);
            Add(y,x);
        }
        sum = 0;  //统计图中桥的条数
        time = 1;
        memset(dfn,0,sizeof(dfn));
        DFS(1);
        int dist=0;   //记录图的双连通分量缩点后的最长直径(最长的桥的长度)(实际不用处理缩点)
        for(i=1; i<=n; ++i) 
        {
            if(dist<dp1[i]+dp2[i])  //dp1[i]+dp2[i]为经过点i的最长路径的长度
                dist = dp1[i]+dp2[i];
        }
        printf("%d\n",sum - dist);//所有桥的条数减最长路径的桥数,即为答案
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值