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;
random_device rd;
mt19937 rnd(rd());
#define pt pair<int, int>
#define x first
#define y second
#define what_is(x) cerr << #x << " is " << x << endl;
#define ok cerr << "ok" << endl;
#define endl '\n'
#define int long long
#define ld long double
const int N = 5e5 + 5;
vector<int> g[N];
signed main()
{
cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0, x, y; i < n - 1; ++i)
{
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> leaves;
for (int i = 0; i < n; ++i)
if (g[i].size() == 1)
leaves.push_back(i + 1);
int c = leaves.size();
cout << (c + 1) / 2 << endl;
for (int i = 0; i + 2 < c; i += 2)
cout << leaves[i] << " " << leaves[i + 1] << endl;
if (c % 2)
cout << leaves[0] << " " << leaves.back() << endl;
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... |