#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int N = 1e5 + 69;
const int K = 17;
int up[N][K];
int n, m, st[4 * N], id[N], sz[N], dep[N], top[N];
int dp[N];
vector<int> g[N];
vector<tuple<int, int, int>> q[N];
int tin[N], tout[N], timer;
int anc(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int Lca(int a, int b) {
if (anc(a, b))
return a;
else if (anc(b, a))
return b;
for (int i = K - 1;i >= 0;i--) {
if (!anc(up[a][i], b) && up[a][i] > 0)
a = up[a][i];
}
return up[a][0];
}
void dfs(int v, int p) {
up[v][0] = p;
tin[v] = ++timer;
dep[v] = dep[p] + 1;
sz[v] = 1;
for (int i = 1;i < K;i++)
up[v][i] = up[up[v][i - 1]][i - 1];
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
sz[v] += sz[u];
}
tout[v] = timer;
}
int x;
void hld(int v, int p, int c) {
top[v] = c;
id[v] = ++x;
int mx = 0, s = -1;
for (auto u : g[v]) {
if (u == p)
continue;
if (sz[u] > mx) {
mx = sz[u];
s = u;
}
}
if (s == -1)
return;
hld(s, v, c);
for (auto u : g[v]) {
if (u == p || u == s)
continue;
hld(u, v, u);
}
}
void modify(int v, int tl, int tr, int p, int x) {
if (tl == tr)
st[v] = x;
else {
int mid = (tl + tr) / 2;
if (p <= mid)
modify(v * 2, tl, mid, p, x);
else
modify(v * 2 + 1, mid + 1, tr, p, x);
st[v] = st[v * 2] + st[v * 2 + 1];
}
}
int Get(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 0;
else if (tl >= l && tr <= r)
return st[v];
else {
int mid = (tl + tr) / 2;
return Get(v * 2, tl, mid, l, r) + Get(v * 2 + 1, mid + 1, tr, l, r);
}
}
int Get(int x, int y) {
int res = 0;
while (top[x] != top[y]) {
res += Get(1, 1, n, id[top[x]], id[x]);
x = up[top[x]][0];
}
res += Get(1, 1, n, id[y], id[x]);
return res;
}
void solve(int v, int p) {
int s = 0;
for (auto u : g[v]) {
if (u == p)
continue;
solve(u, v);
s += dp[u];
}
dp[v] = s;
for (auto u : q[v]) {
int a, b, c;
tie(a, b, c) = u;
dp[v] = max(dp[v], s + Get(a, v) + Get(b, v) + c);
}
modify(1, 1, n, id[v], s - dp[v]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 0);
hld(1, 0, 1);
cin >> m;
for (int i = 1;i <= m;i++) {
int a, b, c;
cin >> a >> b >> c;
int lca = Lca(a, b);
q[lca].push_back({a, b, c});
}
solve(1, 0);
cout << dp[1] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10672 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
98 ms |
31940 KB |
Output is correct |
6 |
Correct |
47 ms |
38996 KB |
Output is correct |
7 |
Correct |
98 ms |
36692 KB |
Output is correct |
8 |
Correct |
101 ms |
32284 KB |
Output is correct |
9 |
Correct |
96 ms |
35140 KB |
Output is correct |
10 |
Correct |
69 ms |
32084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
107 ms |
43784 KB |
Output is correct |
5 |
Correct |
106 ms |
43860 KB |
Output is correct |
6 |
Correct |
95 ms |
43860 KB |
Output is correct |
7 |
Correct |
115 ms |
44176 KB |
Output is correct |
8 |
Correct |
112 ms |
43860 KB |
Output is correct |
9 |
Correct |
99 ms |
44064 KB |
Output is correct |
10 |
Correct |
108 ms |
43860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
107 ms |
43784 KB |
Output is correct |
5 |
Correct |
106 ms |
43860 KB |
Output is correct |
6 |
Correct |
95 ms |
43860 KB |
Output is correct |
7 |
Correct |
115 ms |
44176 KB |
Output is correct |
8 |
Correct |
112 ms |
43860 KB |
Output is correct |
9 |
Correct |
99 ms |
44064 KB |
Output is correct |
10 |
Correct |
108 ms |
43860 KB |
Output is correct |
11 |
Correct |
16 ms |
12124 KB |
Output is correct |
12 |
Correct |
108 ms |
44292 KB |
Output is correct |
13 |
Correct |
110 ms |
44296 KB |
Output is correct |
14 |
Correct |
100 ms |
44224 KB |
Output is correct |
15 |
Correct |
115 ms |
44540 KB |
Output is correct |
16 |
Correct |
98 ms |
44116 KB |
Output is correct |
17 |
Correct |
106 ms |
44124 KB |
Output is correct |
18 |
Correct |
106 ms |
44300 KB |
Output is correct |
19 |
Correct |
106 ms |
44164 KB |
Output is correct |
20 |
Correct |
108 ms |
44116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
278 ms |
36364 KB |
Output is correct |
2 |
Correct |
112 ms |
44116 KB |
Output is correct |
3 |
Correct |
215 ms |
41236 KB |
Output is correct |
4 |
Correct |
180 ms |
36788 KB |
Output is correct |
5 |
Correct |
199 ms |
40668 KB |
Output is correct |
6 |
Correct |
170 ms |
36544 KB |
Output is correct |
7 |
Correct |
220 ms |
40176 KB |
Output is correct |
8 |
Correct |
212 ms |
36460 KB |
Output is correct |
9 |
Correct |
95 ms |
43860 KB |
Output is correct |
10 |
Correct |
215 ms |
39236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10672 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
98 ms |
31940 KB |
Output is correct |
6 |
Correct |
47 ms |
38996 KB |
Output is correct |
7 |
Correct |
98 ms |
36692 KB |
Output is correct |
8 |
Correct |
101 ms |
32284 KB |
Output is correct |
9 |
Correct |
96 ms |
35140 KB |
Output is correct |
10 |
Correct |
69 ms |
32084 KB |
Output is correct |
11 |
Correct |
4 ms |
10844 KB |
Output is correct |
12 |
Correct |
4 ms |
10844 KB |
Output is correct |
13 |
Correct |
4 ms |
10844 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
4 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
4 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10672 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
98 ms |
31940 KB |
Output is correct |
6 |
Correct |
47 ms |
38996 KB |
Output is correct |
7 |
Correct |
98 ms |
36692 KB |
Output is correct |
8 |
Correct |
101 ms |
32284 KB |
Output is correct |
9 |
Correct |
96 ms |
35140 KB |
Output is correct |
10 |
Correct |
69 ms |
32084 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
107 ms |
43784 KB |
Output is correct |
15 |
Correct |
106 ms |
43860 KB |
Output is correct |
16 |
Correct |
95 ms |
43860 KB |
Output is correct |
17 |
Correct |
115 ms |
44176 KB |
Output is correct |
18 |
Correct |
112 ms |
43860 KB |
Output is correct |
19 |
Correct |
99 ms |
44064 KB |
Output is correct |
20 |
Correct |
108 ms |
43860 KB |
Output is correct |
21 |
Correct |
16 ms |
12124 KB |
Output is correct |
22 |
Correct |
108 ms |
44292 KB |
Output is correct |
23 |
Correct |
110 ms |
44296 KB |
Output is correct |
24 |
Correct |
100 ms |
44224 KB |
Output is correct |
25 |
Correct |
115 ms |
44540 KB |
Output is correct |
26 |
Correct |
98 ms |
44116 KB |
Output is correct |
27 |
Correct |
106 ms |
44124 KB |
Output is correct |
28 |
Correct |
106 ms |
44300 KB |
Output is correct |
29 |
Correct |
106 ms |
44164 KB |
Output is correct |
30 |
Correct |
108 ms |
44116 KB |
Output is correct |
31 |
Correct |
278 ms |
36364 KB |
Output is correct |
32 |
Correct |
112 ms |
44116 KB |
Output is correct |
33 |
Correct |
215 ms |
41236 KB |
Output is correct |
34 |
Correct |
180 ms |
36788 KB |
Output is correct |
35 |
Correct |
199 ms |
40668 KB |
Output is correct |
36 |
Correct |
170 ms |
36544 KB |
Output is correct |
37 |
Correct |
220 ms |
40176 KB |
Output is correct |
38 |
Correct |
212 ms |
36460 KB |
Output is correct |
39 |
Correct |
95 ms |
43860 KB |
Output is correct |
40 |
Correct |
215 ms |
39236 KB |
Output is correct |
41 |
Correct |
4 ms |
10844 KB |
Output is correct |
42 |
Correct |
4 ms |
10844 KB |
Output is correct |
43 |
Correct |
4 ms |
10844 KB |
Output is correct |
44 |
Correct |
4 ms |
10844 KB |
Output is correct |
45 |
Correct |
4 ms |
10844 KB |
Output is correct |
46 |
Correct |
4 ms |
10844 KB |
Output is correct |
47 |
Correct |
4 ms |
10844 KB |
Output is correct |
48 |
Correct |
3 ms |
10844 KB |
Output is correct |
49 |
Correct |
4 ms |
10844 KB |
Output is correct |
50 |
Correct |
3 ms |
10844 KB |
Output is correct |
51 |
Correct |
229 ms |
36584 KB |
Output is correct |
52 |
Correct |
108 ms |
44228 KB |
Output is correct |
53 |
Correct |
208 ms |
39560 KB |
Output is correct |
54 |
Correct |
165 ms |
36860 KB |
Output is correct |
55 |
Correct |
286 ms |
36452 KB |
Output is correct |
56 |
Correct |
113 ms |
44200 KB |
Output is correct |
57 |
Correct |
192 ms |
40500 KB |
Output is correct |
58 |
Correct |
177 ms |
36616 KB |
Output is correct |
59 |
Correct |
209 ms |
36644 KB |
Output is correct |
60 |
Correct |
107 ms |
44080 KB |
Output is correct |
61 |
Correct |
172 ms |
40592 KB |
Output is correct |
62 |
Correct |
191 ms |
36648 KB |
Output is correct |
63 |
Correct |
289 ms |
36204 KB |
Output is correct |
64 |
Correct |
106 ms |
44116 KB |
Output is correct |
65 |
Correct |
218 ms |
40196 KB |
Output is correct |
66 |
Correct |
170 ms |
36652 KB |
Output is correct |
67 |
Correct |
297 ms |
36288 KB |
Output is correct |
68 |
Correct |
106 ms |
44168 KB |
Output is correct |
69 |
Correct |
185 ms |
39312 KB |
Output is correct |
70 |
Correct |
177 ms |
36816 KB |
Output is correct |