# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
76787 | shoemakerjo | Network (BOI15_net) | C++14 | 13 ms | 12148 KiB |
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; i++) {
cout << leaves[i] << " " << leaves[leaves.size()-i-1] << endl;
}
if (leaves.size() % 2) {
cout << leaves[leaves.size()/2] << " " << leaves[0] << endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |