Submission #745623

#TimeUsernameProblemLanguageResultExecution timeMemory
745623MilosMilutinovicBeads and wires (APIO14_beads)C++14
0 / 100
3 ms5012 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 2e5 + 10; const ll inf = 1e16; ll dp[N][2][2]; // node, par edge, bad path vector<pair<int, int>> g[N]; int n, w[N]; void setup() { for (int i = 1; i <= n; i++) for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) dp[i][x][y] = -inf; } void solve(int x, int fa, int uw) { vector<int> ch; for (auto& p : g[x]) { int y = p.first; if (y == fa) continue; w[y] = p.second; solve(y, x, w[y]); ch.push_back(y); } if (ch.empty()) { dp[x][0][0] = 0; return; } // don't take x { vector<ll> cur(2, -inf); cur[0] = 0; for (int y : ch) { vector<ll> ndp(2, -inf); ndp[0] = max(ndp[0], cur[0] + dp[y][0][0]); ndp[0] = max(ndp[0], cur[0] + dp[y][1][0]); ndp[1] = max(ndp[1], cur[0] + dp[y][0][1]); ndp[1] = max(ndp[1], cur[0] + dp[y][1][1]); ndp[1] = max(ndp[1], cur[1] + dp[y][0][0]); ndp[1] = max(ndp[1], cur[1] + dp[y][1][0]); swap(cur, ndp); } dp[x][0][0] = cur[0]; dp[x][0][1] = cur[1]; } // take x, from bad path { vector<vector<ll>> cur(3, vector<ll>(2, -inf)); cur[0][0] = 0; for (int y : ch) { vector<vector<ll>> ndp(3, vector<ll>(2, -inf)); // don't take y for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { ndp[i][j] = max(ndp[i][j], cur[i][j] + dp[y][0][0]); ndp[i][j] = max(ndp[i][j], cur[i][j] + dp[y][1][0]); } } // take y for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int o = 0; o < 2; o++) { if (j + o == 2) continue; ndp[i + 1][j + o] = max(ndp[i + 1][j + o], cur[i][j] + dp[y][0][o] + w[y]); } } } swap(cur, ndp); } dp[x][0][1] = max(cur[2][0], cur[2][1]); } // take x, no bad path { vector<ll> cur(2, -inf); cur[0] = uw; for (int y : ch) { vector<ll> ndp(2, -inf); // don't take y for (int i = 0; i < 2; i++) { ndp[i] = max(ndp[i], cur[i] + dp[y][0][0]); ndp[i] = max(ndp[i], cur[i] + dp[y][1][0]); } // take y for (int i = 0; i < 1; i++) { ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][0] + w[y]); ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][1] + w[y]); } swap(cur, ndp); } dp[x][1][0] = cur[1]; } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); g[a].push_back({b, c}); g[b].push_back({a, c}); } setup(); solve(1, 1, -inf / 10); printf("%lld\n", max(dp[1][0][0], dp[1][0][1])); return 0; } /* 5 1 2 10 1 3 40 1 4 15 1 5 20 60 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 140 9 1 3 100 2 3 100 3 7 1 4 5 100 4 6 100 7 4 1 8 7 100 9 7 100 */

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:113:22: warning: overflow in conversion from 'long long int' to 'int' changes value from '-1000000000000000' to '1530494976' [-Woverflow]
  113 |     solve(1, 1, -inf / 10);
      |                 ~~~~~^~~~
beads.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
beads.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...