#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const int INF = 1e9 + 7;
struct Edge{
int v, c;
Edge(int _v, int _c){
v = _v;
c = _c;
}
};
int n, dp[N][4];
vector<Edge> adj[N];
void Dfs(int u, int p){
dp[u][0] = 0;
dp[u][1] = -INF;
dp[u][2] = -INF;
dp[u][3] = -INF;
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (v == p)
continue;
Dfs(v, u);
int nxtDp[4] = {-INF, -INF, -INF, -INF};
nxtDp[0] = max(nxtDp[0], dp[u][0] + dp[v][0]);
nxtDp[0] = max(nxtDp[0], dp[u][0] + dp[v][1] + c);
nxtDp[0] = max(nxtDp[0], dp[u][0] + dp[v][3]);
nxtDp[1] = max(nxtDp[1], dp[u][0] + dp[v][0] + c);
nxtDp[1] = max(nxtDp[1], dp[u][1] + dp[v][0]);
nxtDp[1] = max(nxtDp[1], dp[u][1] + dp[v][1] + c);
nxtDp[1] = max(nxtDp[1], dp[u][1] + dp[v][3]);
nxtDp[2] = max(nxtDp[2], dp[u][0] + dp[v][3] + c);
nxtDp[2] = max(nxtDp[2], dp[u][2] + dp[v][0]);
nxtDp[2] = max(nxtDp[2], dp[u][2] + dp[v][1] + c);
nxtDp[2] = max(nxtDp[2], dp[u][2] + dp[v][3]);
nxtDp[3] = max(nxtDp[3], dp[u][1] + dp[v][0] + c);
nxtDp[3] = max(nxtDp[3], dp[u][1] + dp[v][3] + c);
nxtDp[3] = max(nxtDp[3], dp[u][2] + dp[v][0] + c);
nxtDp[3] = max(nxtDp[3], dp[u][3] + dp[v][1] + c);
nxtDp[3] = max(nxtDp[3], dp[u][3] + dp[v][3]);
dp[u][0] = nxtDp[0];
dp[u][1] = nxtDp[1];
dp[u][2] = nxtDp[2];
dp[u][3] = nxtDp[3];
//cerr << u << ' ' << v << ' ' << dp[u][0] << ' ' << dp[u][1] << ' ' << dp[u][2] << ' ' << dp[u][3] << endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back(Edge(v, c));
adj[v].push_back(Edge(u, c));
}
Dfs(1, -1);
cout << max(dp[1][0], dp[1][3]);
return 0;
}
/*
10
5 6 9
2 3 5
1 10 8
4 5 9
2 7 8
5 7 10
6 9 4
2 8 9
1 7 5
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |