#include "bits/stdc++.h"
using namespace std;
vector<vector<int>> graph;
vector<int> leaves;
int leaf_count, res_count;
void add_leaves(int node, int parent) {
if (graph[node].size() == 1)
leaves.push_back(node);
for (auto adj : graph[node])
if (adj != parent)
add_leaves(adj, node);
}
void run() {
int n;
cin >> n;
if (n == 1) {
cout << 0;
return;
}
if (n == 2) {
cout << "1\n2";
return;
}
graph = vector<vector<int>>(n + 1);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= n; i++)
if (graph[i].size() == 1)
leaf_count++;
res_count = (leaf_count + 1) / 2;
cout << res_count;
leaves.reserve(2 * res_count);
add_leaves(1, 0);
if (res_count & 1)
leaves.push_back(leaves.front());
for (int i = 0; i < res_count; i++)
cout << '\n' << leaves[i] << ' ' << leaves[res_count + i];
}
auto main() -> int {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
run();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |