#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
// SEGMENT TREE
long long T[N << 2], lz[N << 2];
#define mid ((l + r) >> 1)
void push(int v, int l, int r) {
if (lz[v] == 0) return;
if (l < r) lz[v << 1] += lz[v], lz[v << 1 | 1] += lz[v];
T[v] += lz[v]; lz[v] = 0;
return;
}
void upd(int v, int l, int r, int L, int R, long long val) {
push(v, l, r);
if (l > r || R < l || L > r) return;
if (L <= l && r <= R) { lz[v] += val; push(v, l, r); return; }
upd(v << 1, l, mid, L, R, val); upd(v << 1 | 1, mid + 1, r, L, R, val);
T[v] = T[v << 1] + T[v << 1 | 1];
}
long long get(int v, int l, int r, int pos) {
push(v, l, r);
if (l > r || l > pos || r < pos) return 0;
if (l == r) return T[v];
return get(v << 1, l, mid, pos) + get(v << 1 | 1, mid + 1, r, pos);
}
#undef mid
//------------------
struct Query { int u; int v; int w; };
int n, m;
vector<int> G[N];
int par[N][18], dep[N];
long long f[N], g[N];
int tin[N], tout[N], pos[N], TIME;
vector<Query> Q[N];
void dfs(int u, int p) {
tin[u] = ++TIME; pos[u] = TIME;
for (int v : G[u]) if (v != p) {
par[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v, u);
}
tout[u] = TIME;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 17; i >= 0; --i) if (dep[par[u][i]] >= dep[v]) u = par[u][i];
for (int i = 17; i >= 0; --i) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return u == v ? u : par[u][0];
}
void solve(int u, int p) {
for (int v : G[u]) if (v != p) solve(v, u), g[u] += f[v];
for (int v : G[u]) if (v != p) {
// h[v] = g[u] - f[v]
upd(1, 1, n, tin[v], tout[v], g[u] - f[v]);
}
f[u] = g[u];
for (auto &it : Q[u]) {
int x = it.u, y = it.v, c = it.w;
long long hx = get(1, 1, n, pos[x]);
long long hy = get(1, 1, n, pos[y]);
long long cur = hx + hy + g[x] + g[y] - g[u] + c;
f[u] = max(f[u], cur);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
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);
}
par[1][0] = 1;
dfs(1,1);
for (int j = 1; j < 18; ++j) for (int i = 1; i <= n; ++i) {
par[i][j] = par[par[i][j-1]][j-1];
}
cin >> m;
for (int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
int x = lca(u, v);
Q[x].push_back({u, v, w});
}
solve(1,1);
printf("%lld\n", f[1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
23272 KB |
Output is correct |
2 |
Correct |
0 ms |
23272 KB |
Output is correct |
3 |
Correct |
3 ms |
23272 KB |
Output is correct |
4 |
Correct |
0 ms |
23272 KB |
Output is correct |
5 |
Correct |
129 ms |
26572 KB |
Output is correct |
6 |
Correct |
89 ms |
33996 KB |
Output is correct |
7 |
Correct |
146 ms |
31300 KB |
Output is correct |
8 |
Correct |
103 ms |
26984 KB |
Output is correct |
9 |
Correct |
166 ms |
29752 KB |
Output is correct |
10 |
Correct |
126 ms |
26992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23272 KB |
Output is correct |
2 |
Correct |
0 ms |
23272 KB |
Output is correct |
3 |
Correct |
3 ms |
23272 KB |
Output is correct |
4 |
Correct |
266 ms |
36108 KB |
Output is correct |
5 |
Correct |
243 ms |
36108 KB |
Output is correct |
6 |
Correct |
249 ms |
36108 KB |
Output is correct |
7 |
Correct |
236 ms |
36112 KB |
Output is correct |
8 |
Correct |
249 ms |
36112 KB |
Output is correct |
9 |
Correct |
243 ms |
36104 KB |
Output is correct |
10 |
Correct |
236 ms |
36112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23272 KB |
Output is correct |
2 |
Correct |
0 ms |
23272 KB |
Output is correct |
3 |
Correct |
3 ms |
23272 KB |
Output is correct |
4 |
Correct |
266 ms |
36108 KB |
Output is correct |
5 |
Correct |
243 ms |
36108 KB |
Output is correct |
6 |
Correct |
249 ms |
36108 KB |
Output is correct |
7 |
Correct |
236 ms |
36112 KB |
Output is correct |
8 |
Correct |
249 ms |
36112 KB |
Output is correct |
9 |
Correct |
243 ms |
36104 KB |
Output is correct |
10 |
Correct |
236 ms |
36112 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
249 ms |
36108 KB |
Output is correct |
13 |
Correct |
246 ms |
36112 KB |
Output is correct |
14 |
Correct |
236 ms |
36112 KB |
Output is correct |
15 |
Correct |
249 ms |
36112 KB |
Output is correct |
16 |
Correct |
246 ms |
36108 KB |
Output is correct |
17 |
Correct |
229 ms |
36104 KB |
Output is correct |
18 |
Correct |
266 ms |
36108 KB |
Output is correct |
19 |
Correct |
246 ms |
36108 KB |
Output is correct |
20 |
Correct |
249 ms |
36108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
28268 KB |
Output is correct |
2 |
Correct |
213 ms |
36104 KB |
Output is correct |
3 |
Correct |
399 ms |
33052 KB |
Output is correct |
4 |
Correct |
226 ms |
28988 KB |
Output is correct |
5 |
Correct |
389 ms |
32596 KB |
Output is correct |
6 |
Correct |
249 ms |
29324 KB |
Output is correct |
7 |
Correct |
379 ms |
32256 KB |
Output is correct |
8 |
Correct |
303 ms |
28352 KB |
Output is correct |
9 |
Correct |
239 ms |
36108 KB |
Output is correct |
10 |
Correct |
393 ms |
31512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
23272 KB |
Output is correct |
2 |
Correct |
0 ms |
23272 KB |
Output is correct |
3 |
Correct |
3 ms |
23272 KB |
Output is correct |
4 |
Correct |
0 ms |
23272 KB |
Output is correct |
5 |
Correct |
129 ms |
26572 KB |
Output is correct |
6 |
Correct |
89 ms |
33996 KB |
Output is correct |
7 |
Correct |
146 ms |
31300 KB |
Output is correct |
8 |
Correct |
103 ms |
26984 KB |
Output is correct |
9 |
Correct |
166 ms |
29752 KB |
Output is correct |
10 |
Correct |
126 ms |
26992 KB |
Output is correct |
11 |
Correct |
3 ms |
23272 KB |
Output is correct |
12 |
Correct |
3 ms |
23272 KB |
Output is correct |
13 |
Correct |
3 ms |
23272 KB |
Output is correct |
14 |
Correct |
3 ms |
23272 KB |
Output is correct |
15 |
Correct |
3 ms |
23272 KB |
Output is correct |
16 |
Correct |
0 ms |
23272 KB |
Output is correct |
17 |
Correct |
0 ms |
23272 KB |
Output is correct |
18 |
Correct |
0 ms |
23272 KB |
Output is correct |
19 |
Correct |
0 ms |
23272 KB |
Output is correct |
20 |
Correct |
0 ms |
23272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
23272 KB |
Output is correct |
2 |
Correct |
0 ms |
23272 KB |
Output is correct |
3 |
Correct |
3 ms |
23272 KB |
Output is correct |
4 |
Correct |
0 ms |
23272 KB |
Output is correct |
5 |
Correct |
129 ms |
26572 KB |
Output is correct |
6 |
Correct |
89 ms |
33996 KB |
Output is correct |
7 |
Correct |
146 ms |
31300 KB |
Output is correct |
8 |
Correct |
103 ms |
26984 KB |
Output is correct |
9 |
Correct |
166 ms |
29752 KB |
Output is correct |
10 |
Correct |
126 ms |
26992 KB |
Output is correct |
11 |
Correct |
0 ms |
23272 KB |
Output is correct |
12 |
Correct |
0 ms |
23272 KB |
Output is correct |
13 |
Correct |
3 ms |
23272 KB |
Output is correct |
14 |
Correct |
266 ms |
36108 KB |
Output is correct |
15 |
Correct |
243 ms |
36108 KB |
Output is correct |
16 |
Correct |
249 ms |
36108 KB |
Output is correct |
17 |
Correct |
236 ms |
36112 KB |
Output is correct |
18 |
Correct |
249 ms |
36112 KB |
Output is correct |
19 |
Correct |
243 ms |
36104 KB |
Output is correct |
20 |
Correct |
236 ms |
36112 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
249 ms |
36108 KB |
Output is correct |
23 |
Correct |
246 ms |
36112 KB |
Output is correct |
24 |
Correct |
236 ms |
36112 KB |
Output is correct |
25 |
Correct |
249 ms |
36112 KB |
Output is correct |
26 |
Correct |
246 ms |
36108 KB |
Output is correct |
27 |
Correct |
229 ms |
36104 KB |
Output is correct |
28 |
Correct |
266 ms |
36108 KB |
Output is correct |
29 |
Correct |
246 ms |
36108 KB |
Output is correct |
30 |
Correct |
249 ms |
36108 KB |
Output is correct |
31 |
Correct |
289 ms |
28268 KB |
Output is correct |
32 |
Correct |
213 ms |
36104 KB |
Output is correct |
33 |
Correct |
399 ms |
33052 KB |
Output is correct |
34 |
Correct |
226 ms |
28988 KB |
Output is correct |
35 |
Correct |
389 ms |
32596 KB |
Output is correct |
36 |
Correct |
249 ms |
29324 KB |
Output is correct |
37 |
Correct |
379 ms |
32256 KB |
Output is correct |
38 |
Correct |
303 ms |
28352 KB |
Output is correct |
39 |
Correct |
239 ms |
36108 KB |
Output is correct |
40 |
Correct |
393 ms |
31512 KB |
Output is correct |
41 |
Correct |
3 ms |
23272 KB |
Output is correct |
42 |
Correct |
3 ms |
23272 KB |
Output is correct |
43 |
Correct |
3 ms |
23272 KB |
Output is correct |
44 |
Correct |
3 ms |
23272 KB |
Output is correct |
45 |
Correct |
3 ms |
23272 KB |
Output is correct |
46 |
Correct |
0 ms |
23272 KB |
Output is correct |
47 |
Correct |
0 ms |
23272 KB |
Output is correct |
48 |
Correct |
0 ms |
23272 KB |
Output is correct |
49 |
Correct |
0 ms |
23272 KB |
Output is correct |
50 |
Correct |
0 ms |
23272 KB |
Output is correct |
51 |
Correct |
303 ms |
28264 KB |
Output is correct |
52 |
Correct |
263 ms |
36104 KB |
Output is correct |
53 |
Correct |
403 ms |
31636 KB |
Output is correct |
54 |
Correct |
256 ms |
28792 KB |
Output is correct |
55 |
Correct |
293 ms |
28496 KB |
Output is correct |
56 |
Correct |
253 ms |
36108 KB |
Output is correct |
57 |
Correct |
353 ms |
32132 KB |
Output is correct |
58 |
Correct |
226 ms |
28820 KB |
Output is correct |
59 |
Correct |
299 ms |
28328 KB |
Output is correct |
60 |
Correct |
229 ms |
36108 KB |
Output is correct |
61 |
Correct |
406 ms |
32224 KB |
Output is correct |
62 |
Correct |
219 ms |
29416 KB |
Output is correct |
63 |
Correct |
279 ms |
28564 KB |
Output is correct |
64 |
Correct |
246 ms |
36108 KB |
Output is correct |
65 |
Correct |
373 ms |
32372 KB |
Output is correct |
66 |
Correct |
209 ms |
29024 KB |
Output is correct |
67 |
Correct |
273 ms |
28984 KB |
Output is correct |
68 |
Correct |
243 ms |
36108 KB |
Output is correct |
69 |
Correct |
386 ms |
30848 KB |
Output is correct |
70 |
Correct |
206 ms |
29088 KB |
Output is correct |