#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 200001;
vector<pair<int, int>> g[N];
ll d0[N], d1[N], d2[N], d3[N], pw[N], inf = 1e11;
void dfs(int v, int p) {
ll t1 = -inf, t2 = -inf, t3 = -inf, w0 = -inf, w2 = -inf;
for (auto &e : g[v]) {
int u = e.first;
if (u == p)
continue;
dfs(u, v);
pw[u] = e.second;
d1[u] += e.second; d3[u] += e.second;
d1[u] = max(d1[u], d0[u]);
d2[u] = max(d2[u], d0[u]);
d3[u] = max(d3[u], d2[u]);
// d3[u] = max(d3[u], d1[u]);
d0[v] += d1[u];
t1 = max(t1, d0[u] - d1[u] + pw[u]);
t2 = max(t2, d3[u] - d1[u]);
t3 = max(t3, d2[u] - d1[u] + pw[u]);
d2[v] = max(d2[v], w0 + d2[u] + pw[u] - d1[u]);
d2[v] = max(d2[v], w2 + d0[u] + pw[u] - d1[u]);
w0 = max(w0, d0[u] + pw[u] - d1[u]);
w2 = max(w2, d2[u] + pw[u] - d1[u]);
}
d1[v] = d0[v] + t1;
d2[v] = d0[v] + max(d2[v], t2);
d3[v] = d0[v] + t3;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, u, v, w;
cin >> n;
rep(i, n - 1) {
cin >> u >> v >> w;
u--; v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(0, -1);
cout << max(d0[0], d2[0]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
5504 KB |
Output is correct |
25 |
Correct |
5 ms |
5504 KB |
Output is correct |
26 |
Correct |
8 ms |
5760 KB |
Output is correct |
27 |
Correct |
8 ms |
6016 KB |
Output is correct |
28 |
Correct |
7 ms |
6016 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
7 ms |
6016 KB |
Output is correct |
31 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
5504 KB |
Output is correct |
25 |
Correct |
5 ms |
5504 KB |
Output is correct |
26 |
Correct |
8 ms |
5760 KB |
Output is correct |
27 |
Correct |
8 ms |
6016 KB |
Output is correct |
28 |
Correct |
7 ms |
6016 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
7 ms |
6016 KB |
Output is correct |
31 |
Correct |
8 ms |
6272 KB |
Output is correct |
32 |
Correct |
30 ms |
9592 KB |
Output is correct |
33 |
Correct |
47 ms |
9612 KB |
Output is correct |
34 |
Correct |
32 ms |
9604 KB |
Output is correct |
35 |
Correct |
198 ms |
23800 KB |
Output is correct |
36 |
Correct |
192 ms |
23804 KB |
Output is correct |
37 |
Correct |
205 ms |
23800 KB |
Output is correct |
38 |
Correct |
143 ms |
24292 KB |
Output is correct |
39 |
Correct |
134 ms |
22620 KB |
Output is correct |
40 |
Correct |
134 ms |
24288 KB |
Output is correct |
41 |
Correct |
185 ms |
27760 KB |
Output is correct |