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 long long N=1000000000000000000LL;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,pas,dp[200009],dp2[200009];
pair <long long, long long> msh[200009];
vector <pair <long long, long long> > v[200009];
multiset <long long> s[200009];
multiset <long long>::iterator tt;
void dfsst(long long q, long long w){
long long qw=-N;
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
msh[(*it).first]=make_pair(q,(*it).second);
dfsst((*it).first,q);
dp[q]+=max(dp[(*it).first],dp2[(*it).first]);
if(qw<(*it).second+dp[(*it).first]-max(dp[(*it).first],dp2[(*it).first])) qw=(*it).second+dp[(*it).first]-max(dp[(*it).first],dp2[(*it).first]);
s[q].insert((*it).second+dp[(*it).first]-max(dp[(*it).first],dp2[(*it).first]));
}
if(qw!=-N){
dp2[q]=msh[q].second+dp[q]+qw;
}else{
dp2[q]=-N;
}
}
void rt(long long q, long long w){
dp[q]-=max(dp[w],dp2[w]);
long long qw=msh[w].second+dp[w]-max(dp[w],dp2[w]);
s[q].erase(s[q].lower_bound(qw));
msh[q].first=w;msh[q].second=msh[w].second;
if(s[q].size()!=0){
tt=s[q].end();
tt--;
dp2[q]=msh[q].second+dp[q]+(*tt);
}else{
dp2[q]=-N;
}
s[w].insert(msh[q].second+dp[q]-max(dp[q],dp2[q]));
dp[w]+=max(dp[q],dp2[q]);
}
void dfs(long long q, long long w){
pas=max(pas,dp[q]);
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
rt(q,(*it).first);
dfs((*it).first,q);
rt((*it).first,q);
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a;
for(i=1; i<a; i++){
cin>>c>>d>>e;
v[c].push_back(make_pair(d,e));
v[d].push_back(make_pair(c,e));
}
dfsst(1,0);
dfs(1,0);
cout<<pas;
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... |