This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*\ In The Name Of GOD
* Beyrad :D
\*/
#include "bits/stdc++.h"
#define sz(x) ((int) (x).size())
using namespace std;
const int N = 5e5 + 11;
int st[N];
int T = 0;
vector<int> g[N];
void dfs(int u, int p) {
st[u] = T++;
for (int v : g[u]) {
if (v != p)
dfs(v, u);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
int rt = 0;
for (int i = 0; i < n; i++)
if (sz(g[i]) >= 2)
rt = i;
dfs(rt, -1);
vector<pair<int, int>> a;
for (int i = 0; i < n; i++)
if (sz(g[i]) == 1)
a.emplace_back(st[i], i);
sort(a.begin(), a.end());
int k = sz(a);
cout << (k + 1) / 2 << '\n';
for (int i = 0; i < k / 2; i++)
cout << a[i].second + 1 << " " << a[i + (k + 1) / 2].second + 1 << '\n';
if (k % 2 == 1)
cout << a[k / 2].second + 1 << " " << rt + 1 << '\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... |