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>
#define int long long
#define fi first
#define se second
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> deg(n + 1);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
deg[a] += 1;
deg[b] += 1;
}
vector<int> leafs; // lol
for (int i = 1; i <= n; i++) {
if (deg[i] == 1) {
leafs.push_back(i);
}
}
if ((int)leafs.size() % 2 == 1) {
for (int i = 1; i <= n; i++) {
if (deg[i] > 1) {
leafs.push_back(i);
break;
}
}
}
vector<pair<int, int>> ans;
for (int i = 0; i < (int)leafs.size(); i += 2) {
ans.push_back({leafs[i], leafs[i + 1]});
}
cout << (int)ans.size() << '\n';
for (auto [a, b] : ans) cout << a << " " << b << '\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... |