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, a[N], b[N], c[N], d[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);
d[v]+=c[u.F];
}
}
b[v]=c[v]=d[v];
int w=0, x=-2e9, y=-2e9, z=-2e9;
for(pii u: g[v]){
if(u.F!=p){
c[v]=max(c[v], d[v]-c[u.F]+d[u.F]+u.S+py[v]);
b[v]=max(b[v], d[v]-c[u.F]+a[u.F]);
a[v]=max(a[v], d[v]-c[u.F]+b[u.F]+u.S+py[v]);
z=max({z+c[u.F], y+d[u.F]+u.S, x+b[u.F]+u.S});
y=max(y+c[u.F], w+b[u.F]+u.S);
x=max(x+c[u.F], w+d[u.F]+u.S);
w+=c[u.F];
}
}
b[v]=max(b[v], z);
a[v]=max({a[v], b[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<<a[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... |