#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
const int N=2e5+1;
int n;
int dp[N][4];
int p[N],pl[N];
int z[4];
vector<pair<int,int> >adj[N];
void dfs(int id){
for(auto cur:adj[id]){
if(cur.se==p[id]) continue;
p[cur.se]=id,pl[cur.se]=cur.fi;
dfs(cur.se);
}
}
void solve(int id){
for(auto cur:adj[id]){//doesnt take node i as merger
if(cur.se==p[id]) continue;
solve(cur.se);
}
z[0]=z[1]=z[2]=z[3]=-2e9;
z[1]=0;
for(auto cur:adj[id]){//doesnt take node i as merger
if(cur.se==p[id]) continue;
z[3]=max(z[3]+max(dp[cur.se][0],dp[cur.se][1]),z[1]+max(dp[cur.se][2],dp[cur.se][3]));
z[1]=z[1]+max(dp[cur.se][0],dp[cur.se][1]);
}
dp[id][1]=max(dp[id][1],z[1]);dp[id][3]=max(dp[id][3],max(z[1],z[3]));
z[0]=z[1]=z[2]=z[3]=-2e9;z[0]=0;
for(auto cur:adj[id]){//merge horizontally
if(cur.se==p[id]) continue;
z[3]=z[3]+max(dp[cur.se][0],dp[cur.se][1]);
z[3]=max(z[3],max(z[2]+dp[cur.se][1],z[1]+dp[cur.se][3])+pl[cur.se]);
z[2]=max(z[2]+max(dp[cur.se][0],dp[cur.se][1]),z[0]+dp[cur.se][3]+pl[cur.se]);
z[1]=max(z[1]+max(dp[cur.se][0],dp[cur.se][1]),z[0]+dp[cur.se][1]+pl[cur.se]);
z[0]=z[0]+max(dp[cur.se][0],dp[cur.se][1]);
}
dp[id][3]=max(dp[id][3],z[3]);
if(id==1) return;
z[0]=z[1]=z[2]=z[3]=-2e9;z[0]=0;
for(auto cur:adj[id]){//merge vertically
if(cur.se==p[id]) continue;
z[2]=max(z[2]+max(dp[cur.se][0],dp[cur.se][1]),z[0]+dp[cur.se][3]+pl[cur.se]);
z[1]=max(z[1]+max(dp[cur.se][0],dp[cur.se][1]),z[0]+dp[cur.se][1]+pl[cur.se]);
z[0]=z[0]+max(dp[cur.se][0],dp[cur.se][1]);
}
dp[id][2]=max(dp[id][2],z[2]+pl[id]);dp[id][0]=max(dp[id][0],z[1]+pl[id]);
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<n ;i++){
int u,v,w;cin >> u >> v >> w;
adj[u].pb({w,v});adj[v].pb({w,u});
}
dfs(1);
solve(1);
cout << max(max(dp[1][0],dp[1][1]),max(dp[1][2],dp[1][3])) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
6 ms |
5092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
6 ms |
5092 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
9 ms |
5376 KB |
Output is correct |
25 |
Correct |
8 ms |
5376 KB |
Output is correct |
26 |
Correct |
11 ms |
5632 KB |
Output is correct |
27 |
Correct |
11 ms |
5632 KB |
Output is correct |
28 |
Correct |
10 ms |
5760 KB |
Output is correct |
29 |
Correct |
11 ms |
5632 KB |
Output is correct |
30 |
Correct |
10 ms |
5760 KB |
Output is correct |
31 |
Correct |
11 ms |
5888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
6 ms |
5092 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
9 ms |
5376 KB |
Output is correct |
25 |
Correct |
8 ms |
5376 KB |
Output is correct |
26 |
Correct |
11 ms |
5632 KB |
Output is correct |
27 |
Correct |
11 ms |
5632 KB |
Output is correct |
28 |
Correct |
10 ms |
5760 KB |
Output is correct |
29 |
Correct |
11 ms |
5632 KB |
Output is correct |
30 |
Correct |
10 ms |
5760 KB |
Output is correct |
31 |
Correct |
11 ms |
5888 KB |
Output is correct |
32 |
Correct |
38 ms |
8184 KB |
Output is correct |
33 |
Correct |
37 ms |
8056 KB |
Output is correct |
34 |
Correct |
33 ms |
8028 KB |
Output is correct |
35 |
Correct |
231 ms |
17212 KB |
Output is correct |
36 |
Correct |
250 ms |
17272 KB |
Output is correct |
37 |
Correct |
227 ms |
17272 KB |
Output is correct |
38 |
Correct |
155 ms |
17508 KB |
Output is correct |
39 |
Correct |
167 ms |
17504 KB |
Output is correct |
40 |
Correct |
158 ms |
17376 KB |
Output is correct |
41 |
Correct |
233 ms |
20412 KB |
Output is correct |