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;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
const int mxn = 5e5 + 10;
int deg[mxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int f, t;
cin >> f >> t;
deg[f]++;
deg[t]++;
}
vector<int> leafs;
for (int i = 1; i <= n; i++) if (deg[i] == 1) leafs.push_back(i);
int sz = leafs.size();
vector<pair<int, int>> v;
for (int i = 0; i < sz / 2; i++) v.push_back({leafs[i], leafs[i + sz / 2]});
if (sz % 2 != 0) v.push_back({leafs[0], leafs.back()});
cout << v.size() << endl;
for (auto x : v) cout << x.first << " " << x.second << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |