제출 #924992

#제출 시각아이디문제언어결과실행 시간메모리
924992thinknoexit구슬과 끈 (APIO14_beads)C++17
100 / 100
104 ms29020 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200200; const ll INF = 5e17; vector<pair<int, int>> adj[N]; ll dp[4][N]; void dfs(int v, int p = -1) { ll mx0 = 0; pair<ll, int> mxr[2] = { {-INF,0}, {-INF,0} }; pair<ll, int> mxb[2] = { {-INF,0}, {-INF,0} }; for (auto& [x, w] : adj[v]) { if (x == p) continue; dfs(x, v); ll val = max(dp[1][x], dp[2][x] + w); dp[0][v] += val; dp[1][v] += val; dp[2][v] += val; dp[3][v] += val; mx0 = max(mx0, max(dp[0][x], dp[3][x] + w) - val); mxr[1] = max(mxr[1], { dp[1][x] + w - val, x }); mxb[1] = max(mxb[1], { dp[0][x] + w - val, x }); if (mxr[0] < mxr[1]) swap(mxr[0], mxr[1]); if (mxb[0] < mxb[1]) swap(mxb[0], mxb[1]); } ll mx1; if (mxr[0].second != mxb[0].second) mx1 = mxr[0].first + mxb[0].first; else mx1 = max(mxr[0].first + mxb[1].first, mxr[1].first + mxb[0].first); dp[0][v] += max(mx0, mx1); dp[2][v] += mxr[0].first; dp[3][v] += mxb[0].first; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; for (int i = 1;i < n;i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({ v, w }); adj[v].push_back({ u, w }); } dfs(1); cout << max(dp[0][1], dp[1][1]); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...