제출 #907408

#제출 시각아이디문제언어결과실행 시간메모리
907408duckindog구슬과 끈 (APIO14_beads)C++14
0 / 100
2 ms5208 KiB
// from duckindog wth depression #include<bits/stdc++.h> using namespace std; #define int long long const int N = 1e5 + 10, M = -1e15; int n; vector<pair<int, int>> ad[N]; int f[N][3]; void dfs(int u, int pre = 0) { vector<pair<int, int>> best; int all = M, none = M; for (auto duck : ad[u]) { int v, w; tie(v, w) = duck; if (v == pre) continue; dfs(v, u); int x = f[v][0], y = f[v][1]; all = max(0ll, all); if (x > 0) { all += max(x + w, y); none = max(none, y - (x + w > y ? x : y)); } else none = max(none, y + w); best.push_back({v, w}); } f[u][0] = max(M, all + none); using pii = pair<int, int>; best.push_back({0, M}); best.push_back({0, M}); sort(best.begin(), best.end(), [&](pii a, pii b) { int x = max(f[a.first][1], f[a.first][2]) + a.second; int y = max(f[b.first][1], f[b.first][2]) + b.second; return x - max(0ll, f[a.first][0] + a.second) > y - max(0ll, f[b.first][0] + b.second); }); auto fi = best[0], se = best[1]; f[u][2] = max(f[fi.first][1], f[fi.first][2]) + fi.second + max(f[se.first][1], f[se.first][2]) + se.second; f[u][2] = max(M, f[u][2]); for (auto duck : ad[u]) { int v, w; tie(v, w) = duck; if (v == pre) continue; if (v != fi.first && v != se.first && f[u][2] != M) f[u][2] += max(0ll, f[v][0] + w); f[u][1] += max({0ll, f[v][1], f[v][0] + w}); } } int32_t main() { cin.tie(0)->sync_with_stdio(0); if (fopen("duck.inp", "r")) { freopen("duck.inp", "r", stdin); freopen("duck.out", "w", stdout); } f[0][0] = f[0][1] = f[0][2] = M; cin >> n; for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; ad[u].push_back({v, w}); ad[v].push_back({u, w}); } int answer = 0; for (int i = 1; i <= n; ++i) { memset(f, 0, sizeof f); dfs(i); for (int j = 1; j <= n; ++j) answer = max({answer, f[j][1], f[j][2]}); } cout << answer; }

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

beads.cpp: In function 'int32_t main()':
beads.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     freopen("duck.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("duck.out", "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...