# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
606591 | nguyentunglam | Network (BOI15_net) | C++14 | 498 ms | 68212 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>
#define forin(i, a, b) for(int i = a; i <= b; i++)
#define forde(i, a, b) for(int i = a; i >= b; i--)
#define forv(a, b) for(auto & a : b)
#define fi first
#define se second
using namespace std;
const int N = 5e5 + 10;
int n;
vector<int> adj[N];
vector<pair<int, int>> ans;
vector<int> dfs(int u, int par) {
vector<int> f;
if (adj[u].size() == 1) {
f.push_back(u);
return f;
}
forv(v, adj[u]) if (v != par) {
vector<int> g = dfs(v, u);
if (f.size() < g.size()) swap(f, g);
while (f.size() + g.size() > 2) {
ans.push_back({f.back(), g.back()});
f.pop_back(); g.pop_back();
}
forv(t, g) f.push_back(t);
}
return f;
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen ("bluehouse.inp", "r")) {
freopen ("bluehouse.inp", "r", stdin);
freopen ("bluehouse.out", "w", stdout);
}
cin >> n;
forin(i, 1, n - 1) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
forin(i, 1, n) if (adj[i].size() > 1) {
vector<int> t = dfs(i, 0);
if (t.size() >= 2) {
int u = t.back(); t.pop_back();
int v = t.back(); t.pop_back();
ans.push_back({u, v});
}
if (!t.empty()) ans.push_back({i, t[0]});
break;
}
cout << ans.size() << "\n";
forv(v, ans) cout << v.fi << " " << v.se << "\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... |