이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
vector<vector<int>> graph;
vector<int> leaves; // indeksite na posetenite listovi
int leaf_count, res_count; // brojot na listovi i brojot na potrebni vrski
// Rekurentno se dodavaat listovite so DFS
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);
}
// Glavnata funkcija
void run() {
int n;
cin >> n;
// Se spravuvame so posebni sluchai
if (n == 1) {
cout << 0;
return;
}
if (n == 2) {
cout << "1\n2";
return;
}
// Go inicijalizirame drvoto
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);
}
// Go broime brojot na listovi i go presmetuvame brojot na potrebni vrski
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);
// Gi dodavame listovite
add_leaves(1, 0);
if (leaf_count & 1)
leaves.push_back(leaves.front());
// Gi pechatime soodvetnite parovi
for (int i = 0; i < res_count; i++)
cout << '\n' << leaves[i] << ' ' << leaves[res_count + i];
}
// Main funkcija
auto main() -> int {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
run();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |