题意:添加最少的边使树中无桥
思路:
结论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;
}