标签:排序 == com auto 根据 int src with clu
就当作是一个结论吧…当要用链覆盖所有的边时,对叶子节点根据dfs序排序后,根据\((i,i+s/2)\)的配对规则进行配对即可,如果有奇数个叶子节点,则将其与根节点相连。
// Created by CAD on 2020/7/13.
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define ll long long
using namespace std;
const int maxn=2e5+5;
vector<int> g[maxn];
int cnt=0;
vector<pii> leaf;
void dfs(int x,int f){
if(g[x].size()==1) leaf.push_back({++cnt,x});
for(auto i:g[x])
if(i==f) continue;
else dfs(i,x);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n-1;++i){
int a,b;cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
int root=1;
for(int i=1;i<=n;++i)
if(g[i].size()>1){
root=i;
break;
}
dfs(root,0);
vector<pii> ans;
int leafsiz=leaf.size();
if(leafsiz%2)
leaf.push_back({++cnt,root}),leafsiz++;
for(int i=0;i<leafsiz/2;++i)
ans.push_back({leaf[i].se,leaf[i+leafsiz/2].se});
cout<<ans.size()<<endl;
for(auto i:ans)
cout<<i.fi<<" "<<i.se<<endl;
return 0;
}
标签:排序 == com auto 根据 int src with clu
原文地址:https://www.cnblogs.com/CADCADCAD/p/13295651.html