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 <iostream>
#include <vector>
using namespace std;
const int MAX_N = 5 * 1e5;
vector<int> g[MAX_N + 1], leaves;
int n;
void dfs(int u, int p = -1) {
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
if (g[u].size() == 1) {
leaves.push_back(u);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int root = 0;
for (int i = 1; i <= n; i++) {
if (g[i].size() > 1) {
root = i;
}
}
dfs(root);
cout << (leaves.size() + 1) / 2 << "\n";
int cnt = leaves.size() / 2;
for (int i = 0; i < cnt; i++) {
cout << leaves[i] << " " << leaves[i + cnt + leaves.size() % 2] << "\n";
}
if (leaves.size() % 2 == 1) {
cout << root << " " << leaves[cnt] << "\n";
}
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... |