# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
605836 | 2022-07-26T03:20:22 Z | buidangnguyen05 | Network (BOI15_net) | C++14 | 6 ms | 11988 KB |
//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 (cur.size() + v.size() > 2 && 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); //ans.emplace_back(x, y); } int root = 0, save1 = 0, save2 = 0; for (int i = 1; i <= n; ++i) { if (adj[i].size() > 1 && !root) root = i; else if (!save1) save1 = i; else save2 = i; } //cerr << root << "\n"; vector<int> rem = dfs(root, 0); if (rem.size() > 1) { int u = rem.back(); rem.pop_back(); int v = rem.back(); rem.pop_back(); ans.emplace_back(u, v); } if (rem.size()) ans.emplace_back(rem.back(), (save1 == rem.back() ? save2 : save1)); cout << ans.size() << "\n"; for (pair<int, int> i : ans) cout << i.first << " " << i.second << "\n"; cout << endl; //system("graph_.exe"); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 11988 KB | Output is correct |
2 | Correct | 6 ms | 11988 KB | Output is correct |
3 | Correct | 6 ms | 11964 KB | Output is correct |
4 | Incorrect | 5 ms | 11988 KB | Breaking single line is causing network to disconnect. |
5 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 11988 KB | Output is correct |
2 | Correct | 6 ms | 11988 KB | Output is correct |
3 | Correct | 6 ms | 11964 KB | Output is correct |
4 | Incorrect | 5 ms | 11988 KB | Breaking single line is causing network to disconnect. |
5 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 11988 KB | Output is correct |
2 | Correct | 6 ms | 11988 KB | Output is correct |
3 | Correct | 6 ms | 11964 KB | Output is correct |
4 | Incorrect | 5 ms | 11988 KB | Breaking single line is causing network to disconnect. |
5 | Halted | 0 ms | 0 KB | - |