#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int Mxn = 1e5 + 1;
const int mxn = 2e5 + 1;
const int LOGN = 17;
//binary lifting :clown:
int tin[Mxn], tout[Mxn], cnt = 1, ST[LOGN + 1][Mxn], depth[Mxn];
vector<int> g[Mxn];
void build(int node = 1, int parent = 1) {
ST[0][node] = parent; depth[node] = depth[parent] + 1;
for (int i = 1; i <= LOGN; i++) {
ST[i][node] = ST[i - 1][ST[i - 1][node]];
}
tin[node] = cnt++;
for (auto child : g[node]) {
if (child == parent) continue;
build(child, node);
}
tout[node] = cnt++;
}
int jump(int A, int k) {
for (int i = 0; i <= LOGN; i++) {
if ((k>>i)&1) A = ST[i][A];
}
return A;
}
int LCA(int A, int B) {
if (depth[A] < depth[B]) swap(A, B);
A = jump(A, depth[A] - depth[B]);
if (A == B) return A;
for (int i = LOGN; i >= 0; i--) {
if (ST[i][A] != ST[i][B]) A = ST[i][A], B = ST[i][B];
}
return ST[0][A];
}
//fenwi
struct fenwi {
ll BIT[mxn];
void upd(int i, ll val = 0) {
for (; i < mxn; i += i&-i) BIT[i] += val;
}
ll sum(int i, ll val = 0) {
for (; i > 0; i -= i&-i) val += BIT[i];
return val;
}
ll query(int a, int b, int c) {
return sum(tin[a]) + sum(tin[b]) - 2*sum(tin[c]);
}
} bit[2];
//solve
struct query {int a, b, c;};
ll dp[Mxn], children_sum[Mxn];
vector<query> Q[Mxn];
void dfs(int node = 1, int parent = 1) {
children_sum[node] = 0;
for (auto child : g[node]) {
if (child == parent) continue;
dfs(child, node);
children_sum[node] += dp[child];
}
dp[node] = children_sum[node];
bit[0].upd(tin[node], children_sum[node]);
bit[0].upd(tout[node], -children_sum[node]);
for (auto [a, b, c] : Q[node]) {
dp[node] = max(dp[node], bit[0].query(a, b, node) + children_sum[node] - bit[1].query(a, b, node) + c);
}
bit[1].upd(tin[node], dp[node]);
bit[1].upd(tout[node],-dp[node]);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n;
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
cin >> m; build();
for (int i = 0; i < m; i++) {
int x, y, c; cin >> x >> y >> c;
Q[LCA(x, y)].push_back({x, y, c});
}
dfs();
cout << dp[1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
95 ms |
21132 KB |
Output is correct |
6 |
Correct |
43 ms |
30364 KB |
Output is correct |
7 |
Correct |
83 ms |
27076 KB |
Output is correct |
8 |
Correct |
77 ms |
21664 KB |
Output is correct |
9 |
Correct |
89 ms |
25328 KB |
Output is correct |
10 |
Correct |
69 ms |
21452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
100 ms |
32344 KB |
Output is correct |
5 |
Correct |
96 ms |
32404 KB |
Output is correct |
6 |
Correct |
102 ms |
32508 KB |
Output is correct |
7 |
Correct |
97 ms |
32420 KB |
Output is correct |
8 |
Correct |
100 ms |
32408 KB |
Output is correct |
9 |
Correct |
89 ms |
32476 KB |
Output is correct |
10 |
Correct |
101 ms |
32368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
100 ms |
32344 KB |
Output is correct |
5 |
Correct |
96 ms |
32404 KB |
Output is correct |
6 |
Correct |
102 ms |
32508 KB |
Output is correct |
7 |
Correct |
97 ms |
32420 KB |
Output is correct |
8 |
Correct |
100 ms |
32408 KB |
Output is correct |
9 |
Correct |
89 ms |
32476 KB |
Output is correct |
10 |
Correct |
101 ms |
32368 KB |
Output is correct |
11 |
Correct |
10 ms |
5844 KB |
Output is correct |
12 |
Correct |
105 ms |
32496 KB |
Output is correct |
13 |
Correct |
101 ms |
32352 KB |
Output is correct |
14 |
Correct |
91 ms |
32460 KB |
Output is correct |
15 |
Correct |
104 ms |
32432 KB |
Output is correct |
16 |
Correct |
88 ms |
32404 KB |
Output is correct |
17 |
Correct |
96 ms |
32380 KB |
Output is correct |
18 |
Correct |
96 ms |
32352 KB |
Output is correct |
19 |
Correct |
92 ms |
32384 KB |
Output is correct |
20 |
Correct |
97 ms |
32336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
22612 KB |
Output is correct |
2 |
Correct |
99 ms |
32408 KB |
Output is correct |
3 |
Correct |
166 ms |
29644 KB |
Output is correct |
4 |
Correct |
125 ms |
25580 KB |
Output is correct |
5 |
Correct |
164 ms |
30592 KB |
Output is correct |
6 |
Correct |
115 ms |
25488 KB |
Output is correct |
7 |
Correct |
184 ms |
30228 KB |
Output is correct |
8 |
Correct |
177 ms |
25360 KB |
Output is correct |
9 |
Correct |
98 ms |
34888 KB |
Output is correct |
10 |
Correct |
179 ms |
29068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
95 ms |
21132 KB |
Output is correct |
6 |
Correct |
43 ms |
30364 KB |
Output is correct |
7 |
Correct |
83 ms |
27076 KB |
Output is correct |
8 |
Correct |
77 ms |
21664 KB |
Output is correct |
9 |
Correct |
89 ms |
25328 KB |
Output is correct |
10 |
Correct |
69 ms |
21452 KB |
Output is correct |
11 |
Correct |
4 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5460 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
3 ms |
5332 KB |
Output is correct |
15 |
Correct |
4 ms |
5372 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
4 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
95 ms |
21132 KB |
Output is correct |
6 |
Correct |
43 ms |
30364 KB |
Output is correct |
7 |
Correct |
83 ms |
27076 KB |
Output is correct |
8 |
Correct |
77 ms |
21664 KB |
Output is correct |
9 |
Correct |
89 ms |
25328 KB |
Output is correct |
10 |
Correct |
69 ms |
21452 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
100 ms |
32344 KB |
Output is correct |
15 |
Correct |
96 ms |
32404 KB |
Output is correct |
16 |
Correct |
102 ms |
32508 KB |
Output is correct |
17 |
Correct |
97 ms |
32420 KB |
Output is correct |
18 |
Correct |
100 ms |
32408 KB |
Output is correct |
19 |
Correct |
89 ms |
32476 KB |
Output is correct |
20 |
Correct |
101 ms |
32368 KB |
Output is correct |
21 |
Correct |
10 ms |
5844 KB |
Output is correct |
22 |
Correct |
105 ms |
32496 KB |
Output is correct |
23 |
Correct |
101 ms |
32352 KB |
Output is correct |
24 |
Correct |
91 ms |
32460 KB |
Output is correct |
25 |
Correct |
104 ms |
32432 KB |
Output is correct |
26 |
Correct |
88 ms |
32404 KB |
Output is correct |
27 |
Correct |
96 ms |
32380 KB |
Output is correct |
28 |
Correct |
96 ms |
32352 KB |
Output is correct |
29 |
Correct |
92 ms |
32384 KB |
Output is correct |
30 |
Correct |
97 ms |
32336 KB |
Output is correct |
31 |
Correct |
167 ms |
22612 KB |
Output is correct |
32 |
Correct |
99 ms |
32408 KB |
Output is correct |
33 |
Correct |
166 ms |
29644 KB |
Output is correct |
34 |
Correct |
125 ms |
25580 KB |
Output is correct |
35 |
Correct |
164 ms |
30592 KB |
Output is correct |
36 |
Correct |
115 ms |
25488 KB |
Output is correct |
37 |
Correct |
184 ms |
30228 KB |
Output is correct |
38 |
Correct |
177 ms |
25360 KB |
Output is correct |
39 |
Correct |
98 ms |
34888 KB |
Output is correct |
40 |
Correct |
179 ms |
29068 KB |
Output is correct |
41 |
Correct |
4 ms |
5332 KB |
Output is correct |
42 |
Correct |
3 ms |
5460 KB |
Output is correct |
43 |
Correct |
3 ms |
5332 KB |
Output is correct |
44 |
Correct |
3 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5372 KB |
Output is correct |
46 |
Correct |
4 ms |
5332 KB |
Output is correct |
47 |
Correct |
3 ms |
5332 KB |
Output is correct |
48 |
Correct |
4 ms |
5332 KB |
Output is correct |
49 |
Correct |
3 ms |
5332 KB |
Output is correct |
50 |
Correct |
4 ms |
5460 KB |
Output is correct |
51 |
Correct |
149 ms |
25616 KB |
Output is correct |
52 |
Correct |
106 ms |
35332 KB |
Output is correct |
53 |
Correct |
168 ms |
29572 KB |
Output is correct |
54 |
Correct |
107 ms |
25808 KB |
Output is correct |
55 |
Correct |
137 ms |
25472 KB |
Output is correct |
56 |
Correct |
102 ms |
35168 KB |
Output is correct |
57 |
Correct |
164 ms |
30384 KB |
Output is correct |
58 |
Correct |
127 ms |
25784 KB |
Output is correct |
59 |
Correct |
186 ms |
25656 KB |
Output is correct |
60 |
Correct |
104 ms |
35188 KB |
Output is correct |
61 |
Correct |
175 ms |
30496 KB |
Output is correct |
62 |
Correct |
116 ms |
25576 KB |
Output is correct |
63 |
Correct |
148 ms |
25216 KB |
Output is correct |
64 |
Correct |
109 ms |
35164 KB |
Output is correct |
65 |
Correct |
184 ms |
30316 KB |
Output is correct |
66 |
Correct |
137 ms |
25860 KB |
Output is correct |
67 |
Correct |
147 ms |
25348 KB |
Output is correct |
68 |
Correct |
100 ms |
35156 KB |
Output is correct |
69 |
Correct |
168 ms |
28976 KB |
Output is correct |
70 |
Correct |
114 ms |
25852 KB |
Output is correct |