#include <bits/stdc++.h>
using namespace std;
const int kN = 1e5;
const int kLog = 16;
int n, timer, tin[1 + kN], tout[1 + kN], depth[1 + kN], anc[1 + kN][1 + kLog];
vector<int> g[1 + kN];
vector<tuple<int, int, int>> paths[1 + kN];
int64_t ans, aib[1 + kN], sum[1 + kN];
void maxSelf(int64_t &x, int64_t y) {
if (x < y) {
x = y;
}
}
void update(int64_t aib[], int x, int v) {
for (int i = x; i <= n; i += i & -i) {
aib[i] += v;
}
}
int64_t query(int64_t aib[], int x) {
int64_t ans = 0;
for (int i = x; i > 0; i = i & (i - 1)) {
ans += aib[i];
}
return ans;
}
int64_t query(int64_t aib[], int l, int r) {
return query(aib, r) - query(aib, l);
}
void dfs1(int u) {
tin[u] = ++timer;
for (int i = 1; i <= kLog; ++i) {
anc[u][i] = anc[anc[u][i - 1]][i - 1];
if (anc[u][i] == 0) {
break;
}
}
for (int v : g[u]) {
if (v != anc[u][0]) {
anc[v][0] = u;
depth[v] = depth[u] + 1;
dfs1(v);
}
}
tout[u] = timer;
}
int getLca(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
for (int i = kLog; i >= 0; --i) {
if (depth[u] - (1 << i) >= depth[v]) {
u = anc[u][i];
}
}
if (u == v) {
return u;
}
for (int i = kLog; i >= 0; --i) {
if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
}
return anc[u][0];
}
int64_t dfs2(int u) {
int64_t sumChildren = 0;
for (int v : g[u]) {
if (v != anc[u][0]) {
sumChildren += dfs2(v);
}
}
update(sum, tin[u], sumChildren);
update(sum, tout[u] + 1, -sumChildren);
int64_t dp = sumChildren;
for (auto it : paths[u]) {
int p, q, w;
tie(p, q, w) = it;
maxSelf(dp, query(sum, tin[u], tin[p]) + query(sum, tin[u], tin[q]) + sumChildren + w
- query(aib, tin[u], tin[p]) - query(aib, tin[u], tin[q]));
}
update(aib, tin[u], dp);
update(aib, tout[u] + 1, -dp);
return dp;
}
void testCase() {
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
depth[1] = 1;
dfs1(1);
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
paths[getLca(u, v)].emplace_back(u, v, w);
}
cout << dfs2(1) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
81 ms |
18828 KB |
Output is correct |
6 |
Correct |
42 ms |
26484 KB |
Output is correct |
7 |
Correct |
78 ms |
23784 KB |
Output is correct |
8 |
Correct |
55 ms |
19092 KB |
Output is correct |
9 |
Correct |
73 ms |
22188 KB |
Output is correct |
10 |
Correct |
56 ms |
19180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5196 KB |
Output is correct |
4 |
Correct |
95 ms |
27292 KB |
Output is correct |
5 |
Correct |
105 ms |
27304 KB |
Output is correct |
6 |
Correct |
87 ms |
27316 KB |
Output is correct |
7 |
Correct |
96 ms |
27348 KB |
Output is correct |
8 |
Correct |
104 ms |
27348 KB |
Output is correct |
9 |
Correct |
86 ms |
27296 KB |
Output is correct |
10 |
Correct |
99 ms |
27432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5196 KB |
Output is correct |
4 |
Correct |
95 ms |
27292 KB |
Output is correct |
5 |
Correct |
105 ms |
27304 KB |
Output is correct |
6 |
Correct |
87 ms |
27316 KB |
Output is correct |
7 |
Correct |
96 ms |
27348 KB |
Output is correct |
8 |
Correct |
104 ms |
27348 KB |
Output is correct |
9 |
Correct |
86 ms |
27296 KB |
Output is correct |
10 |
Correct |
99 ms |
27432 KB |
Output is correct |
11 |
Correct |
11 ms |
5708 KB |
Output is correct |
12 |
Correct |
100 ms |
27320 KB |
Output is correct |
13 |
Correct |
100 ms |
27276 KB |
Output is correct |
14 |
Correct |
89 ms |
27280 KB |
Output is correct |
15 |
Correct |
98 ms |
27352 KB |
Output is correct |
16 |
Correct |
103 ms |
27336 KB |
Output is correct |
17 |
Correct |
97 ms |
27256 KB |
Output is correct |
18 |
Correct |
104 ms |
27272 KB |
Output is correct |
19 |
Correct |
89 ms |
27332 KB |
Output is correct |
20 |
Correct |
99 ms |
27292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
19160 KB |
Output is correct |
2 |
Correct |
90 ms |
29732 KB |
Output is correct |
3 |
Correct |
164 ms |
26624 KB |
Output is correct |
4 |
Correct |
93 ms |
22012 KB |
Output is correct |
5 |
Correct |
139 ms |
26156 KB |
Output is correct |
6 |
Correct |
95 ms |
22036 KB |
Output is correct |
7 |
Correct |
156 ms |
25852 KB |
Output is correct |
8 |
Correct |
127 ms |
21792 KB |
Output is correct |
9 |
Correct |
87 ms |
29764 KB |
Output is correct |
10 |
Correct |
156 ms |
24952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
81 ms |
18828 KB |
Output is correct |
6 |
Correct |
42 ms |
26484 KB |
Output is correct |
7 |
Correct |
78 ms |
23784 KB |
Output is correct |
8 |
Correct |
55 ms |
19092 KB |
Output is correct |
9 |
Correct |
73 ms |
22188 KB |
Output is correct |
10 |
Correct |
56 ms |
19180 KB |
Output is correct |
11 |
Correct |
4 ms |
5156 KB |
Output is correct |
12 |
Correct |
3 ms |
5160 KB |
Output is correct |
13 |
Correct |
3 ms |
5196 KB |
Output is correct |
14 |
Correct |
3 ms |
5196 KB |
Output is correct |
15 |
Correct |
3 ms |
5196 KB |
Output is correct |
16 |
Correct |
3 ms |
5196 KB |
Output is correct |
17 |
Correct |
4 ms |
5196 KB |
Output is correct |
18 |
Correct |
3 ms |
5196 KB |
Output is correct |
19 |
Correct |
4 ms |
5196 KB |
Output is correct |
20 |
Correct |
3 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
81 ms |
18828 KB |
Output is correct |
6 |
Correct |
42 ms |
26484 KB |
Output is correct |
7 |
Correct |
78 ms |
23784 KB |
Output is correct |
8 |
Correct |
55 ms |
19092 KB |
Output is correct |
9 |
Correct |
73 ms |
22188 KB |
Output is correct |
10 |
Correct |
56 ms |
19180 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5196 KB |
Output is correct |
14 |
Correct |
95 ms |
27292 KB |
Output is correct |
15 |
Correct |
105 ms |
27304 KB |
Output is correct |
16 |
Correct |
87 ms |
27316 KB |
Output is correct |
17 |
Correct |
96 ms |
27348 KB |
Output is correct |
18 |
Correct |
104 ms |
27348 KB |
Output is correct |
19 |
Correct |
86 ms |
27296 KB |
Output is correct |
20 |
Correct |
99 ms |
27432 KB |
Output is correct |
21 |
Correct |
11 ms |
5708 KB |
Output is correct |
22 |
Correct |
100 ms |
27320 KB |
Output is correct |
23 |
Correct |
100 ms |
27276 KB |
Output is correct |
24 |
Correct |
89 ms |
27280 KB |
Output is correct |
25 |
Correct |
98 ms |
27352 KB |
Output is correct |
26 |
Correct |
103 ms |
27336 KB |
Output is correct |
27 |
Correct |
97 ms |
27256 KB |
Output is correct |
28 |
Correct |
104 ms |
27272 KB |
Output is correct |
29 |
Correct |
89 ms |
27332 KB |
Output is correct |
30 |
Correct |
99 ms |
27292 KB |
Output is correct |
31 |
Correct |
117 ms |
19160 KB |
Output is correct |
32 |
Correct |
90 ms |
29732 KB |
Output is correct |
33 |
Correct |
164 ms |
26624 KB |
Output is correct |
34 |
Correct |
93 ms |
22012 KB |
Output is correct |
35 |
Correct |
139 ms |
26156 KB |
Output is correct |
36 |
Correct |
95 ms |
22036 KB |
Output is correct |
37 |
Correct |
156 ms |
25852 KB |
Output is correct |
38 |
Correct |
127 ms |
21792 KB |
Output is correct |
39 |
Correct |
87 ms |
29764 KB |
Output is correct |
40 |
Correct |
156 ms |
24952 KB |
Output is correct |
41 |
Correct |
4 ms |
5156 KB |
Output is correct |
42 |
Correct |
3 ms |
5160 KB |
Output is correct |
43 |
Correct |
3 ms |
5196 KB |
Output is correct |
44 |
Correct |
3 ms |
5196 KB |
Output is correct |
45 |
Correct |
3 ms |
5196 KB |
Output is correct |
46 |
Correct |
3 ms |
5196 KB |
Output is correct |
47 |
Correct |
4 ms |
5196 KB |
Output is correct |
48 |
Correct |
3 ms |
5196 KB |
Output is correct |
49 |
Correct |
4 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5196 KB |
Output is correct |
51 |
Correct |
137 ms |
22084 KB |
Output is correct |
52 |
Correct |
118 ms |
30000 KB |
Output is correct |
53 |
Correct |
175 ms |
25340 KB |
Output is correct |
54 |
Correct |
91 ms |
22272 KB |
Output is correct |
55 |
Correct |
127 ms |
21948 KB |
Output is correct |
56 |
Correct |
102 ms |
30076 KB |
Output is correct |
57 |
Correct |
159 ms |
26028 KB |
Output is correct |
58 |
Correct |
95 ms |
22264 KB |
Output is correct |
59 |
Correct |
131 ms |
22080 KB |
Output is correct |
60 |
Correct |
97 ms |
30016 KB |
Output is correct |
61 |
Correct |
157 ms |
26180 KB |
Output is correct |
62 |
Correct |
92 ms |
22048 KB |
Output is correct |
63 |
Correct |
126 ms |
21680 KB |
Output is correct |
64 |
Correct |
101 ms |
29980 KB |
Output is correct |
65 |
Correct |
166 ms |
25964 KB |
Output is correct |
66 |
Correct |
93 ms |
22256 KB |
Output is correct |
67 |
Correct |
119 ms |
21788 KB |
Output is correct |
68 |
Correct |
113 ms |
30276 KB |
Output is correct |
69 |
Correct |
149 ms |
24836 KB |
Output is correct |
70 |
Correct |
92 ms |
22428 KB |
Output is correct |