#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _ <<" "<<
const ll MAXN = 1e5+5;
const ll INF = 1e18;
vector<ll> adjlist[MAXN];
bool visited[MAXN];
//lca
ll parent[18][MAXN], depth[MAXN];
ll n;
//hld
ll stsize[MAXN], preorder[MAXN], head[MAXN], id;
void hld_sz(ll u, ll par){
stsize[u] = 1;
for (auto &v : adjlist[u]){
if (v != par) {
hld_sz(v, u);
stsize[u] += stsize[v];
if (adjlist[u][0] == par || stsize[v] > stsize[adjlist[u][0]]) swap(v, adjlist[u][0]);
}
}
return;
}
void hld(ll u, ll par){
preorder[u] = ++id;
if (u == par) head[u] = u;
for (auto v : adjlist[u]){
if (v != par){
head[v] = (v == adjlist[u][0]) ? head[u] : v;
hld(v, u);
}
}
return;
}
void dfs(ll u, ll par, ll dep){
visited[u] = 1;
depth[u] = dep;
parent[0][u] = par;
for (auto v : adjlist[u]){
if (!visited[v]) dfs(v, u, dep+1);
}
return;
}
void lcainit(){
for (int power=1; power<18; power++) for (int i=1; i<=n; i++){
parent[power][i] = parent[power-1][parent[power-1][i]];
}
return;
}
ll lca(ll a, ll b){
if (depth[a] > depth[b]) swap(a,b);
ll diff = depth[b] - depth[a];
ll power = 0;
while (diff){
if (diff%2) b = parent[power][b];
diff>>=1;
power++;
}
if (a == b) return a;
for (int i=17; i>=0; i--){
if (parent[i][a] != parent[i][b]){
a = parent[i][a];
b = parent[i][b];
}
}
return parent[0][a];
}
ll nodesum[MAXN], childsum[MAXN];
void upd(ll x, ll val, ll type){
if (type) {
for (int i=x; i<=n; i+=i&(-i)) nodesum[i] += val;
} else {
for (int i=x; i<=n; i+=i&(-i)) childsum[i] += val;
}
return;
}
ll query(ll a, ll b, ll type){
ll resa=0, resb=0;
if (type) {
for (int i=--a; i; i-=i&(-i)) resa += nodesum[i];
for (int i=b; i; i-=i&(-i)) resb += nodesum[i];
} else {
for (int i=--a; i; i-=i&(-i)) resa += childsum[i];
for (int i=b; i; i-=i&(-i)) resb += childsum[i];
}
return resb-resa;
}
ll dp[MAXN];
bool visitd[MAXN];
vector<tuple<ll,ll,ll> > lcas[MAXN];
void solve(ll u){
visitd[u] = 1;
for (auto v : adjlist[u]){
if (visitd[v]) continue;
solve(v);
dp[u] += dp[v];
}
for (auto paths : lcas[u]){
ll x, y, val;
ll nsum = 0, csum = 0;
tie(x, y, val) = paths;
// cout << x _ y _ val << "gay ";
while (true){
if (head[x] == head[y]){
if (preorder[x] > preorder[y]) swap(x, y);
nsum += query(preorder[x], preorder[y], 1);
csum += query(preorder[x], preorder[y], 0);
break;
} else {
if (depth[head[x]] >= depth[head[y]]) {
nsum += query(preorder[head[x]], preorder[x], 1);
csum += query(preorder[head[x]], preorder[x], 0);
x = parent[0][head[x]];
} else {
nsum += query(preorder[head[y]], preorder[y], 1);
csum += query(preorder[head[y]], preorder[y], 0);
y = parent[0][head[y]];
}
}
}
// cout << csum _ nsum _ val << "vals ";
ll newval = csum - nsum + val;
dp[u] = max(dp[u], newval);
}
upd(preorder[u], dp[u], 1);
upd(preorder[parent[0][u]], dp[u], 0);
// cout << u _ dp[u] << '\n';
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i=1; i<n; i++){
ll a, b;
cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
dfs(1,1,0);
lcainit();
hld_sz(1,1);
hld(1,1);
ll q;
cin >> q;
while (q--) {
ll a, b, c;
cin >> a >> b >> c;
lcas[lca(a,b)].emplace_back(a,b,c);
}
solve(1);
cout << dp[1];
// for (int i=1; i<11; i++) cout << stsize[i] _ preorder[i] _ head[i] << '\n';
// cout << lca(2, 7) _ lca(3, 5) _ lca(1, 1) _ lca(6, 7);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
128 ms |
28780 KB |
Output is correct |
6 |
Correct |
63 ms |
38892 KB |
Output is correct |
7 |
Correct |
101 ms |
35436 KB |
Output is correct |
8 |
Correct |
78 ms |
27372 KB |
Output is correct |
9 |
Correct |
94 ms |
33388 KB |
Output is correct |
10 |
Correct |
83 ms |
27528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
178 ms |
42476 KB |
Output is correct |
5 |
Correct |
144 ms |
42476 KB |
Output is correct |
6 |
Correct |
135 ms |
42476 KB |
Output is correct |
7 |
Correct |
164 ms |
42476 KB |
Output is correct |
8 |
Correct |
157 ms |
42476 KB |
Output is correct |
9 |
Correct |
126 ms |
42476 KB |
Output is correct |
10 |
Correct |
161 ms |
42476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
178 ms |
42476 KB |
Output is correct |
5 |
Correct |
144 ms |
42476 KB |
Output is correct |
6 |
Correct |
135 ms |
42476 KB |
Output is correct |
7 |
Correct |
164 ms |
42476 KB |
Output is correct |
8 |
Correct |
157 ms |
42476 KB |
Output is correct |
9 |
Correct |
126 ms |
42476 KB |
Output is correct |
10 |
Correct |
161 ms |
42476 KB |
Output is correct |
11 |
Correct |
13 ms |
6508 KB |
Output is correct |
12 |
Correct |
162 ms |
42368 KB |
Output is correct |
13 |
Correct |
155 ms |
42604 KB |
Output is correct |
14 |
Correct |
135 ms |
42472 KB |
Output is correct |
15 |
Correct |
160 ms |
42476 KB |
Output is correct |
16 |
Correct |
129 ms |
42604 KB |
Output is correct |
17 |
Correct |
166 ms |
42604 KB |
Output is correct |
18 |
Correct |
149 ms |
42452 KB |
Output is correct |
19 |
Correct |
136 ms |
42476 KB |
Output is correct |
20 |
Correct |
151 ms |
42476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
422 ms |
31712 KB |
Output is correct |
2 |
Correct |
129 ms |
42476 KB |
Output is correct |
3 |
Correct |
332 ms |
38548 KB |
Output is correct |
4 |
Correct |
214 ms |
30624 KB |
Output is correct |
5 |
Correct |
236 ms |
37672 KB |
Output is correct |
6 |
Correct |
255 ms |
30428 KB |
Output is correct |
7 |
Correct |
344 ms |
37412 KB |
Output is correct |
8 |
Correct |
307 ms |
31904 KB |
Output is correct |
9 |
Correct |
125 ms |
42476 KB |
Output is correct |
10 |
Correct |
324 ms |
36000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
128 ms |
28780 KB |
Output is correct |
6 |
Correct |
63 ms |
38892 KB |
Output is correct |
7 |
Correct |
101 ms |
35436 KB |
Output is correct |
8 |
Correct |
78 ms |
27372 KB |
Output is correct |
9 |
Correct |
94 ms |
33388 KB |
Output is correct |
10 |
Correct |
83 ms |
27528 KB |
Output is correct |
11 |
Correct |
5 ms |
5484 KB |
Output is correct |
12 |
Correct |
4 ms |
5484 KB |
Output is correct |
13 |
Correct |
4 ms |
5484 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
4 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
4 ms |
5484 KB |
Output is correct |
19 |
Correct |
4 ms |
5484 KB |
Output is correct |
20 |
Correct |
5 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
128 ms |
28780 KB |
Output is correct |
6 |
Correct |
63 ms |
38892 KB |
Output is correct |
7 |
Correct |
101 ms |
35436 KB |
Output is correct |
8 |
Correct |
78 ms |
27372 KB |
Output is correct |
9 |
Correct |
94 ms |
33388 KB |
Output is correct |
10 |
Correct |
83 ms |
27528 KB |
Output is correct |
11 |
Correct |
4 ms |
5228 KB |
Output is correct |
12 |
Correct |
3 ms |
5228 KB |
Output is correct |
13 |
Correct |
4 ms |
5484 KB |
Output is correct |
14 |
Correct |
178 ms |
42476 KB |
Output is correct |
15 |
Correct |
144 ms |
42476 KB |
Output is correct |
16 |
Correct |
135 ms |
42476 KB |
Output is correct |
17 |
Correct |
164 ms |
42476 KB |
Output is correct |
18 |
Correct |
157 ms |
42476 KB |
Output is correct |
19 |
Correct |
126 ms |
42476 KB |
Output is correct |
20 |
Correct |
161 ms |
42476 KB |
Output is correct |
21 |
Correct |
13 ms |
6508 KB |
Output is correct |
22 |
Correct |
162 ms |
42368 KB |
Output is correct |
23 |
Correct |
155 ms |
42604 KB |
Output is correct |
24 |
Correct |
135 ms |
42472 KB |
Output is correct |
25 |
Correct |
160 ms |
42476 KB |
Output is correct |
26 |
Correct |
129 ms |
42604 KB |
Output is correct |
27 |
Correct |
166 ms |
42604 KB |
Output is correct |
28 |
Correct |
149 ms |
42452 KB |
Output is correct |
29 |
Correct |
136 ms |
42476 KB |
Output is correct |
30 |
Correct |
151 ms |
42476 KB |
Output is correct |
31 |
Correct |
422 ms |
31712 KB |
Output is correct |
32 |
Correct |
129 ms |
42476 KB |
Output is correct |
33 |
Correct |
332 ms |
38548 KB |
Output is correct |
34 |
Correct |
214 ms |
30624 KB |
Output is correct |
35 |
Correct |
236 ms |
37672 KB |
Output is correct |
36 |
Correct |
255 ms |
30428 KB |
Output is correct |
37 |
Correct |
344 ms |
37412 KB |
Output is correct |
38 |
Correct |
307 ms |
31904 KB |
Output is correct |
39 |
Correct |
125 ms |
42476 KB |
Output is correct |
40 |
Correct |
324 ms |
36000 KB |
Output is correct |
41 |
Correct |
5 ms |
5484 KB |
Output is correct |
42 |
Correct |
4 ms |
5484 KB |
Output is correct |
43 |
Correct |
4 ms |
5484 KB |
Output is correct |
44 |
Correct |
5 ms |
5484 KB |
Output is correct |
45 |
Correct |
5 ms |
5484 KB |
Output is correct |
46 |
Correct |
4 ms |
5484 KB |
Output is correct |
47 |
Correct |
5 ms |
5484 KB |
Output is correct |
48 |
Correct |
4 ms |
5484 KB |
Output is correct |
49 |
Correct |
4 ms |
5484 KB |
Output is correct |
50 |
Correct |
5 ms |
5484 KB |
Output is correct |
51 |
Correct |
286 ms |
31764 KB |
Output is correct |
52 |
Correct |
149 ms |
42476 KB |
Output is correct |
53 |
Correct |
336 ms |
36396 KB |
Output is correct |
54 |
Correct |
184 ms |
30372 KB |
Output is correct |
55 |
Correct |
415 ms |
31796 KB |
Output is correct |
56 |
Correct |
158 ms |
42476 KB |
Output is correct |
57 |
Correct |
234 ms |
37032 KB |
Output is correct |
58 |
Correct |
239 ms |
30496 KB |
Output is correct |
59 |
Correct |
284 ms |
31716 KB |
Output is correct |
60 |
Correct |
152 ms |
42604 KB |
Output is correct |
61 |
Correct |
234 ms |
37288 KB |
Output is correct |
62 |
Correct |
218 ms |
30016 KB |
Output is correct |
63 |
Correct |
423 ms |
31388 KB |
Output is correct |
64 |
Correct |
149 ms |
42476 KB |
Output is correct |
65 |
Correct |
332 ms |
37296 KB |
Output is correct |
66 |
Correct |
177 ms |
30236 KB |
Output is correct |
67 |
Correct |
405 ms |
31492 KB |
Output is correct |
68 |
Correct |
160 ms |
42476 KB |
Output is correct |
69 |
Correct |
236 ms |
35368 KB |
Output is correct |
70 |
Correct |
248 ms |
30356 KB |
Output is correct |