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;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 5e5 + 5;
int dep[N];
vector<int> g[N];
int dfs(int v, int p) {
int ret = v;
for (int u : g[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
int t = dfs(u, v);
if (dep[t] > dep[ret]) {
ret = t;
}
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int a = dfs(1, 1);
dep[a] = 0;
int b = dfs(a, a);
vector<pair<int, int>> ans = {{a, b}};
int t = -1;
for (int i = 1; i <= n; ++i) {
if ((int) g[i].size() > 1 || i == a || i == b) {
continue;
}
if (t == -1) {
t = i;
} else {
ans.emplace_back(t, i);
t = -1;
}
}
if (t != -1) {
ans.emplace_back(1, t);
}
cout << ans.size() << '\n';
for (auto [u, v] : ans) {
cout << u << ' ' << v << '\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... |