BAPC2018 K kingpin escape

题意:添加最少的边使树中无桥

思路:

结论1:每个度数为一的点至少连一条边

结论2:连接 i 和 i + 叶子节点个数的上取整可以保证不存在只在一颗子树内连边的情况

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 200000 + 100;
const LL maxm = 400000 + 100;
LL T,he[maxn],ver[maxm],ne[maxm],tot,d[maxn];
vector<LL> ve;
void init(){
    memset( he,0,sizeof( he ) );
    tot = 1;
    memset( d,0,sizeof(d) );
}
void add( LL x,LL y ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
void dfs( LL x,LL fa ){
    if( x != T && d[x] == 1 ){
        ve.push_back( x );
    }
    for( LL cure = he[x];cure;cure = ne[cure] ){
        LL y = ver[cure];
        if( y == fa ) continue;
        dfs( y,x );
    }
}
int main(){
    LL n,x,y;
    scanf("%lld%lld",&n,&T);
    init();
    for( LL i = 1;i <= n-1;i++ ){
        scanf("%lld%lld",&x,&y);
        d[x]++; d[y]++;
        add( x,y );
        add( y,x );
    }
    dfs( T,-1 );
    LL ans = ve.size() ;
    if( d[T] == 1 ) ans++;
    ans = (ans + 1) / 2;
    LL cnt = (ve.size()+1) / 2;
    printf("%lld\n",ans);
    for( LL i = 0;i + cnt < ve.size();i++ ){
        printf("%lld %lld\n",ve[i],ve[i+cnt]);
    }
    if( ve.size() % 2 || d[T] == 1 ){
        printf("%lld %lld\n",T,ve[ve.size() /2] );
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值