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;
struct edge{
int v,w;
edge(int v=0,int w=0): v(v), w(w) {}
};
int n;
vector <edge> G[200005];
int dp[200005][5];
inline int val1(int pos,int w){
return dp[pos][0] + w;
}
inline int val02(int pos,int w){
return max(dp[pos][0], dp[pos][2] + w);
}
void dfs(int u,int p){
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
if (v == p) continue;
dfs(v,u);
}
// dp[i][0]
dp[u][0] = 0;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
if (v == p) continue;
dp[u][0] += val02(v,G[u][i].w);
}
// dp[i][1]
int fi02=0,se02=0,fiva02=-(int)1e9, seva02=-(int)1e9;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
int w = G[u][i].w;
if (v == p) continue;
if (val1(v,w) - val02(v,w) >= val1(fi02,fiva02) - val02(fi02,fiva02))
se02 = fi02,
seva02 = fiva02,
fi02 = v,
fiva02 = w;
else if (val1(v,w) - val02(v,w) >= val1(se02,seva02) - val02(se02,seva02))
se02 = v,
seva02 = w;
}
dp[u][1] = - (int) 1e9;
//cout << u << ' '<< fi02 << ' ' << se02 << ' ' << fiva02 << ' ' << seva02 << endl;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
int w = G[u][i].w;
if (v == p) continue;
dp[u][1] = max(dp[u][1], dp[u][0] - val02(v,w) + dp[v][1]);
//cout << "UV " << u << ' ' << v << ' ' << w << ' ' <<dp[u][0] << ' ' << -val02(v,w) << ' ' << dp[v][1] << ' ' << w << ' ' << -val02(fi02,fiva02) << ' ' << dp[fi02][0] << ' ' << fiva02 << endl;
if (v != fi02)//cout <<"k\n",
dp[u][1] = max(dp[u][1], dp[u][0] - val02(v,w) + dp[v][1] + w - val02(fi02,fiva02) + dp[fi02][0] + fiva02);
//cout << dp[u][1] << endl;
else if (se02)
dp[u][1] = max(dp[u][1], dp[u][0] - val02(v,w) + dp[v][1] + w - val02(se02,seva02) + dp[se02][0] + seva02);
}
if (se02){
//cout << "u " << u << ' ' << fi02 << ' ' << se02 << endl;
dp[u][1] = max(dp[u][1], dp[u][0] - val02(fi02,fiva02) - val02(se02,seva02) + val1(fi02,fiva02) + val1(se02,seva02));
}
// dp[i][2]
dp[u][2] = -(int)1e9;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
int w = G[u][i].w;
if (v == p) continue;
dp[u][2] = max(dp[u][2], dp[u][0] - val02(v,w) + dp[v][0] + G[u][i].w);
}
// dp[i][3]
dp[u][3] = -(int)1e9;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
int w = G[u][i].w;
if (v == p) continue;
dp[u][3] = max(dp[u][3], dp[u][0] - val02(v,w) + dp[v][1] + G[u][i].w);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
int x,y,w;
scanf("%d %d %d",&x,&y,&w);
G[x].push_back(edge(y,w));
G[y].push_back(edge(x,w));
}
dfs(1,1);
cout << max(dp[1][0], dp[1][1]) << endl;
//for(int i=1;i<=n;i++)
// cout << "I " << i << ' ' << dp[i][0] <<' ' << dp[i][1] << ' ' << dp[i][2] << ' ' << dp[i][3] << endl;
}
/*
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
10
4 10 5
1 10 7
3 10 10
3 9 10
2 3 5
3 8 9
7 10 3
5 8 9
4 6 6
10
2 3 7
6 9 7
2 10 6
4 9 2
2 8 5
1 6 1
6 7 10
6 8 9
2 5 6
*/
Compilation message (stderr)
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp:24:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp:31:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp:66:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp:74:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[u].size();i++){
~^~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
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... |