# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
605797 | buidangnguyen05 | Network (BOI15_net) | C++14 | 7 ms | 12056 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.
//source: https://oj.uz/problem/view/BOI15_net
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
vector<int> adj[N];
vector<pair<int, int>> ans;
vector<int> dfs(int x, int par) {
if (adj[x].size() == 1) return vector<int>(1, x);
vector<int> cur;
for (int i : adj[x]) if (i != par) {
vector<int> v = dfs(i, x);
if (cur.size() < v.size()) swap(v, cur);
while (v.size()) {
int p = cur.back(); cur.pop_back();
int q = v.back(); v.pop_back();
ans.emplace_back(p, q);
}
for (int j : v) cur.push_back(j);
}
return cur;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
int n; cin >> n;
for (int i = 1; i < n; ++i) {
int x, y; cin >> x >> y;
adj[x].push_back(y); adj[y].push_back(x);
}
int root = 0, save = 0;
for (int i = 1; i <= n; ++i) {
if (adj[i].size() > 1) root = i;
else save = i;
}
vector<int> rem = dfs(root, 0);
assert(rem.size() < 2);
if (rem.size()) ans.emplace_back(rem.back(), save);
cout << ans.size() << "\n";
for (pair<int, int> i : ans) cout << i.first << " " << i.second << "\n";
}
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... |