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 fi first
#define se second
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
int timer, n;
vvi adj;
vii leaves;
void dfs(int u, int p) {
//cerr << "// dfs(" << u << ", " << p << ")\n";
timer++;
if (adj[u].size() == 1) leaves.push_back({timer, u});
for (int v : adj[u]) if (v != p) dfs(v, u);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
adj.assign(n + 20, vi());
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0, -1);
sort(leaves.begin(), leaves.end());
//cerr << "// leaves :";
//for (ii leaf : leaves) cerr << " {" << leaf.fi << ", " << leaf.se << '}';
//cerr << '\n';
int leafCount = leaves.size();
int edgeCount = (leaves.size() + 1) / 2;
cout << edgeCount << '\n';
for (int i = 0; i < edgeCount; i++) {
int j = leafCount - i - 1;
if (i == j) j = 0;
cout << leaves[i].se + 1 << ' ' << leaves[j].se + 1 << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |