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 lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long
vector<int> adj[500'005];
int dist[500'005];
void dfs(int i, int sum, int par)
{
dist[i] = sum;
for (auto nxt : adj[i]) if (nxt != par) dfs(nxt, sum+1, i);
}
signed main()
{
lesgooo;
int n, u, v, sz = 0;
cin >> n;
for (int i = 0; i < n-1; i++) cin >> u >> v, adj[--v].push_back(--u), adj[u].push_back(v);
dfs(0, 0, -1);
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) if (adj[i].size() == 1) sz++, a.push_back({dist[i], i});
sort(a.begin(), a.end());
cout << (sz+1)/2 << endl;
int l = sz/2-1, r = a.size()-1;
while (l >= 0) cout << a[l--].second+1 << " " << a[r--].second+1 << endl;
if (sz&1)
{
int mx = 0, x = 0, h = a[r].second;
dfs(h, 0, -1);
for (int i = 0; i < n; i++) if (i != l && dist[i] >= mx) mx = dist[i], x =i;
cout << h+1 << " " << x+1 << 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... |