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>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 5e5 + 5;
vector<int> ad[mxN];
int in[mxN];
vector<int> leafs;
int timer;
void dfs(int u, int p) {
int childs = 0;
in[u] = ++timer;
for (int v : ad[u]) {
if (v == p) continue;
dfs(v, u);
childs++;
}
if (!childs) {
leafs.push_back(u);
}
}
void testCase() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if ((int) ad[i].size() > 1) {
dfs(i, -1);
break;
}
}
sort(leafs.begin(), leafs.end(), [&](int i, int j) { return in[i] < in[j]; });
cout << (int) (leafs.size() + 1) / 2 << "\n";
for (int i = 0; i < (int) (leafs.size() + 1) / 2; i++) {
int j = (int) leafs.size() / 2 + i;
cout << leafs[i] << " " << leafs[j] << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |