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 N=2e5+10;
struct edge{
int w,to,nxt;
};
edge ed[N<<1];
int cnt,h[N];
void add(int st,int et,int wi){
cnt++;
ed[cnt].to=et;
ed[cnt].w=wi;
ed[cnt].nxt=h[st];
h[st]=cnt;
}
int sum[N][3],dp[N][3][2],n;
void dfs(int x,int fa){
sum[x][0]=0;
dp[x][0][0]=0;
for(int i=h[x];i;i=ed[i].nxt){
int v=ed[i].to;
if(v==fa)continue;
dfs(v,x);
dp[x][2][1]=max({dp[x][2][1]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]),dp[x][1][0]+ed[i].w+max(sum[v][2],dp[v][0][1]),dp[x][1][1]+ed[i].w+dp[v][0][0]});
dp[x][2][0]=max({dp[x][2][0]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]),dp[x][1][0]+ed[i].w+dp[v][0][0]});
dp[x][1][1]=max({dp[x][1][1]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]),dp[x][0][0]+ed[i].w+max(sum[v][2],dp[v][0][1])});
dp[x][1][0]=max({dp[x][1][0]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]),dp[x][0][0]+ed[i].w+dp[v][0][0]});
dp[x][0][1]=max({dp[x][0][1]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]),dp[x][0][0]+max({ed[i].w+dp[v][1][1],sum[v][2],dp[v][0][1]})});
dp[x][0][0]=dp[x][0][0]+max(dp[v][1][0]+ed[i].w,dp[v][0][0]);
}
sum[x][0]=max(dp[x][0][0],dp[x][0][1]);
sum[x][1]=max(dp[x][1][0],dp[x][1][1]);
sum[x][2]=max(dp[x][2][0],dp[x][2][1]);
}
int main(){
scanf("%d",&n);
for(int i=1,x,y,w;i<n;i++){
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
memset(dp,-63,sizeof dp);
memset(sum,-63,sizeof sum);
dfs(1,0);
printf("%d",max(sum[1][0],sum[1][2]));
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
beads.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d%d",&x,&y,&w);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |