Submission #233408

#TimeUsernameProblemLanguageResultExecution timeMemory
233408dolphingarlicIslands (IOI08_islands)C++14
28 / 100
657 ms131076 KiB
#include <bits/stdc++.h> #define FOR(i, x, y) for (int i = x; i < y; i++) typedef long long ll; using namespace std; vector<pair<int, ll>> graph[1000001], stck, cycle; ll discarded, ans = 0, cmp_ans, dp[1000001], p[4][1000001]; bool processed[1000001], on_stck[1000001], on_cycle[1000001]; bool find_cycle(int node, pair<int, ll> parent = {0, 0}) { on_stck[node] = true; for (pair<int, ll> i : graph[node]) if (i != parent) { stck.push_back({node, i.second}); if (on_stck[i.first]) { cycle.push_back({i.first, 0}); on_cycle[i.first] = true; while (stck.back().first != i.first) { cycle.push_back(stck.back()); on_cycle[stck.back().first] = true; stck.pop_back(); } discarded = stck.back().second; return true; } if (find_cycle(i.first, {node, i.second})) return true; stck.pop_back(); on_stck[i.first] = false; } return false; } void calc_tree(int node, int parent = 0) { processed[node] = true; ll mx = 0, sc = 0; for (pair<int, ll> i : graph[node]) if (i.first != parent && !on_cycle[i.first]) { calc_tree(i.first, node); if (dp[i.first] + i.second > mx) sc = mx, mx = dp[i.first] + i.second; else if (dp[i.first] + i.second > sc) sc = dp[i.first] + i.second; } dp[node] = mx; cmp_ans = max(cmp_ans, mx + sc); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; FOR(i, 1, n + 1) { int v; ll l; cin >> v >> l; graph[i].push_back({v, l}); graph[v].push_back({i, l}); } FOR(i, 1, n + 1) if (!processed[i]) { stck.clear(); cycle.clear(); find_cycle(i); cmp_ans = 0; int m = cycle.size(); FOR(j, 1, m) p[0][j] = p[0][j - 1] + cycle[j].second; for (int j = m - 2; ~j; j--) p[1][j] = p[1][j + 1] + cycle[j + 1].second; FOR(j, 0, m) { calc_tree(cycle[j].first); p[2][j] = dp[cycle[j].first] - p[0][j]; p[3][j] = dp[cycle[j].first] + p[0][j]; p[4][j] = dp[cycle[j].first] + p[1][j]; } ll mx = p[2][0]; FOR(j, 1, m) { cmp_ans = max(cmp_ans, p[3][j] + mx); mx = max(mx, p[2][j]); } mx = p[4][m - 1]; for (int j = m - 2; ~j; j--) { cmp_ans = max(cmp_ans, p[3][j] + mx + discarded); mx = max(mx, p[4][j]); } ans += cmp_ans; FOR(j, 0, 5) FOR(k, 0, m) p[j][k] = 0; } cout << ans; return 0; }

Compilation message (stderr)

islands.cpp: In function 'int main()':
islands.cpp:71:16: warning: array subscript is above array bounds [-Warray-bounds]
             p[4][j] = dp[cycle[j].first] + p[1][j];
             ~~~^
islands.cpp:79:17: warning: array subscript is above array bounds [-Warray-bounds]
         mx = p[4][m - 1];
              ~~~^
islands.cpp:82:29: warning: array subscript is above array bounds [-Warray-bounds]
             mx = max(mx, p[4][j]);
                          ~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...