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>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=2e5+5;
int n, dp[N], pd[N], py[N];
vector<pii>g[N];
void dfs(const int &v, const int &p=-1){
for(pii u: g[v]){
if(u.F!=p){
py[u.F]=u.S;
dfs(u.F, v);
pd[v]+=dp[u.F];
}
}
int x=0, y=-2e9, z=0;
for(pii u: g[v]){
if(u.F!=p){
z=max(z+dp[u.F], y+pd[u.F]+u.S);
y=max(y+dp[u.F], x+pd[u.F]+u.S);
x+=dp[u.F];
}
}
pd[v]=max(pd[v], z);
dp[v]=max(pd[v], y+py[v]);
}
int main(){
cin>>n;
for(int i=1;i<n;++i){
int u, v, w;
cin>>u>>v>>w;
g[--u].push_back({--v, w});
g[v].push_back({u, w});
}
py[0]=-2e9;
dfs(0);
cout<<dp[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... |