제출 #754001

#제출 시각아이디문제언어결과실행 시간메모리
754001thinknoexit구슬과 끈 (APIO14_beads)C++17
0 / 100
4 ms5076 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200200; vector<pair<int, ll>> adj[N]; ll dp[2][N]; void dfs(int v, int p = 0) { if (adj[v].size() == 1 && adj[v][0].first == p) { dp[0][v] = 0; dp[1][v] = -2e9; } ll mx = -2e9, mx2 = -2e9; ll sum = 0; for (auto& [x, w] : adj[v]) { if (x == p) continue; dfs(x, v); ll val = max(dp[0][x], dp[1][x] + w); sum += val; ll val2 = dp[0][x] + w - val; if (val2 > mx) { mx2 = mx; mx = val2; } else if (val2 > mx2) { mx2 = val2; } } dp[1][v] = sum + mx; dp[0][v] = max(sum, sum + mx + mx2); } int main() { cin.tie(nullptr)->sync_with_stdio(false); freopen("stdin", "r", stdin); freopen("stdout", "w", stdout); int n; cin >> n; for (int i = 1;i < n;i++) { int u, v; ll w; cin >> u >> v >> w; adj[u].push_back({ v,w }); adj[v].push_back({ u,w }); } dfs(1); cout << dp[0][1]; return 0; } /* 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 */

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

beads.cpp: In function 'int main()':
beads.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("stdin", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
beads.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     freopen("stdout", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...