제출 #745615

#제출 시각아이디문제언어결과실행 시간메모리
745615MilosMilutinovic구슬과 끈 (APIO14_beads)C++14
0 / 100
3 ms5076 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<ll> cur(3, -inf); cur[0] = 0; for (int y : ch) { vector<ll> ndp(3, -inf); // don't take y for (int i = 0; i < 3; 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 < 2; 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][0][1] = cur[2]; } // 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 */

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'int main()':
beads.cpp:107:22: warning: overflow in conversion from 'long long int' to 'int' changes value from '-1000000000000000' to '1530494976' [-Woverflow]
  107 |     solve(1, 1, -inf / 10);
      |                 ~~~~~^~~~
beads.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
beads.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         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...