#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5, LG = 17;
int n, m;
int pr[LG][N], s[N], dp[N], L[N], R[N], dep[N];
vector<int> g[N];
vector<array<int, 3>> cands[N];
void upd(int i, int x) {
for (; i <= n; i += i & -i) {
s[i] += x;
}
}
void upd(int l, int r, int x) {
upd(l, x);
upd(r + 1, -x);
}
int qry(int i) {
int res = 0;
for (; i; i -= i & -i) {
res += s[i];
}
return res;
}
int timer;
void pre_dfs(int u) {
L[u] = ++timer;
for (int v : g[u]) {
if (v != pr[0][u]) {
dep[v] = dep[u] + 1;
pr[0][v] = u;
for (int j = 1; j < LG; ++j) {
pr[j][v] = pr[j - 1][pr[j - 1][v]];
}
pre_dfs(v);
}
}
R[u] = timer;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int j = LG - 1; ~j; --j) {
if (dep[u] - (1 << j) >= dep[v]) {
u = pr[j][u];
}
}
if (u == v) {
return u;
}
for (int j = LG - 1; ~j; --j) {
if (pr[j][u] != pr[j][v]) {
u = pr[j][u], v = pr[j][v];
}
}
return pr[0][u];
}
void dfs(int u) {
int sum = 0;
for (int v : g[u]) {
if (v != pr[0][u]) {
dfs(v);
sum += dp[v];
}
}
dp[u] = sum;
for (auto [x, y, w] : cands[u]) {
dp[u] = max(dp[u], qry(L[x]) + qry(L[y]) + w + sum);
}
upd(L[u], R[u], sum - dp[u]);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pre_dfs(1);
cin >> m;
while (m--) {
int u, v, w; cin >> u >> v >> w;
cands[lca(u, v)].push_back({u, v, w});
}
dfs(1);
cout << dp[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5164 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
41 ms |
18004 KB |
Output is correct |
6 |
Correct |
31 ms |
25756 KB |
Output is correct |
7 |
Correct |
47 ms |
23120 KB |
Output is correct |
8 |
Correct |
34 ms |
18516 KB |
Output is correct |
9 |
Correct |
46 ms |
21588 KB |
Output is correct |
10 |
Correct |
36 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5160 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
76 ms |
28948 KB |
Output is correct |
5 |
Correct |
92 ms |
29080 KB |
Output is correct |
6 |
Correct |
73 ms |
28956 KB |
Output is correct |
7 |
Correct |
77 ms |
29164 KB |
Output is correct |
8 |
Correct |
81 ms |
29008 KB |
Output is correct |
9 |
Correct |
68 ms |
29012 KB |
Output is correct |
10 |
Correct |
77 ms |
29008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5160 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
76 ms |
28948 KB |
Output is correct |
5 |
Correct |
92 ms |
29080 KB |
Output is correct |
6 |
Correct |
73 ms |
28956 KB |
Output is correct |
7 |
Correct |
77 ms |
29164 KB |
Output is correct |
8 |
Correct |
81 ms |
29008 KB |
Output is correct |
9 |
Correct |
68 ms |
29012 KB |
Output is correct |
10 |
Correct |
77 ms |
29008 KB |
Output is correct |
11 |
Correct |
8 ms |
6232 KB |
Output is correct |
12 |
Correct |
104 ms |
29268 KB |
Output is correct |
13 |
Correct |
80 ms |
29264 KB |
Output is correct |
14 |
Correct |
65 ms |
29276 KB |
Output is correct |
15 |
Correct |
84 ms |
29264 KB |
Output is correct |
16 |
Correct |
69 ms |
29272 KB |
Output is correct |
17 |
Correct |
78 ms |
29344 KB |
Output is correct |
18 |
Correct |
78 ms |
29268 KB |
Output is correct |
19 |
Correct |
65 ms |
29432 KB |
Output is correct |
20 |
Correct |
90 ms |
29520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
20748 KB |
Output is correct |
2 |
Correct |
64 ms |
29004 KB |
Output is correct |
3 |
Correct |
117 ms |
25936 KB |
Output is correct |
4 |
Correct |
67 ms |
21260 KB |
Output is correct |
5 |
Correct |
99 ms |
25636 KB |
Output is correct |
6 |
Correct |
77 ms |
21284 KB |
Output is correct |
7 |
Correct |
117 ms |
25232 KB |
Output is correct |
8 |
Correct |
74 ms |
21012 KB |
Output is correct |
9 |
Correct |
63 ms |
28956 KB |
Output is correct |
10 |
Correct |
116 ms |
24332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5164 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
41 ms |
18004 KB |
Output is correct |
6 |
Correct |
31 ms |
25756 KB |
Output is correct |
7 |
Correct |
47 ms |
23120 KB |
Output is correct |
8 |
Correct |
34 ms |
18516 KB |
Output is correct |
9 |
Correct |
46 ms |
21588 KB |
Output is correct |
10 |
Correct |
36 ms |
18524 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
3 ms |
5468 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5468 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5164 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
41 ms |
18004 KB |
Output is correct |
6 |
Correct |
31 ms |
25756 KB |
Output is correct |
7 |
Correct |
47 ms |
23120 KB |
Output is correct |
8 |
Correct |
34 ms |
18516 KB |
Output is correct |
9 |
Correct |
46 ms |
21588 KB |
Output is correct |
10 |
Correct |
36 ms |
18524 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
2 ms |
5464 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
76 ms |
28948 KB |
Output is correct |
15 |
Correct |
92 ms |
29080 KB |
Output is correct |
16 |
Correct |
73 ms |
28956 KB |
Output is correct |
17 |
Correct |
77 ms |
29164 KB |
Output is correct |
18 |
Correct |
81 ms |
29008 KB |
Output is correct |
19 |
Correct |
68 ms |
29012 KB |
Output is correct |
20 |
Correct |
77 ms |
29008 KB |
Output is correct |
21 |
Correct |
8 ms |
6232 KB |
Output is correct |
22 |
Correct |
104 ms |
29268 KB |
Output is correct |
23 |
Correct |
80 ms |
29264 KB |
Output is correct |
24 |
Correct |
65 ms |
29276 KB |
Output is correct |
25 |
Correct |
84 ms |
29264 KB |
Output is correct |
26 |
Correct |
69 ms |
29272 KB |
Output is correct |
27 |
Correct |
78 ms |
29344 KB |
Output is correct |
28 |
Correct |
78 ms |
29268 KB |
Output is correct |
29 |
Correct |
65 ms |
29432 KB |
Output is correct |
30 |
Correct |
90 ms |
29520 KB |
Output is correct |
31 |
Correct |
82 ms |
20748 KB |
Output is correct |
32 |
Correct |
64 ms |
29004 KB |
Output is correct |
33 |
Correct |
117 ms |
25936 KB |
Output is correct |
34 |
Correct |
67 ms |
21260 KB |
Output is correct |
35 |
Correct |
99 ms |
25636 KB |
Output is correct |
36 |
Correct |
77 ms |
21284 KB |
Output is correct |
37 |
Correct |
117 ms |
25232 KB |
Output is correct |
38 |
Correct |
74 ms |
21012 KB |
Output is correct |
39 |
Correct |
63 ms |
28956 KB |
Output is correct |
40 |
Correct |
116 ms |
24332 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
2 ms |
5468 KB |
Output is correct |
43 |
Correct |
3 ms |
5468 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
2 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5212 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5468 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5468 KB |
Output is correct |
51 |
Correct |
83 ms |
21328 KB |
Output is correct |
52 |
Correct |
85 ms |
29364 KB |
Output is correct |
53 |
Correct |
122 ms |
24584 KB |
Output is correct |
54 |
Correct |
60 ms |
21644 KB |
Output is correct |
55 |
Correct |
83 ms |
21256 KB |
Output is correct |
56 |
Correct |
80 ms |
29268 KB |
Output is correct |
57 |
Correct |
95 ms |
25428 KB |
Output is correct |
58 |
Correct |
67 ms |
21512 KB |
Output is correct |
59 |
Correct |
75 ms |
21332 KB |
Output is correct |
60 |
Correct |
84 ms |
29248 KB |
Output is correct |
61 |
Correct |
98 ms |
25400 KB |
Output is correct |
62 |
Correct |
71 ms |
21300 KB |
Output is correct |
63 |
Correct |
89 ms |
21120 KB |
Output is correct |
64 |
Correct |
85 ms |
29360 KB |
Output is correct |
65 |
Correct |
172 ms |
25272 KB |
Output is correct |
66 |
Correct |
70 ms |
21560 KB |
Output is correct |
67 |
Correct |
85 ms |
21188 KB |
Output is correct |
68 |
Correct |
77 ms |
29268 KB |
Output is correct |
69 |
Correct |
94 ms |
24252 KB |
Output is correct |
70 |
Correct |
67 ms |
21700 KB |
Output is correct |