HDU 4612 Warm up 边双联通分量,树宽

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 <cstdio>
#include <string.h>
#include <algorithm>
#include <stack>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int N=200010;
const int M=1000010;
struct Edge
{
    int v,nt,flag;
} edge[M<<2];
int s[N],cnt;
int n,m;
int dfn[N],low[N],dep,vis[N];
int id[N],res;
bool instack[N];
int nbridge,bridge[M][2];
int maxlen;
stack<int>st;

void AddEdge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].nt=s[u];
    edge[cnt].flag=1;
    s[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].nt=s[v];
    edge[cnt].flag=1;
    s[v]=cnt++;
}

void tarjan(int u,int pre)
{
    vis[u]=true;
    dfn[u]=low[u]=++dep;
    st.push(u);
    instack[u]=true;
    for(int i=s[u]; ~i; i=edge[i].nt)
    {
        int v=edge[i].v;
        if(edge[i].flag==false) continue;///如果有重边,那么需要当做一条新的边来处理,因此不选择标记点,而是选择对边标记
        edge[i].flag=edge[i^1].flag=false;
        if(!vis[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]<low[v])
            {
                bridge[nbridge][0]=u;
                bridge[nbridge++][1]=v;
            }
        }
        else if(instack[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        int t;
        do
        {
            id[t=st.top()]=res;
            st.pop();
            instack[t]=false;
        }
        while(t!=u);
        res++;
    }
}
int dfs(int u,int pre)
{
    int tmp=0;
    for(int i=s[u]; ~i; i=edge[i].nt)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        int d=dfs(v,u);
        maxlen=max(maxlen,tmp+d);///当前值+方向最大值求最大值
        tmp=max(tmp,d);///方向最大值
    }
    return tmp+1;
}

int main()
{
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        memset(s,-1,sizeof s);
        cnt=0;
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            AddEdge(u,v);
        }
        memset(vis,0,sizeof vis);
        memset(instack,0,sizeof instack);
        nbridge=0,res=0,dep=0;
        while(!st.empty()) st.pop();
        for(int i=1; i<=n; i++)
            if(!vis[i]) tarjan(i,0);
        memset(s,-1,sizeof s);
        cnt=0;
        for(int i=0; i<nbridge; i++)
        {
            int u=id[bridge[i][0]],v=id[bridge[i][1]];
            AddEdge(u,v);
        }
        maxlen=0;
        dfs(0,-1);
        printf("%d\n",res-1-maxlen);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值