#include <bits/stdc++.h>
#define N 131072
using namespace std;
#define ll long long
#define ff first
#define ss second
struct Seg{
ll val[N << 1];
void update(int L, int R, ll v, int id = 1, int l = 1, int r = N){
if(L <= l && r <= R){
val[id] += v;
return;
}
if(r < L || l > R){
return;
}
int m = (l + r) >> 1;
update(L, R, v, id << 1, l, m);
update(L, R, v, id << 1 | 1, m + 1, r);
}
inline ll get(int v){
v += N - 1;
ll s = 0;
while(v){
s += val[v];
v >>= 1;
}
return s;
}
} child;
vector<int> adj[N];
int in[N], out[N], p[N][20];
int d[N];
int n;
int cnt;
void dfs2(int v, int par){
in[v] = ++cnt;
d[v] = d[par] + 1;
p[v][0] = par;
for(int i = 0; p[v][i] != 0; i++){
p[v][i + 1] = p[p[v][i]][i];
}
for(int& x : adj[v]){
if(x == par) continue;
dfs2(x, v);
}
out[v] = cnt;
}
int find(int v, int u){
if(d[v] < d[u]) swap(v, u);
for(int i = 0; d[v] - d[u]; i++)
if((d[v] - d[u]) & (1 << i))
v = p[v][i];
if(v == u) return v;
for(int i = 18; i >= 0; i--){
if(p[v][i] != p[u][i]){
v = p[v][i];
u = p[u][i];
}
}
return p[v][0];
}
vector<pair<pair<int, int>, int> > obj[N];
ll is1[N], is0[N];
void dfs(int v, int p){
ll res = 0, tmp;
for(int& x : adj[v]){
if(x == p) continue;
dfs(x, v);
res += is1[x];
}
is1[v] = is0[v] = res;
for(auto& x : obj[v]){
tmp = x.ss + res;
tmp += child.get(in[x.ff.ff]);
tmp += child.get(in[x.ff.ss]);
is1[v] = max(is1[v], tmp);
}
child.update(in[v], out[v], res - is1[v]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int v, u, i = 1; i < n; i++){
cin>>v>>u;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs2(1, 0);
cin>>n;
while(n--){
int a, b, c;
cin>>a>>b>>c;
obj[find(a, b)].push_back({{a, b}, c});
}
dfs(1, 0);
cout<<is1[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6520 KB |
Output is correct |
3 |
Correct |
8 ms |
6520 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
131 ms |
23160 KB |
Output is correct |
6 |
Correct |
87 ms |
30840 KB |
Output is correct |
7 |
Correct |
128 ms |
28088 KB |
Output is correct |
8 |
Correct |
106 ms |
22776 KB |
Output is correct |
9 |
Correct |
125 ms |
26592 KB |
Output is correct |
10 |
Correct |
87 ms |
22832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6520 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6776 KB |
Output is correct |
4 |
Correct |
163 ms |
34292 KB |
Output is correct |
5 |
Correct |
167 ms |
34168 KB |
Output is correct |
6 |
Correct |
150 ms |
34168 KB |
Output is correct |
7 |
Correct |
170 ms |
34116 KB |
Output is correct |
8 |
Correct |
164 ms |
34172 KB |
Output is correct |
9 |
Correct |
146 ms |
34168 KB |
Output is correct |
10 |
Correct |
160 ms |
34196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6520 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6776 KB |
Output is correct |
4 |
Correct |
163 ms |
34292 KB |
Output is correct |
5 |
Correct |
167 ms |
34168 KB |
Output is correct |
6 |
Correct |
150 ms |
34168 KB |
Output is correct |
7 |
Correct |
170 ms |
34116 KB |
Output is correct |
8 |
Correct |
164 ms |
34172 KB |
Output is correct |
9 |
Correct |
146 ms |
34168 KB |
Output is correct |
10 |
Correct |
160 ms |
34196 KB |
Output is correct |
11 |
Correct |
19 ms |
7672 KB |
Output is correct |
12 |
Correct |
173 ms |
34460 KB |
Output is correct |
13 |
Correct |
172 ms |
34424 KB |
Output is correct |
14 |
Correct |
157 ms |
34424 KB |
Output is correct |
15 |
Correct |
168 ms |
34396 KB |
Output is correct |
16 |
Correct |
156 ms |
34456 KB |
Output is correct |
17 |
Correct |
175 ms |
34468 KB |
Output is correct |
18 |
Correct |
186 ms |
34424 KB |
Output is correct |
19 |
Correct |
155 ms |
34424 KB |
Output is correct |
20 |
Correct |
179 ms |
34488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
26004 KB |
Output is correct |
2 |
Correct |
171 ms |
34164 KB |
Output is correct |
3 |
Correct |
263 ms |
31092 KB |
Output is correct |
4 |
Correct |
144 ms |
25796 KB |
Output is correct |
5 |
Correct |
202 ms |
30516 KB |
Output is correct |
6 |
Correct |
144 ms |
25708 KB |
Output is correct |
7 |
Correct |
255 ms |
30520 KB |
Output is correct |
8 |
Correct |
189 ms |
26236 KB |
Output is correct |
9 |
Correct |
148 ms |
34188 KB |
Output is correct |
10 |
Correct |
246 ms |
29356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6520 KB |
Output is correct |
3 |
Correct |
8 ms |
6520 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
131 ms |
23160 KB |
Output is correct |
6 |
Correct |
87 ms |
30840 KB |
Output is correct |
7 |
Correct |
128 ms |
28088 KB |
Output is correct |
8 |
Correct |
106 ms |
22776 KB |
Output is correct |
9 |
Correct |
125 ms |
26592 KB |
Output is correct |
10 |
Correct |
87 ms |
22832 KB |
Output is correct |
11 |
Correct |
8 ms |
6776 KB |
Output is correct |
12 |
Correct |
9 ms |
6908 KB |
Output is correct |
13 |
Correct |
9 ms |
6776 KB |
Output is correct |
14 |
Correct |
8 ms |
6776 KB |
Output is correct |
15 |
Correct |
9 ms |
6776 KB |
Output is correct |
16 |
Correct |
9 ms |
6776 KB |
Output is correct |
17 |
Correct |
8 ms |
6776 KB |
Output is correct |
18 |
Correct |
9 ms |
6776 KB |
Output is correct |
19 |
Correct |
8 ms |
6780 KB |
Output is correct |
20 |
Correct |
8 ms |
6776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6520 KB |
Output is correct |
3 |
Correct |
8 ms |
6520 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
131 ms |
23160 KB |
Output is correct |
6 |
Correct |
87 ms |
30840 KB |
Output is correct |
7 |
Correct |
128 ms |
28088 KB |
Output is correct |
8 |
Correct |
106 ms |
22776 KB |
Output is correct |
9 |
Correct |
125 ms |
26592 KB |
Output is correct |
10 |
Correct |
87 ms |
22832 KB |
Output is correct |
11 |
Correct |
7 ms |
6520 KB |
Output is correct |
12 |
Correct |
8 ms |
6648 KB |
Output is correct |
13 |
Correct |
8 ms |
6776 KB |
Output is correct |
14 |
Correct |
163 ms |
34292 KB |
Output is correct |
15 |
Correct |
167 ms |
34168 KB |
Output is correct |
16 |
Correct |
150 ms |
34168 KB |
Output is correct |
17 |
Correct |
170 ms |
34116 KB |
Output is correct |
18 |
Correct |
164 ms |
34172 KB |
Output is correct |
19 |
Correct |
146 ms |
34168 KB |
Output is correct |
20 |
Correct |
160 ms |
34196 KB |
Output is correct |
21 |
Correct |
19 ms |
7672 KB |
Output is correct |
22 |
Correct |
173 ms |
34460 KB |
Output is correct |
23 |
Correct |
172 ms |
34424 KB |
Output is correct |
24 |
Correct |
157 ms |
34424 KB |
Output is correct |
25 |
Correct |
168 ms |
34396 KB |
Output is correct |
26 |
Correct |
156 ms |
34456 KB |
Output is correct |
27 |
Correct |
175 ms |
34468 KB |
Output is correct |
28 |
Correct |
186 ms |
34424 KB |
Output is correct |
29 |
Correct |
155 ms |
34424 KB |
Output is correct |
30 |
Correct |
179 ms |
34488 KB |
Output is correct |
31 |
Correct |
259 ms |
26004 KB |
Output is correct |
32 |
Correct |
171 ms |
34164 KB |
Output is correct |
33 |
Correct |
263 ms |
31092 KB |
Output is correct |
34 |
Correct |
144 ms |
25796 KB |
Output is correct |
35 |
Correct |
202 ms |
30516 KB |
Output is correct |
36 |
Correct |
144 ms |
25708 KB |
Output is correct |
37 |
Correct |
255 ms |
30520 KB |
Output is correct |
38 |
Correct |
189 ms |
26236 KB |
Output is correct |
39 |
Correct |
148 ms |
34188 KB |
Output is correct |
40 |
Correct |
246 ms |
29356 KB |
Output is correct |
41 |
Correct |
8 ms |
6776 KB |
Output is correct |
42 |
Correct |
9 ms |
6908 KB |
Output is correct |
43 |
Correct |
9 ms |
6776 KB |
Output is correct |
44 |
Correct |
8 ms |
6776 KB |
Output is correct |
45 |
Correct |
9 ms |
6776 KB |
Output is correct |
46 |
Correct |
9 ms |
6776 KB |
Output is correct |
47 |
Correct |
8 ms |
6776 KB |
Output is correct |
48 |
Correct |
9 ms |
6776 KB |
Output is correct |
49 |
Correct |
8 ms |
6780 KB |
Output is correct |
50 |
Correct |
8 ms |
6776 KB |
Output is correct |
51 |
Correct |
190 ms |
26604 KB |
Output is correct |
52 |
Correct |
166 ms |
34444 KB |
Output is correct |
53 |
Correct |
255 ms |
29716 KB |
Output is correct |
54 |
Correct |
149 ms |
26036 KB |
Output is correct |
55 |
Correct |
190 ms |
26332 KB |
Output is correct |
56 |
Correct |
166 ms |
34424 KB |
Output is correct |
57 |
Correct |
237 ms |
30328 KB |
Output is correct |
58 |
Correct |
148 ms |
25904 KB |
Output is correct |
59 |
Correct |
184 ms |
26468 KB |
Output is correct |
60 |
Correct |
171 ms |
34424 KB |
Output is correct |
61 |
Correct |
206 ms |
30428 KB |
Output is correct |
62 |
Correct |
144 ms |
25780 KB |
Output is correct |
63 |
Correct |
187 ms |
26080 KB |
Output is correct |
64 |
Correct |
167 ms |
34424 KB |
Output is correct |
65 |
Correct |
244 ms |
30256 KB |
Output is correct |
66 |
Correct |
138 ms |
26124 KB |
Output is correct |
67 |
Correct |
176 ms |
26096 KB |
Output is correct |
68 |
Correct |
163 ms |
34428 KB |
Output is correct |
69 |
Correct |
212 ms |
29304 KB |
Output is correct |
70 |
Correct |
152 ms |
26080 KB |
Output is correct |