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 int nax = 200005;
vector<pair<int,int>>g[nax];
int dp[nax][2], n;
int solve(int v, int topar, int p = -1) {
int &ret = dp[v][(topar>0)];
if(ret!=-1) return ret;
ret = 0;
int base = 0;
for(auto [u, w] : g[v]) if(u!=p) {
base += solve(u, w, v);
}
ret = base;
vector<int>lap;
for(auto [u, w] : g[v]) if(u!=p) {
lap.push_back(-solve(u, w, v) + solve(u, 0, v) + w);
}
sort(lap.rbegin(), lap.rend());
for(int i=0, acc=0; i<min(2, (int)lap.size()); ++i) {
acc += lap[i];
if((~i&1) && topar) {
ret = max(ret, base + acc + topar);
} else if(i&1) {
ret = max(ret, base + acc);
}
}
return ret;
}
void PlayGround() {
cin>>n;
for(int i=1; i<n; ++i) {
int u, v, w;
cin>>u>>v>>w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(dp, -1, sizeof dp);
cout<<solve(1, 0)<<'\n';
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
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... |