#include <bits/stdc++.h>
using namespace std;
vector<int> in, out, dep, dp, dp2;
vector<vector<int>> g, up;
vector<vector<tuple<int, int, int>>> qr;
int t, lg;
struct fenwick {
vector<int> fenw;
int n;
void init(int n_) {
fenw.resize(n = n_);
}
void upd(int u, int x) {
for (; u < n; u |= u + 1) fenw[u] += x;
}
int qry(int u) {
int s = 0;
for (; u >= 0; u = (u & (u + 1)) - 1) s += fenw[u];
return s;
}
int qry(int l, int r) {
return qry(r) - qry(l - 1);
}
} fen;
void dfs(int u) {
in[u] = t++;
for (int i = 1; i < lg; i++) up[i][u] = up[i - 1][up[i - 1][u]];
for (int v : g[u])
if (v ^ up[0][u])
dep[v] = dep[u] + 1, up[0][v] = u, dfs(v);
out[u] = t++;
}
void calc_dp(int u, int p) {
for (int v : g[u])
if (v ^ p) {
calc_dp(v, u);
dp2[u] += dp[v];
}
dp[u] = dp2[u];
for (auto [x, y, w] : qr[u]) {
int cost = dp2[u] - fen.qry(in[u], in[x]) - fen.qry(in[u], in[y]);
dp[u] = max(dp[u], cost + w);
}
fen.upd(in[u], dp[u] - dp2[u]);
fen.upd(out[u], dp2[u] - dp[u]);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int k = dep[u] - dep[v];
for (int i = 0; i < lg; i++)
if (k >> i & 1)
u = up[i][u];
if (u == v) return u;
for (int i = lg - 1; i >= 0; i--)
if (up[i][u] ^ up[i][v])
u = up[i][u], v = up[i][v];
return up[0][u];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
while ((1 << lg) <= n) ++lg;
g.resize(n), up = vector<vector<int>> (lg, vector<int> (n)), dp.resize(n), dp2.resize(n), dep.resize(n), qr.resize(n), in.resize(n), out.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
fen.init(t);
int q;
cin >> q;
while (q--) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
qr[lca(u, v)].push_back({u, v, w});
}
calc_dp(0, 0);
cout << dp[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
105 ms |
18768 KB |
Output is correct |
6 |
Correct |
62 ms |
26596 KB |
Output is correct |
7 |
Correct |
115 ms |
23816 KB |
Output is correct |
8 |
Correct |
92 ms |
19228 KB |
Output is correct |
9 |
Correct |
107 ms |
22192 KB |
Output is correct |
10 |
Correct |
77 ms |
19164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
135 ms |
29880 KB |
Output is correct |
5 |
Correct |
122 ms |
29784 KB |
Output is correct |
6 |
Correct |
112 ms |
29824 KB |
Output is correct |
7 |
Correct |
150 ms |
29824 KB |
Output is correct |
8 |
Correct |
121 ms |
29864 KB |
Output is correct |
9 |
Correct |
115 ms |
29852 KB |
Output is correct |
10 |
Correct |
131 ms |
29768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
135 ms |
29880 KB |
Output is correct |
5 |
Correct |
122 ms |
29784 KB |
Output is correct |
6 |
Correct |
112 ms |
29824 KB |
Output is correct |
7 |
Correct |
150 ms |
29824 KB |
Output is correct |
8 |
Correct |
121 ms |
29864 KB |
Output is correct |
9 |
Correct |
115 ms |
29852 KB |
Output is correct |
10 |
Correct |
131 ms |
29768 KB |
Output is correct |
11 |
Correct |
9 ms |
1236 KB |
Output is correct |
12 |
Correct |
134 ms |
30108 KB |
Output is correct |
13 |
Correct |
128 ms |
30140 KB |
Output is correct |
14 |
Correct |
132 ms |
30172 KB |
Output is correct |
15 |
Correct |
120 ms |
30092 KB |
Output is correct |
16 |
Correct |
105 ms |
30160 KB |
Output is correct |
17 |
Correct |
139 ms |
30020 KB |
Output is correct |
18 |
Correct |
139 ms |
30032 KB |
Output is correct |
19 |
Correct |
109 ms |
30176 KB |
Output is correct |
20 |
Correct |
115 ms |
30144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
21796 KB |
Output is correct |
2 |
Correct |
103 ms |
29868 KB |
Output is correct |
3 |
Correct |
228 ms |
26668 KB |
Output is correct |
4 |
Correct |
116 ms |
22424 KB |
Output is correct |
5 |
Correct |
161 ms |
26268 KB |
Output is correct |
6 |
Correct |
132 ms |
22564 KB |
Output is correct |
7 |
Correct |
225 ms |
25924 KB |
Output is correct |
8 |
Correct |
164 ms |
21824 KB |
Output is correct |
9 |
Correct |
109 ms |
29844 KB |
Output is correct |
10 |
Correct |
237 ms |
25052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
105 ms |
18768 KB |
Output is correct |
6 |
Correct |
62 ms |
26596 KB |
Output is correct |
7 |
Correct |
115 ms |
23816 KB |
Output is correct |
8 |
Correct |
92 ms |
19228 KB |
Output is correct |
9 |
Correct |
107 ms |
22192 KB |
Output is correct |
10 |
Correct |
77 ms |
19164 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
444 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
508 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
105 ms |
18768 KB |
Output is correct |
6 |
Correct |
62 ms |
26596 KB |
Output is correct |
7 |
Correct |
115 ms |
23816 KB |
Output is correct |
8 |
Correct |
92 ms |
19228 KB |
Output is correct |
9 |
Correct |
107 ms |
22192 KB |
Output is correct |
10 |
Correct |
77 ms |
19164 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
135 ms |
29880 KB |
Output is correct |
15 |
Correct |
122 ms |
29784 KB |
Output is correct |
16 |
Correct |
112 ms |
29824 KB |
Output is correct |
17 |
Correct |
150 ms |
29824 KB |
Output is correct |
18 |
Correct |
121 ms |
29864 KB |
Output is correct |
19 |
Correct |
115 ms |
29852 KB |
Output is correct |
20 |
Correct |
131 ms |
29768 KB |
Output is correct |
21 |
Correct |
9 ms |
1236 KB |
Output is correct |
22 |
Correct |
134 ms |
30108 KB |
Output is correct |
23 |
Correct |
128 ms |
30140 KB |
Output is correct |
24 |
Correct |
132 ms |
30172 KB |
Output is correct |
25 |
Correct |
120 ms |
30092 KB |
Output is correct |
26 |
Correct |
105 ms |
30160 KB |
Output is correct |
27 |
Correct |
139 ms |
30020 KB |
Output is correct |
28 |
Correct |
139 ms |
30032 KB |
Output is correct |
29 |
Correct |
109 ms |
30176 KB |
Output is correct |
30 |
Correct |
115 ms |
30144 KB |
Output is correct |
31 |
Correct |
158 ms |
21796 KB |
Output is correct |
32 |
Correct |
103 ms |
29868 KB |
Output is correct |
33 |
Correct |
228 ms |
26668 KB |
Output is correct |
34 |
Correct |
116 ms |
22424 KB |
Output is correct |
35 |
Correct |
161 ms |
26268 KB |
Output is correct |
36 |
Correct |
132 ms |
22564 KB |
Output is correct |
37 |
Correct |
225 ms |
25924 KB |
Output is correct |
38 |
Correct |
164 ms |
21824 KB |
Output is correct |
39 |
Correct |
109 ms |
29844 KB |
Output is correct |
40 |
Correct |
237 ms |
25052 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
444 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
508 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
2 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
1 ms |
456 KB |
Output is correct |
51 |
Correct |
166 ms |
22224 KB |
Output is correct |
52 |
Correct |
128 ms |
30120 KB |
Output is correct |
53 |
Correct |
191 ms |
25588 KB |
Output is correct |
54 |
Correct |
111 ms |
22368 KB |
Output is correct |
55 |
Correct |
157 ms |
22020 KB |
Output is correct |
56 |
Correct |
135 ms |
30144 KB |
Output is correct |
57 |
Correct |
181 ms |
26192 KB |
Output is correct |
58 |
Correct |
142 ms |
22468 KB |
Output is correct |
59 |
Correct |
143 ms |
22224 KB |
Output is correct |
60 |
Correct |
136 ms |
30016 KB |
Output is correct |
61 |
Correct |
199 ms |
26152 KB |
Output is correct |
62 |
Correct |
124 ms |
23140 KB |
Output is correct |
63 |
Correct |
163 ms |
22432 KB |
Output is correct |
64 |
Correct |
112 ms |
30072 KB |
Output is correct |
65 |
Correct |
204 ms |
26436 KB |
Output is correct |
66 |
Correct |
131 ms |
22340 KB |
Output is correct |
67 |
Correct |
146 ms |
22320 KB |
Output is correct |
68 |
Correct |
126 ms |
30104 KB |
Output is correct |
69 |
Correct |
169 ms |
24896 KB |
Output is correct |
70 |
Correct |
152 ms |
22572 KB |
Output is correct |