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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
int N;
int x,y,z;
vector<pii> grafo[200005];
int A[200005],B[200005];
void DFS(int k, int p=-1){
for(pii f:grafo[k])
if(f.first!=p)
DFS(f.first,k);
vector<int> v;
for(pii f:grafo[k])
if(f.first!=p){
A[k]+=max(A[f.first],B[f.first]+f.second);
v.push_back(A[f.first]+f.second-max(A[f.first],B[f.first]+f.second));
}
sort(v.begin(),v.end(),greater<int>());
if(!B[k])
B[k]=-2e9;
if(v.size()>0 and v[0]>0)
B[k]=A[k]+v[0];
if(v.size()>1 and v[0]+v[1]>0)
A[k]=A[k]+v[0]+v[1];
}
int main(){
cin>>N;
for(int i=0;i<N-1;i++){
cin>>x>>y>>z;
grafo[x].push_back({y,z});
grafo[y].push_back({x,z});
}
DFS(1);
cout<<A[1];
}
# | 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... |