이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |