void dfs3(int node, int pa) { for (int e = nrbegin[node]; e; e = nrnext[e]) { int son = nrto[e]; if (son == pa) continue; int sans = 0; if (max2[node][0].first != son) sans = std::max(sans, max2[node][0].second); else sans = std::max(sans, max2[node][1].second); sans += dp1[node] - std::max(0, dp0[son] - 2 * cost[nredge[e]]); int sbns = dp3[node] + dp0[node] - std::max(0, dp0[son] - 2 * cost[nredge[e]]); dp3[son] = std::max(0, std::max(sans, sbns) - cost[nredge[e]]); dfs3(son, node); } }
void dfs3(int u,int fas,int deps) { if (fas==-1) { for(int i=0;i<=Bei;i++) fa[u][i]=u; sum[u]=we[u]; } else { fa[u][0]=fas; for(int i=1;i<=Bei;i++) fa[u][i]=fa[ fa[u][i-1] ][i-1]; sum[u]=sum[fas]+we[u]; } dep[u]=deps; for(int p=trbox[u];p!=-1;p=tre[p].next) if (tre[p].to!=fas) dfs3(tre[p].to,u,deps+1); }
void prelca() { dfs3(0,-1,0); }
inline void do_dp3() { dp3[1] = 0; dfs3(1, 0); }