This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <vector<int>> G(n);
for ( int i = 1; i < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v), G[v].pb(u);
}
vector <int> lf;
auto dfs = [&](auto dfs, int u, int p) -> void{
if ( G[u].size() == 1 ){
lf.pb(u);
}
for ( auto &v: G[u] ){
if ( v != p ){
dfs(dfs, v, u);
}
}
};
dfs(dfs, 0, -1);
if ( lf.size() & 1 ){
lf.pb(lf[0]);
}
int k = lf.size();
cout << k / 2 << ln;
for ( int i = 0; i * 2 < k; i++ ){
cout << lf[i] + 1 << ' ' << lf[i + k / 2] + 1 << ln;
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |