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;
const int kN = 5e5;
vector<int> g[1 + kN], a;
void dfs(int u, int par) {
if ((int)g[u].size() == 1) {
a.emplace_back(u);
}
for (int v : g[u]) {
if (v != par) {
dfs(v, u);
}
}
}
/*
9
1 2
2 3
2 4
2 5
1 6
1 7
3 8
3 9
*/
void testCase() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
for (int v = 1; v <= n; ++v) {
if ((int)g[v].size() > 1) {
dfs(v, 0);
break;
}
}
int m = (a.size() + 1) / 2;
cout << m << '\n';
int k = a.size() / 2;
for (int i = 0; i < m; ++i) {
cout << a[i] << ' ' << a[(i + k) % a.size()] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |