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>
#define f first
#define s second
using namespace std;
const int N=2e5+5,mod=1e9+7,Inf=1e10;
int dp[N][2],n,x[N],leaf[N],ans;
vector<pair<int,int> > V[N];
pair<int,int> mx[N];
void dfs2(int u,int p) {
ans = max(ans,dp[u][1]);
for(int i=0;i<V[u].size();i++) {
int v = V[u][i].f;
if(v==p) continue;
int x = 0,bef0 = dp[u][0],bef1 = dp[u][1];
if(!leaf[v]) x = max(dp[v][0] + V[u][i].s,dp[v][1]);
dp[u][1] -= x;
dp[u][0] = mx[u].f;
if(mx[u].f == -x+dp[v][1]+V[u][i].s) dp[u][0] = mx[u].s;
dp[u][0] += dp[u][1];
x = max(dp[u][1],dp[u][0] + V[u][i].s);
dp[v][1] += max(dp[u][1],dp[u][0] + V[u][i].s);
dp[v][0] = max(dp[v][0],-x + V[u][i].s + dp[u][1]);
if(-x +dp[u][1]+V[u][i].s > mx[v].f) {
swap(mx[v].f,mx[v].s);
mx[v].f = -x +dp[u][1]+V[u][i].s;
}
else mx[v].s = max(mx[v].s,-x +dp[u][1]+V[u][i].s);
dfs2(v,u);
dp[u][0] = bef0;
dp[u][1] = bef1;
}
}
void dfs(int u,int p) {
leaf[u] = 0;
dp[u][0] = dp[u][1] = 0;
dp[u][0] = -Inf;
mx[u] = {-Inf,-Inf};
for(int i=0;i<V[u].size();i++) {
int v = V[u][i].f;
if(v==p) continue;
dfs(v,u);
int x = 0;
if(!leaf[v]) dp[u][1] += max(dp[v][1],dp[v][0] + V[u][i].s),x = max(dp[v][1],dp[v][0] + V[u][i].s);
dp[u][0] = max(dp[u][0], -x +dp[v][1]+V[u][i].s);
if(-x +dp[v][1]+V[u][i].s > mx[u].f) {
swap(mx[u].f,mx[u].s);
mx[u].f = -x +dp[v][1]+V[u][i].s;
}
else mx[u].s = max(mx[u].s,-x +dp[v][1]+V[u][i].s);
}
dp[u][0] += dp[u][1];
if(V[u].size()==1 && p) leaf[u] = 1;
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++){
int u,v,w;
cin>>u>>v>>w;
V[u].push_back({v,w});
V[v].push_back({u,w});
}
int u = 1;
for(int i=1;i<=n;i++) if(V[i].size()!=1){
u = i; break;
}
dfs(u,0);
//
dfs2(u,0);
cout<<ans;
}
Compilation message (stderr)
beads.cpp:5:33: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+10' to '2147483647' [-Woverflow]
5 | const int N=2e5+5,mod=1e9+7,Inf=1e10;
| ^~~~
beads.cpp: In function 'void dfs2(int, int)':
beads.cpp:11:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for(int i=0;i<V[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=0;i<V[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: At global scope:
beads.cpp:58:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main(){
| ^~~~
# | 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... |