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;
const int mxN = 5e5 + 12;
vector<int> adj[mxN];
vector<int> lisce;
void dfs(int s, int p = -1) {
if(adj[s].size() == 1) lisce.push_back(s);
for(auto e : adj[s]) {
if(e == p) continue;
dfs(e, s);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0);
int vel = lisce.size();
cout << (vel + 1) / 2 << '\n';
for(int i = 0; i < (vel + 1) / 2; ++i) {
int x = lisce[i];
int y = lisce[(i + vel / 2) % vel];
cout << x + 1 << " " << y + 1 << '\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... |