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;
typedef long long int ll;
const int MAXN = 2e5 + 10, inf = INT_MAX / 4;
int n, dp[3][MAXN], tmp[MAXN], last[MAXN], parw[MAXN], mx[MAXN];
vector <pair<int,int>> adj[MAXN];
void dfs(int v, int parv){
tmp[v] = -inf;
for(auto i: adj[v]){
int u = i.first, w = i.second;
if(u == parv) continue;
parw[u] = w;
dfs(u, v);
dp[1][v] = max(dp[1][v]+ mx[u], dp[0][v]+parw[v]+w+max(dp[0][u], dp[2][u]));
dp[2][v] = max(dp[2][v]+mx[u], tmp[v]+w+max(dp[0][u],dp[2][u]));
tmp[v] = max(tmp[v]+mx[u], dp[0][v]+w+max(dp[0][u],dp[2][u]));
dp[0][v] += mx[u];
}
mx[v] = max(max(dp[0][v], dp[1][v]), dp[2][v]);
return;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n-1; i++){
int a, b, w;
cin >> a >> b >> w; a--; b--;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
dfs(0, -1);
cout << max(dp[0][0],dp[2][0]) << '\n';
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... |