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 maxn 500010
int n;
vector<vector<int>> adj(maxn);
vector<int> leaves;
void dfs(int u, int p) {
if (adj[u].size() == 1) leaves.push_back(u);
for (int v : adj[u]) {
if (v != p) dfs(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int a, b;
for (int i = 0; i < n-1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (adj[i].size() != 1 || i == n) {
dfs(i, -1);
break;
}
}
cout << (leaves.size()+1)/2 << endl;
for (int i = 0; i + leaves.size()/2 < leaves.size(); i++) {
cout << leaves[i] << " " << leaves[i + leaves.size()/2] << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |