Submission #605843

#TimeUsernameProblemLanguageResultExecution timeMemory
605843buidangnguyen05Network (BOI15_net)C++14
63 / 100
2095 ms91980 KiB
//source: https://oj.uz/problem/view/BOI15_net #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6 + 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(); cerr << x << " " << p << " " << q << "\n"; 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; for (int i = 1; i <= n; ++i) if (adj[i].size() > 1 && !root) root = 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(), root); cout << ans.size() << "\n"; for (pair<int, int> i : ans) cout << i.first << " " << i.second << "\n"; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |   freopen("task.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |   freopen("task.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...