#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
#include <functional>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define MAX_N (1<<17)
#define pb push_back
#define _1 first
#define _2 second
struct HLDecomposition {
int N;
vector<vector<int> > G;
vector<int> head, par, next, tid;
HLDecomposition(int N) : N(N), G(N), head(N, -1), par(N, -1), next(N, -1), tid(N, -1) {}
void add_edge(int x, int y) {
G[x].pb(y), G[y].pb(x);
}
int dfs(int x, int p) {
par[x] = p;
int s = 1, mc = 0;
for (int t : G[x]) if (t != p) {
int w = dfs(t, x);
if (w > mc) mc = w, next[x] = t;
s += w;
}
return s;
}
void build() {
assert(dfs(0, -1) == N);
queue<int> q;
int k = 0;
q.push(0);
while (!q.empty()) {
int h = q.front(); q.pop();
for (int v=h; v!=-1; v=next[v]) tid[v] = k++, head[v] = h;
for (int v=h; v!=-1; v=next[v]) for (int t : G[v]) if (tid[t] == -1) q.push(t);
}
assert(k == N);
}
int lca(int u, int v) {
if (tid[u] < tid[v]) swap(u, v);
if (head[u] == head[v]) return v;
return lca(par[head[u]], v);
}
void for_each(int u, int v, function<void(int, int)> f) {
if (tid[u] < tid[v]) swap(u, v);
if (head[u] == head[v]) return f(tid[v], tid[u]);
f(tid[head[u]], tid[u]);
return for_each(par[head[u]], v, f);
}
};
struct BIT {
int n;
vector<int> xs;
BIT(int n) : n(n), xs(n+1, 0) {}
void add(int i, int v) {
for (int x=i+1; x<=n; x+=x&-x) xs[x] += v;
}
int sum(int i) {
int s = 0;
for (int x=i+1; x>0; x-=x&-x) s += xs[x];
return s;
}
int sum(int l, int r) {
if (l > r) return 0;
return sum(r) - sum(l-1);
}
};
int N, M;
int A[100000], B[100000], C[100000];
HLDecomposition hld(0);
BIT bit(0);
vector<int> G[100000];
vector<int> Q[100000];
int dp[100000];
void dfs(int x, int p) {
int sum = 0;
for (int t : G[x]) if (t != p) {
dfs(t, x);
sum += dp[t];
}
dp[x] = sum;
for (int id : Q[x]) {
int u = A[id], v = B[id], w = C[id]+sum;
hld.for_each(u, v, [&](int l, int r) { w += bit.sum(l, r); });
dp[x] = max(dp[x], w);
}
bit.add(hld.tid[x], sum-dp[x]);
}
int main() {
cin >> N;
hld = HLDecomposition(N);
bit = BIT(N);
rep(i, N-1) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].pb(v);
G[v].pb(u);
hld.add_edge(u, v);
}
hld.build();
cin >> M;
rep(i, M) {
cin >> A[i] >> B[i] >> C[i];
A[i]--, B[i]--;
Q[hld.lca(A[i], B[i])].pb(i);
}
dfs(0, -1);
cout << dp[0] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4976 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
5 ms |
5436 KB |
Output is correct |
5 |
Correct |
180 ms |
17964 KB |
Output is correct |
6 |
Correct |
142 ms |
56228 KB |
Output is correct |
7 |
Correct |
185 ms |
56228 KB |
Output is correct |
8 |
Correct |
137 ms |
56228 KB |
Output is correct |
9 |
Correct |
174 ms |
56228 KB |
Output is correct |
10 |
Correct |
137 ms |
56228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
56228 KB |
Output is correct |
2 |
Correct |
5 ms |
56228 KB |
Output is correct |
3 |
Correct |
6 ms |
56228 KB |
Output is correct |
4 |
Correct |
266 ms |
65712 KB |
Output is correct |
5 |
Correct |
247 ms |
68052 KB |
Output is correct |
6 |
Correct |
259 ms |
70648 KB |
Output is correct |
7 |
Correct |
251 ms |
73128 KB |
Output is correct |
8 |
Correct |
251 ms |
75568 KB |
Output is correct |
9 |
Correct |
266 ms |
78024 KB |
Output is correct |
10 |
Correct |
250 ms |
80384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
56228 KB |
Output is correct |
2 |
Correct |
5 ms |
56228 KB |
Output is correct |
3 |
Correct |
6 ms |
56228 KB |
Output is correct |
4 |
Correct |
266 ms |
65712 KB |
Output is correct |
5 |
Correct |
247 ms |
68052 KB |
Output is correct |
6 |
Correct |
259 ms |
70648 KB |
Output is correct |
7 |
Correct |
251 ms |
73128 KB |
Output is correct |
8 |
Correct |
251 ms |
75568 KB |
Output is correct |
9 |
Correct |
266 ms |
78024 KB |
Output is correct |
10 |
Correct |
250 ms |
80384 KB |
Output is correct |
11 |
Correct |
26 ms |
80384 KB |
Output is correct |
12 |
Correct |
257 ms |
83400 KB |
Output is correct |
13 |
Correct |
263 ms |
86104 KB |
Output is correct |
14 |
Correct |
260 ms |
89012 KB |
Output is correct |
15 |
Correct |
263 ms |
91712 KB |
Output is correct |
16 |
Correct |
259 ms |
94460 KB |
Output is correct |
17 |
Correct |
261 ms |
97248 KB |
Output is correct |
18 |
Correct |
271 ms |
99796 KB |
Output is correct |
19 |
Correct |
273 ms |
102656 KB |
Output is correct |
20 |
Correct |
265 ms |
105300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
105300 KB |
Output is correct |
2 |
Correct |
253 ms |
110332 KB |
Output is correct |
3 |
Correct |
316 ms |
110332 KB |
Output is correct |
4 |
Correct |
257 ms |
110332 KB |
Output is correct |
5 |
Correct |
316 ms |
110332 KB |
Output is correct |
6 |
Correct |
282 ms |
110332 KB |
Output is correct |
7 |
Correct |
303 ms |
110332 KB |
Output is correct |
8 |
Correct |
318 ms |
110332 KB |
Output is correct |
9 |
Correct |
246 ms |
127616 KB |
Output is correct |
10 |
Correct |
308 ms |
127616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4976 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
5 ms |
5436 KB |
Output is correct |
5 |
Correct |
180 ms |
17964 KB |
Output is correct |
6 |
Correct |
142 ms |
56228 KB |
Output is correct |
7 |
Correct |
185 ms |
56228 KB |
Output is correct |
8 |
Correct |
137 ms |
56228 KB |
Output is correct |
9 |
Correct |
174 ms |
56228 KB |
Output is correct |
10 |
Correct |
137 ms |
56228 KB |
Output is correct |
11 |
Correct |
9 ms |
127616 KB |
Output is correct |
12 |
Correct |
7 ms |
127616 KB |
Output is correct |
13 |
Correct |
7 ms |
127616 KB |
Output is correct |
14 |
Correct |
8 ms |
127616 KB |
Output is correct |
15 |
Correct |
7 ms |
127616 KB |
Output is correct |
16 |
Correct |
8 ms |
127616 KB |
Output is correct |
17 |
Correct |
7 ms |
127616 KB |
Output is correct |
18 |
Correct |
7 ms |
127616 KB |
Output is correct |
19 |
Correct |
7 ms |
127616 KB |
Output is correct |
20 |
Correct |
7 ms |
127616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4976 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
5 ms |
5436 KB |
Output is correct |
5 |
Correct |
180 ms |
17964 KB |
Output is correct |
6 |
Correct |
142 ms |
56228 KB |
Output is correct |
7 |
Correct |
185 ms |
56228 KB |
Output is correct |
8 |
Correct |
137 ms |
56228 KB |
Output is correct |
9 |
Correct |
174 ms |
56228 KB |
Output is correct |
10 |
Correct |
137 ms |
56228 KB |
Output is correct |
11 |
Correct |
5 ms |
56228 KB |
Output is correct |
12 |
Correct |
5 ms |
56228 KB |
Output is correct |
13 |
Correct |
6 ms |
56228 KB |
Output is correct |
14 |
Correct |
266 ms |
65712 KB |
Output is correct |
15 |
Correct |
247 ms |
68052 KB |
Output is correct |
16 |
Correct |
259 ms |
70648 KB |
Output is correct |
17 |
Correct |
251 ms |
73128 KB |
Output is correct |
18 |
Correct |
251 ms |
75568 KB |
Output is correct |
19 |
Correct |
266 ms |
78024 KB |
Output is correct |
20 |
Correct |
250 ms |
80384 KB |
Output is correct |
21 |
Correct |
26 ms |
80384 KB |
Output is correct |
22 |
Correct |
257 ms |
83400 KB |
Output is correct |
23 |
Correct |
263 ms |
86104 KB |
Output is correct |
24 |
Correct |
260 ms |
89012 KB |
Output is correct |
25 |
Correct |
263 ms |
91712 KB |
Output is correct |
26 |
Correct |
259 ms |
94460 KB |
Output is correct |
27 |
Correct |
261 ms |
97248 KB |
Output is correct |
28 |
Correct |
271 ms |
99796 KB |
Output is correct |
29 |
Correct |
273 ms |
102656 KB |
Output is correct |
30 |
Correct |
265 ms |
105300 KB |
Output is correct |
31 |
Correct |
339 ms |
105300 KB |
Output is correct |
32 |
Correct |
253 ms |
110332 KB |
Output is correct |
33 |
Correct |
316 ms |
110332 KB |
Output is correct |
34 |
Correct |
257 ms |
110332 KB |
Output is correct |
35 |
Correct |
316 ms |
110332 KB |
Output is correct |
36 |
Correct |
282 ms |
110332 KB |
Output is correct |
37 |
Correct |
303 ms |
110332 KB |
Output is correct |
38 |
Correct |
318 ms |
110332 KB |
Output is correct |
39 |
Correct |
246 ms |
127616 KB |
Output is correct |
40 |
Correct |
308 ms |
127616 KB |
Output is correct |
41 |
Correct |
9 ms |
127616 KB |
Output is correct |
42 |
Correct |
7 ms |
127616 KB |
Output is correct |
43 |
Correct |
7 ms |
127616 KB |
Output is correct |
44 |
Correct |
8 ms |
127616 KB |
Output is correct |
45 |
Correct |
7 ms |
127616 KB |
Output is correct |
46 |
Correct |
8 ms |
127616 KB |
Output is correct |
47 |
Correct |
7 ms |
127616 KB |
Output is correct |
48 |
Correct |
7 ms |
127616 KB |
Output is correct |
49 |
Correct |
7 ms |
127616 KB |
Output is correct |
50 |
Correct |
7 ms |
127616 KB |
Output is correct |
51 |
Correct |
342 ms |
127616 KB |
Output is correct |
52 |
Correct |
275 ms |
135804 KB |
Output is correct |
53 |
Correct |
320 ms |
135804 KB |
Output is correct |
54 |
Correct |
284 ms |
135804 KB |
Output is correct |
55 |
Correct |
378 ms |
135804 KB |
Output is correct |
56 |
Correct |
273 ms |
146800 KB |
Output is correct |
57 |
Correct |
347 ms |
146800 KB |
Output is correct |
58 |
Correct |
266 ms |
146800 KB |
Output is correct |
59 |
Correct |
362 ms |
146800 KB |
Output is correct |
60 |
Correct |
268 ms |
157940 KB |
Output is correct |
61 |
Correct |
339 ms |
157940 KB |
Output is correct |
62 |
Correct |
272 ms |
157940 KB |
Output is correct |
63 |
Correct |
361 ms |
157940 KB |
Output is correct |
64 |
Correct |
270 ms |
168700 KB |
Output is correct |
65 |
Correct |
324 ms |
168700 KB |
Output is correct |
66 |
Correct |
279 ms |
168700 KB |
Output is correct |
67 |
Correct |
357 ms |
168700 KB |
Output is correct |
68 |
Correct |
263 ms |
179772 KB |
Output is correct |
69 |
Correct |
306 ms |
179772 KB |
Output is correct |
70 |
Correct |
270 ms |
179772 KB |
Output is correct |