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 md = 1e6 + 10;
vector < int > adj[md];
vector < int > leaves;
int n;
void dfs(int u, int par) {
if (adj[u].size() == 1)
leaves.push_back(u);
for(auto v : adj[u]) {
if (v != par)
dfs(v, u);
}
}
int main() {
//freopen("test.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
leaves.push_back(0);
dfs(1, -1);
int len = leaves.size() - 1;
cout << (len + 1) / 2 << '\n';
for(int i = 1; i <= len / 2; i++)
cout << leaves[i] << " " << leaves[len - i + 1] << '\n';
if (len & 1)
cout << leaves[len / 2] << " " << leaves[len / 2 + 1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |