#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int const maxn = 202020;
int const inf = 0x3f3f3f3f;
struct Edge {
int u, w, id;
};
struct FEdge {
int a, b, w;
};
vector<Edge> v[maxn*3];
vector<FEdge> eg;
int dp[3*maxn][2];
inline int invalid(int x, int p) {
return v[x].size() == 1;
}
int solve(int id, int t = 0) {
int x = eg[id].b;
int p = eg[id].a;
if (dp[id][t] >= 0) return dp[id][t];
int d = 0;
if (t == 0) {
for (Edge const& e : v[x]) {
if (e.u == p) continue;
if (!invalid(e.u, x)) {
d += max(solve(e.id, 1) + e.w, solve(e.id, 0));
}
}
}
else {
int sum = solve(id, 0);
for (Edge const& e : v[x]) {
if (e.u == p) continue;
if (!invalid(e.u, x))
d = max(d, sum - max(solve(e.id, 1) + e.w, solve(e.id, 0))
+ e.w + solve(e.id, 0));
else
d = max(d, sum + e.w);
}
}
return dp[id][t] = d;
}
int main() {
memset(dp, -1, sizeof(dp));
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b, w;
cin >> a >> b >> w;
v[a].push_back({b, w, i*2});
v[b].push_back({a, w, i*2+1});
eg.push_back({a, b, w});
eg.push_back({b, a, w});
}
for (int i = 1; i <= n; i++) {
v[i+n].push_back({i, -inf, 2*n - 3 + i});
eg.push_back({i+n, i, -inf});
}
int ans = 0;
for (int i = 2*n-2; i < 3*n-2; i++) {
ans = max(ans, solve(i));
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19320 KB |
Output is correct |
2 |
Correct |
23 ms |
19320 KB |
Output is correct |
3 |
Correct |
19 ms |
19320 KB |
Output is correct |
4 |
Correct |
19 ms |
19324 KB |
Output is correct |
5 |
Correct |
19 ms |
19320 KB |
Output is correct |
6 |
Correct |
23 ms |
19320 KB |
Output is correct |
7 |
Correct |
23 ms |
19320 KB |
Output is correct |
8 |
Correct |
23 ms |
19272 KB |
Output is correct |
9 |
Correct |
19 ms |
19320 KB |
Output is correct |
10 |
Correct |
19 ms |
19320 KB |
Output is correct |
11 |
Correct |
19 ms |
19320 KB |
Output is correct |
12 |
Correct |
19 ms |
19320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19320 KB |
Output is correct |
2 |
Correct |
23 ms |
19320 KB |
Output is correct |
3 |
Correct |
19 ms |
19320 KB |
Output is correct |
4 |
Correct |
19 ms |
19324 KB |
Output is correct |
5 |
Correct |
19 ms |
19320 KB |
Output is correct |
6 |
Correct |
23 ms |
19320 KB |
Output is correct |
7 |
Correct |
23 ms |
19320 KB |
Output is correct |
8 |
Correct |
23 ms |
19272 KB |
Output is correct |
9 |
Correct |
19 ms |
19320 KB |
Output is correct |
10 |
Correct |
19 ms |
19320 KB |
Output is correct |
11 |
Correct |
19 ms |
19320 KB |
Output is correct |
12 |
Correct |
19 ms |
19320 KB |
Output is correct |
13 |
Correct |
19 ms |
19320 KB |
Output is correct |
14 |
Correct |
19 ms |
19320 KB |
Output is correct |
15 |
Correct |
23 ms |
19320 KB |
Output is correct |
16 |
Correct |
23 ms |
19320 KB |
Output is correct |
17 |
Correct |
25 ms |
19320 KB |
Output is correct |
18 |
Correct |
23 ms |
19324 KB |
Output is correct |
19 |
Correct |
25 ms |
19292 KB |
Output is correct |
20 |
Correct |
23 ms |
19320 KB |
Output is correct |
21 |
Correct |
19 ms |
19320 KB |
Output is correct |
22 |
Correct |
24 ms |
19324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19320 KB |
Output is correct |
2 |
Correct |
23 ms |
19320 KB |
Output is correct |
3 |
Correct |
19 ms |
19320 KB |
Output is correct |
4 |
Correct |
19 ms |
19324 KB |
Output is correct |
5 |
Correct |
19 ms |
19320 KB |
Output is correct |
6 |
Correct |
23 ms |
19320 KB |
Output is correct |
7 |
Correct |
23 ms |
19320 KB |
Output is correct |
8 |
Correct |
23 ms |
19272 KB |
Output is correct |
9 |
Correct |
19 ms |
19320 KB |
Output is correct |
10 |
Correct |
19 ms |
19320 KB |
Output is correct |
11 |
Correct |
19 ms |
19320 KB |
Output is correct |
12 |
Correct |
19 ms |
19320 KB |
Output is correct |
13 |
Correct |
19 ms |
19320 KB |
Output is correct |
14 |
Correct |
19 ms |
19320 KB |
Output is correct |
15 |
Correct |
23 ms |
19320 KB |
Output is correct |
16 |
Correct |
23 ms |
19320 KB |
Output is correct |
17 |
Correct |
25 ms |
19320 KB |
Output is correct |
18 |
Correct |
23 ms |
19324 KB |
Output is correct |
19 |
Correct |
25 ms |
19292 KB |
Output is correct |
20 |
Correct |
23 ms |
19320 KB |
Output is correct |
21 |
Correct |
19 ms |
19320 KB |
Output is correct |
22 |
Correct |
24 ms |
19324 KB |
Output is correct |
23 |
Correct |
27 ms |
19832 KB |
Output is correct |
24 |
Correct |
23 ms |
19872 KB |
Output is correct |
25 |
Correct |
23 ms |
19832 KB |
Output is correct |
26 |
Correct |
28 ms |
20404 KB |
Output is correct |
27 |
Correct |
34 ms |
20404 KB |
Output is correct |
28 |
Correct |
481 ms |
20404 KB |
Output is correct |
29 |
Correct |
213 ms |
20404 KB |
Output is correct |
30 |
Correct |
272 ms |
20408 KB |
Output is correct |
31 |
Correct |
177 ms |
20656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19320 KB |
Output is correct |
2 |
Correct |
23 ms |
19320 KB |
Output is correct |
3 |
Correct |
19 ms |
19320 KB |
Output is correct |
4 |
Correct |
19 ms |
19324 KB |
Output is correct |
5 |
Correct |
19 ms |
19320 KB |
Output is correct |
6 |
Correct |
23 ms |
19320 KB |
Output is correct |
7 |
Correct |
23 ms |
19320 KB |
Output is correct |
8 |
Correct |
23 ms |
19272 KB |
Output is correct |
9 |
Correct |
19 ms |
19320 KB |
Output is correct |
10 |
Correct |
19 ms |
19320 KB |
Output is correct |
11 |
Correct |
19 ms |
19320 KB |
Output is correct |
12 |
Correct |
19 ms |
19320 KB |
Output is correct |
13 |
Correct |
19 ms |
19320 KB |
Output is correct |
14 |
Correct |
19 ms |
19320 KB |
Output is correct |
15 |
Correct |
23 ms |
19320 KB |
Output is correct |
16 |
Correct |
23 ms |
19320 KB |
Output is correct |
17 |
Correct |
25 ms |
19320 KB |
Output is correct |
18 |
Correct |
23 ms |
19324 KB |
Output is correct |
19 |
Correct |
25 ms |
19292 KB |
Output is correct |
20 |
Correct |
23 ms |
19320 KB |
Output is correct |
21 |
Correct |
19 ms |
19320 KB |
Output is correct |
22 |
Correct |
24 ms |
19324 KB |
Output is correct |
23 |
Correct |
27 ms |
19832 KB |
Output is correct |
24 |
Correct |
23 ms |
19872 KB |
Output is correct |
25 |
Correct |
23 ms |
19832 KB |
Output is correct |
26 |
Correct |
28 ms |
20404 KB |
Output is correct |
27 |
Correct |
34 ms |
20404 KB |
Output is correct |
28 |
Correct |
481 ms |
20404 KB |
Output is correct |
29 |
Correct |
213 ms |
20404 KB |
Output is correct |
30 |
Correct |
272 ms |
20408 KB |
Output is correct |
31 |
Correct |
177 ms |
20656 KB |
Output is correct |
32 |
Correct |
74 ms |
25512 KB |
Output is correct |
33 |
Correct |
94 ms |
25516 KB |
Output is correct |
34 |
Correct |
88 ms |
25512 KB |
Output is correct |
35 |
Correct |
484 ms |
43996 KB |
Output is correct |
36 |
Correct |
389 ms |
47516 KB |
Output is correct |
37 |
Correct |
431 ms |
47640 KB |
Output is correct |
38 |
Execution timed out |
1072 ms |
47768 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |