This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of GOD
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 5e5 + 10;
ll n, rt, lf;
ll d[MXN];
vector<ll> adj[MXN], L;
void dfs(ll u, ll par){
if(d[u] == 1){
L.push_back(u), lf ++;
}
for(auto v : adj[u]){
if(v != par) dfs(v, u);
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i < n; i ++){
ll u, v; cin >> u >> v; d[u] ++, d[v] ++;
adj[u].push_back(v), adj[v].push_back(u);
}
for(int i = 1; i <= n; i ++) if(d[i] > 1) rt = i;
dfs(rt, 0);
cout << (lf + 1) / 2 << '\n';
for(int i = 0; i < lf - lf / 2; i ++){
cout << L[i] << ' ' << L[i + lf / 2] << '\n';
}
return 0;
}
// N.N_2004
// THIS IS THE PAYOFF
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |