Submission #120273

#TimeUsernameProblemLanguageResultExecution timeMemory
120273nvmdavaIslands (IOI08_islands)C++17
18 / 100
712 ms131072 KiB
#include <bits/stdc++.h> using namespace std; #define N 1000001 vector<pair<int, long long> > adj[N]; bool in[N]; vector<int> s; long long dp[N][3]; vector<int> cyc; bool inCyc[N]; void dfs(int v, int p){ long long b1 = 0, b2 = 0; for(auto xx : adj[v]){ int x = xx.first; long long c = xx.second; if(inCyc[x] || p == x) continue; dfs(x, v); long long up; dp[v][0] += dp[x][2]; up = dp[x][1] - dp[x][2] + c; if(b1 < up){ b2 = b1; b1 = up; } else b2 = max(b2, up); } dp[v][1] = dp[v][0] + b1; dp[v][2] = dp[v][0] + b1 + b2; } long long ans = 0; unordered_map<int, unordered_map<int, int> > lol; void solve(){ for(int x : s){ if(inCyc[x]) { cyc.push_back(x); dfs(x, 0); } } long long dp[2][2][3][3]; memset(dp, -0x3f, sizeof dp); dp[0][0][0][0] = ::dp[cyc[0]][0]; dp[0][0][1][1] = ::dp[cyc[0]][1]; dp[0][0][2][2] = ::dp[cyc[0]][2]; int st = 0; for(int i = 1; i < cyc.size(); i++){ st = i & 1; memset(dp[st], -0x3f, sizeof dp[st]); for(int t = 0; t <= 2; t++){ for(int j = 0; j <= 2; j++){ for(int l = 0; l <= 2; l++){ dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j]); dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j]); } } if(i == 1 && t == 2) break; for(int j = 1; j <= 2; j++){ for(int l = 0; l < 2; l++){ if(i == 1){ dp[st][1][t + 1][j] = max(dp[st][1][t][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]); dp[st][0][t + 1][j] = max(dp[st][0][t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]); } else { dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]); dp[st][0][t][j] = max(dp[st][0][t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]); } } } } } long long res = 0; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ res = max(res, dp[st][0][i][j]); res = max(res, dp[st][1][i][j]); } } for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ res = max(res, dp[st][1][i][j] + lol[cyc[0]][cyc.back()]); } } ans += res; } int trav(int i, int p){ s.push_back(i); in[i] = 1; int res = 0; bool ok = 0; for(auto xx : adj[i]){ int x = xx.first; if(p == x && ok == 0){ ok = 1; continue; } if(in[x]){ res = x; continue; } int t = trav(x, i); if(res < t)res = t; } if(res) inCyc[i] = 1; if(res == i) return 0; return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i = 1; i <= n; i++){ int l, e; cin>>e>>l; adj[e].push_back({i, l}); adj[i].push_back({e, l}); lol[e][i] = max(lol[e][i], l); lol[i][e] = max(lol[i][e], l); } for(int i = 1; i <= n; i++){ if(in[i]) continue; s.clear(); cyc.clear(); trav(i, 0); solve(); } cout<<ans; }

Compilation message (stderr)

islands.cpp: In function 'void solve()':
islands.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < cyc.size(); i++){
                 ~~^~~~~~~~~~~~
#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...