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;
using ll = long long;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> ind(n+1, 0), conn(n+1);
for(int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
ind[a]++;
ind[b]++;
conn[a] = b;
conn[b] = a;
}
vector<int> edges;
for(int i = 0; i <= n; i++) {
if (ind[i] == 1) {
edges.push_back(i);
}
}
int sz = edges.size();
cout << ceil((double) sz / 2) << "\n";
for(int i = 2; i <= sz; i += 2) {
cout << edges[i-1] << " " << edges[i-2] << "\n";
}
if (sz % 2) {
int last = edges[sz-1];
if (conn[last] == 1) {
conn[last] = 2;
} else {
conn[last] = 1;
}
cout << last << " " << conn[last] << "\n";
}
// for(int i )
// for(int i : edges) {
// cout << i << " ";
// }
return 0;
}
//~ check for overflows
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |