This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
vector <pair <int, int>> g[N];
int dp[N][2];
const int inf = 2e9;
void dfs(int v, int fg, int p){
if(dp[v][fg] != -1) return;
int sum = 0;
int best1 = -inf, best2 = -inf;
for(auto [x, w] : g[v]){
if(x == p){
if(fg == 1) continue;
if(best1 < w){
best2 = best1;
best1 = w;
}
else if(best2 < w){
best2 = w;
}
continue;
}
dfs(x, 0, v);
dfs(x, 1, v);
sum += dp[x][0];
if(best1 < dp[x][1]+w-dp[x][0]){
best2 = best1;
best1 = dp[x][1]+w-dp[x][0];
}
else if(best2 < dp[x][1]+w-dp[x][0]){
best2 = dp[x][1]+w-dp[x][0];
}
}
if(best2 == -inf) dp[v][fg] = sum;
else dp[v][fg] = max(sum, sum+best1+best2);
}
int main(){
memset(dp, -1, sizeof dp);
int n;
cin >> n;
for(int i = 1;i < n;i++){
int a, b, w;
cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
dfs(1, 1, 1);
cout << dp[1][1] << '\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... |