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 md = 1e6 + 10;
vector < int > adj[md];
vector < int > leaves;
int n;
void dfs(int u, int par) {
if (adj[u].size() == 1)
leaves.push_back(u);
for(auto v : adj[u]) {
if (v != par)
dfs(v, u);
}
}
int main() {
//freopen("test.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
int len = leaves.size();
cout << (len + 1) / 2 << '\n';
for(int i = 0; i < (len + 1) / 2; i++)
cout << leaves[i] << " " << leaves[i + len / 2] << '\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... |