bzoj1718 Redundant Paths(桥,tarjan边双缩点)

132 篇文章 0 订阅

首先tarjan把边双都缩成一个点,这样我们就得到了一棵树。
如何把这棵树加最少的边变成边双呢?

统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 5010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m,h[N],num=1,dfn[N],low[N],dfnum=0,scc,bel[N],du[N],ans=0;
bool used[N<<1];
struct edge{
    int to,next;
}data[N<<2];
stack<int>qq;
inline void tarjan(int x){
    dfn[x]=low[x]=++dfnum;qq.push(x);
    for(int i=h[x];i;i=data[i].next){
        if(used[i>>1]) continue;int y=data[i].to;used[i>>1]=1;
        if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
        else low[x]=min(low[x],dfn[y]);
    }if(dfn[x]==low[x]){
        ++scc;while(1){
            int y=qq.top();qq.pop();bel[y]=scc;
            if(y==x) break;
        }
    }
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=1;i<=m;++i){
        int x=read(),y=read();
        data[++num].to=y;data[num].next=h[x];h[x]=num;
        data[++num].to=x;data[num].next=h[y];h[y]=num;
    }tarjan(1);
    for(int x=1;x<=n;++x)
        for(int i=h[x];i;i=data[i].next){
            int y=data[i].to;if(bel[x]!=bel[y]) ++du[bel[x]];
        }
    for(int i=1;i<=scc;++i) if(du[i]==1) ++ans;
    printf("%d\n",ans+1>>1);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值