이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
#define int long long
using ll = long long;
using ii = pair<int, int>;
const ll inf = 1e18;
int n;
vector<vector<ii>> adj;
vector<vector<ll>> dp;
void dfs(int x, int p = -1, ll pw = -inf){
ll best1 = -inf, best2 = -inf;
ll sum = 0;
for (auto [k, w]: adj[x]){
if (k == p) continue;
dfs(k, x, w);
sum += dp[k][0];
ll delta = dp[k][1] - dp[k][0];
if (delta > best1){
best2 = best1;
best1 = delta;
}
else if (delta > best2){
best2 = delta;
}
}
sum = max(sum, 0LL);
dp[x][0] = max({sum, sum + best1 + best2, sum + best1 + pw});
dp[x][1] = max({sum + pw, sum + best1 + best2 + pw});
}
void main_program(){
cin >> n;
adj.resize(n);
for (int i = 0; i < n-1; i++){
int x, y, w; cin >> x >> y >> w;
x--; y--;
adj[x].emplace_back(y, w);
adj[y].emplace_back(x, w);
}
dp.assign(n, {0, 0});
dfs(0);
cout << dp[0][0] << "\n";
}
# | 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... |