#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define all(x) x.begin(),x.end()
#define int long long
#define ar array
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
template<class T> using ste = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;
namespace FAST {
template<typename T, typename F>
istream &operator>>(istream &cin, pair<T, F> &p) {
cin >> p.first >> p.second;
return cin;
}
template<typename T, typename F>
ostream &operator<<(ostream &cout, pair<T, F> &p) {
cout << p.first << ' ' << p.second;
return cout;
}
template<typename T>
istream &operator>>(istream &cin, vector<T> &a) {
for (T &i: a) cin >> i;
return cin;
}
template<typename T>
ostream &operator<<(ostream &cout, vector<T> &a) {
for (T i: a) cout << i << ' ';
return cout;
}
template<typename T>
istream &operator>>(istream &cin, deque<T> &a) {
for (T &i: a) cin >> i;
return cin;
}
template<typename T>
ostream &operator<<(ostream &cout, deque<T> &a) {
for (T i: a) cout << i << ' ';
return cout;
}
}
using namespace FAST;
const int inf = 3e18 + 7;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;
const int md = 998244353;
int binpow(int a, int b, int m){
if(b == 0)return 1;
if(b % 2 == 0){
int c = binpow(a,b/2,m);
return (c*c)%m;
}
return (binpow(a,b-1,m)*a)%m;
}
int divi(int a, int b, int m){
return (a*(binpow(b,m-2, m)))%m;
}
int n,m,k,q;
vector<int> g[N], dep(N);
vector<ar<int,2>>vs;
void dfs(int x, int pr){
if(g[x].size() == 1){
vs.pb({dep[x], x});
return;
}
for(auto to:g[x]){
if(to == pr)continue;
dep[to] = dep[x] + 1;
dfs(to, x);
}
}
void solve(){
cin >> n;
for(int i = 2;i<=n;i++){
int a,b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
vector<ar<int,2>>u;
int sz = inf;
for(int i = 1;i<=n;i++){
vector<ar<int,2>>ans;
vector<int>op;
for(auto to:g[i]){
dep[to] = 1;
vs.clear();
dfs(to, i);
sort(all(vs));
reverse(all(vs));
// ans.pb({i, vs[0][1]});
for(int i = 1;i<vs.size();i+=2){
ans.pb({vs[i-1][1], vs[i][1]});
}
op.pb(vs[vs.size()-1][1]);
}
for(int i = 1;i<op.size();i+=2){
ans.pb({op[i-1], op[i]});
}
if(op.size() % 2 == 1){
ans.pb({op[op.size()-1], i});
}
if(umin(sz, (int)ans.size())){
u = ans;
}
}
cout << u.size() << "\n";
for(auto [a, b] : u){
cout << a << " " << b << "\n";
}
}
/*
*/
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int tt=1;//cin>>tt;
while(tt--)solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |