#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define SPEED \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define ins insert
#define fi first
#define se second
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define sz(x) x.size()
#define intt long long
const intt mod = 1e9 + 7;
const intt base = 31;
const intt inf = 1e9;
const intt mxN = 2e5 + 5;
const intt L = 21;
vector<vector<intt>> graph;
vector<intt> v;
void dfs(intt node, intt parent) {
if(graph[node].size() == 1) {
v.pb(node);
}
for(auto u : graph[node]) {
if(u != parent){
dfs(u, node);
}
}
}
void solve() {
intt N;
cin >> N;
graph.assign(N + 1, vector<intt> {});
for(intt i = 0; i < N - 1; i++) {
intt a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs(1, 1);
cout << v.size() / 2 + (v.size() % 2 != 0) << endl;
intt l = 0, r = v.size() - 1;
while(l < v.size() / 2) {
cout << v[l] << " " << v[l+v.size()/2] << endl;
l++;
}
if(v.size() % 2 != 0) {
cout << v[0] << " " << v.back() << endl;
}
}
signed main() {
SPEED;
intt tst = 1;
// cin >> tst;
while (tst--) {
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... |