Submission #33484

#TimeUsernameProblemLanguageResultExecution timeMemory
33484szawinisBeads and wires (APIO14_beads)C++14
100 / 100
242 ms29300 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5+1; const ll INF = 1e12+1; int n; ll dp[N][2][2]; vector<pair<int,int> > g[N]; void dfs(int u, int par) { for(auto p: g[u]) if(p.first != par) dfs(p.first, u); vector<int> v, w, vals; for(auto p: g[u]) if(p.first != par) { v.push_back(p.first); w.push_back(p.second); vals.push_back(max(dp[p.first][1][0] + p.second, dp[p.first][0][0])); } //init dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = -INF; // 00 for(int i = 0; i < v.size(); i++) dp[u][0][0] += vals[i]; //01 ll mx = -INF; for(int i = 0; i < v.size(); i++) mx = max(max(dp[v[i]][0][1], dp[v[i]][1][1] + w[i]) - vals[i], mx); dp[u][0][1] = dp[u][0][0] + mx; mx = -INF; for(int i = 0; i < v.size(); i++) { dp[u][0][1] = max(dp[u][0][0] + dp[v[i]][0][0] + w[i] - vals[i] + mx, dp[u][0][1]); mx = max(max(dp[v[i]][0][0], dp[v[i]][0][1]) + w[i] - vals[i], mx); } mx = -INF; for(int i = v.size()-1; i >= 0; i--) { dp[u][0][1] = max(dp[u][0][0] + dp[v[i]][0][0] + w[i] - vals[i] + mx, dp[u][0][1]); mx = max(max(dp[v[i]][0][0], dp[v[i]][0][1]) + w[i] - vals[i], mx); } //10 mx = -INF; for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][0] + w[i] - vals[i], mx); dp[u][1][0] = dp[u][0][0] + mx; //11 mx = -INF; for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][1] + w[i] - vals[i], mx); dp[u][1][1] = dp[u][0][0] + mx; mx = -INF; for(int i = 0; i < v.size(); i++) { dp[u][1][1] = max(dp[u][0][0] + dp[v[i]][1][0] + w[i] - vals[i] + mx, dp[u][1][1]); mx = max(dp[v[i]][0][1] - vals[i], mx); } // cout << u << ' ' << dp[u][0][0] << ' ' << dp[u][0][1] << ' ' << dp[u][1][0] << ' ' << dp[u][1][1] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1, u, v, w; i < n; i++) { cin >> u >> v >> w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } dfs(1, 0); cout << max(dp[1][0][0], dp[1][0][1]); }

Compilation message (stderr)

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:23:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) dp[u][0][0] += vals[i];
                 ~~^~~~~~~~~~
beads.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) mx = max(max(dp[v[i]][0][1], dp[v[i]][1][1] + w[i]) - vals[i], mx);
                 ~~^~~~~~~~~~
beads.cpp:30:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) {
                 ~~^~~~~~~~~~
beads.cpp:42:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][0] + w[i] - vals[i], mx);
                 ~~^~~~~~~~~~
beads.cpp:47:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][1] + w[i] - vals[i], mx);
                 ~~^~~~~~~~~~
beads.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.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...