# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
291584 | Fischer | Network (BOI15_net) | C++14 | 9 ms | 12032 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;
const int maxn = 5e5 + 10;
vector<int> g[maxn];
int n;
using ii = pair<int, int>;
int dfs(vector<ii>& edges, int x, int p=0) {
int node = g[x].size() == 1 ? x : -1;
for (int v : g[x]) {
if (v==p) continue;
int u = dfs(edges, v, x);
if (node == -1) node = u;
else if (u != -1) edges.emplace_back(node, u), node = -1;
}
return node;
}
int main() {
scanf("%d", &n);
for (int i=0; i<n-1; ++i) {
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
vector<ii> edges;
for (int i=1; i<=n; ++i) {
if (g[i].size()>1) {
int extra = dfs(edges, i);
if (extra != -1) edges.emplace_back(extra, i);
break;
}
}
cout << edges.size() << endl;
for (auto e : edges) cout << e.first << " " << e.second << '\n';
return 0;
}
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... |