#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define pill pair<ll, ll>
using namespace std;
const ll N = 5e5 + 10;
const ll big = 1e16;
ll n;
vector<pill> g[N];
ll dp[N][2], dp2[2][3], dp3[2][2];
void dfs(ll v, ll p, ll z) {
for(auto u : g[v]) {
if(u.f != p)
dfs(u.f, v, u.s);
}
dp2[0][0] = 0, dp2[0][1] = -big, dp2[0][2] = -big;
for(auto u : g[v]) {
if(u.f == p)continue;
dp2[1][0] = dp2[0][0] + max(dp[u.f][0], dp[u.f][1]);
dp2[1][1] = max(dp2[0][1] + max(dp[u.f][0], dp[u.f][1]), dp2[0][0] + dp[u.f][0] + u.s);
dp2[1][2] = max(dp2[0][2] + max(dp[u.f][0], dp[u.f][1]), dp2[0][1] + dp[u.f][0] + u.s);
dp2[0][0] = dp2[1][0];
dp2[0][1] = dp2[1][1];
dp2[0][2] = dp2[1][2];
}
dp3[0][0] = 0, dp3[0][1] = -big;
for(auto u : g[v]) {
if(u.f == p)continue;
dp3[1][0] = dp3[0][0] + max(dp[u.f][1], dp[u.f][0]);
dp3[1][1] = max(dp3[0][1] + max(dp[u.f][1], dp[u.f][0]), dp3[0][0] + u.s + dp[u.f][0]);
dp3[0][0] = dp3[1][0];
dp3[0][1] = dp3[1][1];
}
dp[v][0] = max(dp2[0][0], dp2[0][2]);
dp[v][1] = dp3[0][1] + z;
}
int main() {
speed;
cin >> n;
for(int i = 1, a, b, c; i < n; i++)
cin >> a >> b >> c, g[a].pb(mp(b, c)), g[b].pb(mp(a, c));
dfs(2, 0, 0);
cout << dp[2][0];
}
/*
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 |
7 ms |
12040 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12004 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Incorrect |
9 ms |
11956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12040 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12004 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Incorrect |
9 ms |
11956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12040 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12004 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Incorrect |
9 ms |
11956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12040 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12004 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Incorrect |
9 ms |
11956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |