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;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef palilo
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int n;
cin >> n;
vector<int> deg(n);
for (int u, v, i = n - 1; i--;) {
cin >> u >> v, --u, --v;
++deg[u], ++deg[v];
}
vector<int> ans;
for (int i = 0; i < n; ++i) {
if (deg[i] == 1) {
ans.emplace_back(i);
}
}
const int leaves = count(deg.begin(), deg.end(), 1);
cout << (leaves + 1) / 2 << '\n';
int prv = leaves & 1
? int(find_if(deg.begin(), deg.end(), [&](const auto& x) { return x != 1; }) - deg.begin())
: -1;
for (int i = 0; i < n; ++i) {
if (deg[i] == 1) {
if (prv == -1) {
prv = i;
} else {
cout << prv + 1 << ' ' << i + 1 << '\n';
prv = -1;
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |