#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("popcnt,sse4,avx2")
const int mxn = 1e5+10;
int N,Q;
vector<int> tree[mxn];
vector<pair<pii,int>> req[mxn];
namespace HLD{
int idx[mxn],link_top[mxn],par[mxn],dep[mxn],bs[mxn],sz[mxn];
ll bit[mxn];
bitset<mxn> done;
void modify(int p,ll v){
done[p] = true;
bit[p] = bit[p+1]+v;
return;
/*
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
*/
}
ll getval(int s,int e){
if(!done[s])return bit[s+1]-bit[e+1];
return bit[s]-bit[e+1];
/*
int re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
*/
}
int cnt = 0;
void dfs1(int now){
sz[now] = 1;
for(auto nxt:tree[now]){
if(nxt == par[now])continue;
par[nxt] = now;
dep[nxt] = dep[now]+1;
dfs1(nxt);
sz[now] += sz[nxt];
if(!bs[now]||sz[bs[now]]<sz[nxt])bs[now] = nxt;
}
return;
}
void dfs2(int now,int top){
idx[now] = ++cnt;
link_top[now] = top;
if(bs[now])dfs2(bs[now],top);
for(auto nxt:tree[now]){
if(nxt == par[now]||nxt == bs[now])continue;
dfs2(nxt,nxt);
}
return;
}
pll LCA(int a,int b){
pll re = pll(0,0);
int ta = link_top[a],tb = link_top[b];
while(ta != tb){
if(dep[ta]<dep[tb]){
swap(a,b);
swap(ta,tb);
}
re.sc += getval(idx[ta],idx[a]);
a = par[ta];ta = link_top[a];
}
if(idx[a]>idx[b])swap(a,b);
re.sc += getval(idx[a],idx[b]);
re.fs = a;
return re;
}
}
namespace CALC{
ll ans = 0;
ll sum[mxn],dp[mxn];
void dfs(int now){
sort(tree[now].begin(),tree[now].end(),[](int a,int b){return HLD::idx[a]>HLD::idx[b];});
for(auto nxt:tree[now]){
if(nxt == HLD::par[now])continue;
dfs(nxt);
sum[now] += dp[nxt];
}
dp[now] = max(dp[now],sum[now]);
for(auto &i:req[now]){
auto [a,b] = i.fs;
int w = i.sc;
ll ts = sum[now]+HLD::LCA(a,b).sc+w;
dp[now] = max(dp[now],ts);
}
HLD::modify(HLD::idx[now],sum[now]-dp[now]);
}
void GO(){
dfs(1);
cout<<dp[1]<<'\n';
return;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<N;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
HLD::dfs1(1);
HLD::dfs2(1,1);
cin>>Q;
while(Q--){
int a,b,c;
cin>>a>>b>>c;
int h = HLD::LCA(a,b).fs;
req[h].push_back(make_pair(pii(a,b),c));
}
CALC::GO();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
35 ms |
11796 KB |
Output is correct |
6 |
Correct |
47 ms |
36604 KB |
Output is correct |
7 |
Correct |
51 ms |
28188 KB |
Output is correct |
8 |
Correct |
43 ms |
12492 KB |
Output is correct |
9 |
Correct |
43 ms |
23124 KB |
Output is correct |
10 |
Correct |
33 ms |
12220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
66 ms |
38616 KB |
Output is correct |
5 |
Correct |
67 ms |
38828 KB |
Output is correct |
6 |
Correct |
81 ms |
38868 KB |
Output is correct |
7 |
Correct |
77 ms |
38680 KB |
Output is correct |
8 |
Correct |
68 ms |
38856 KB |
Output is correct |
9 |
Correct |
66 ms |
38772 KB |
Output is correct |
10 |
Correct |
84 ms |
38976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
66 ms |
38616 KB |
Output is correct |
5 |
Correct |
67 ms |
38828 KB |
Output is correct |
6 |
Correct |
81 ms |
38868 KB |
Output is correct |
7 |
Correct |
77 ms |
38680 KB |
Output is correct |
8 |
Correct |
68 ms |
38856 KB |
Output is correct |
9 |
Correct |
66 ms |
38772 KB |
Output is correct |
10 |
Correct |
84 ms |
38976 KB |
Output is correct |
11 |
Correct |
7 ms |
9304 KB |
Output is correct |
12 |
Correct |
74 ms |
38716 KB |
Output is correct |
13 |
Correct |
70 ms |
38620 KB |
Output is correct |
14 |
Correct |
72 ms |
38864 KB |
Output is correct |
15 |
Correct |
73 ms |
38740 KB |
Output is correct |
16 |
Correct |
87 ms |
38736 KB |
Output is correct |
17 |
Correct |
71 ms |
38740 KB |
Output is correct |
18 |
Correct |
77 ms |
38816 KB |
Output is correct |
19 |
Correct |
66 ms |
38740 KB |
Output is correct |
20 |
Correct |
66 ms |
38740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
13292 KB |
Output is correct |
2 |
Correct |
72 ms |
38616 KB |
Output is correct |
3 |
Correct |
79 ms |
29264 KB |
Output is correct |
4 |
Correct |
59 ms |
13792 KB |
Output is correct |
5 |
Correct |
80 ms |
27380 KB |
Output is correct |
6 |
Correct |
58 ms |
13776 KB |
Output is correct |
7 |
Correct |
76 ms |
27020 KB |
Output is correct |
8 |
Correct |
69 ms |
13648 KB |
Output is correct |
9 |
Correct |
64 ms |
38740 KB |
Output is correct |
10 |
Correct |
69 ms |
24140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
35 ms |
11796 KB |
Output is correct |
6 |
Correct |
47 ms |
36604 KB |
Output is correct |
7 |
Correct |
51 ms |
28188 KB |
Output is correct |
8 |
Correct |
43 ms |
12492 KB |
Output is correct |
9 |
Correct |
43 ms |
23124 KB |
Output is correct |
10 |
Correct |
33 ms |
12220 KB |
Output is correct |
11 |
Correct |
3 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
3 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8736 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
35 ms |
11796 KB |
Output is correct |
6 |
Correct |
47 ms |
36604 KB |
Output is correct |
7 |
Correct |
51 ms |
28188 KB |
Output is correct |
8 |
Correct |
43 ms |
12492 KB |
Output is correct |
9 |
Correct |
43 ms |
23124 KB |
Output is correct |
10 |
Correct |
33 ms |
12220 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
66 ms |
38616 KB |
Output is correct |
15 |
Correct |
67 ms |
38828 KB |
Output is correct |
16 |
Correct |
81 ms |
38868 KB |
Output is correct |
17 |
Correct |
77 ms |
38680 KB |
Output is correct |
18 |
Correct |
68 ms |
38856 KB |
Output is correct |
19 |
Correct |
66 ms |
38772 KB |
Output is correct |
20 |
Correct |
84 ms |
38976 KB |
Output is correct |
21 |
Correct |
7 ms |
9304 KB |
Output is correct |
22 |
Correct |
74 ms |
38716 KB |
Output is correct |
23 |
Correct |
70 ms |
38620 KB |
Output is correct |
24 |
Correct |
72 ms |
38864 KB |
Output is correct |
25 |
Correct |
73 ms |
38740 KB |
Output is correct |
26 |
Correct |
87 ms |
38736 KB |
Output is correct |
27 |
Correct |
71 ms |
38740 KB |
Output is correct |
28 |
Correct |
77 ms |
38816 KB |
Output is correct |
29 |
Correct |
66 ms |
38740 KB |
Output is correct |
30 |
Correct |
66 ms |
38740 KB |
Output is correct |
31 |
Correct |
91 ms |
13292 KB |
Output is correct |
32 |
Correct |
72 ms |
38616 KB |
Output is correct |
33 |
Correct |
79 ms |
29264 KB |
Output is correct |
34 |
Correct |
59 ms |
13792 KB |
Output is correct |
35 |
Correct |
80 ms |
27380 KB |
Output is correct |
36 |
Correct |
58 ms |
13776 KB |
Output is correct |
37 |
Correct |
76 ms |
27020 KB |
Output is correct |
38 |
Correct |
69 ms |
13648 KB |
Output is correct |
39 |
Correct |
64 ms |
38740 KB |
Output is correct |
40 |
Correct |
69 ms |
24140 KB |
Output is correct |
41 |
Correct |
3 ms |
8536 KB |
Output is correct |
42 |
Correct |
2 ms |
8796 KB |
Output is correct |
43 |
Correct |
3 ms |
8540 KB |
Output is correct |
44 |
Correct |
3 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
3 ms |
8540 KB |
Output is correct |
47 |
Correct |
3 ms |
8540 KB |
Output is correct |
48 |
Correct |
3 ms |
8540 KB |
Output is correct |
49 |
Correct |
4 ms |
8736 KB |
Output is correct |
50 |
Correct |
3 ms |
8796 KB |
Output is correct |
51 |
Correct |
71 ms |
13512 KB |
Output is correct |
52 |
Correct |
71 ms |
38680 KB |
Output is correct |
53 |
Correct |
75 ms |
24588 KB |
Output is correct |
54 |
Correct |
56 ms |
13784 KB |
Output is correct |
55 |
Correct |
91 ms |
13324 KB |
Output is correct |
56 |
Correct |
79 ms |
38716 KB |
Output is correct |
57 |
Correct |
83 ms |
25996 KB |
Output is correct |
58 |
Correct |
61 ms |
13584 KB |
Output is correct |
59 |
Correct |
87 ms |
13696 KB |
Output is correct |
60 |
Correct |
68 ms |
38728 KB |
Output is correct |
61 |
Correct |
98 ms |
26308 KB |
Output is correct |
62 |
Correct |
68 ms |
13704 KB |
Output is correct |
63 |
Correct |
81 ms |
13240 KB |
Output is correct |
64 |
Correct |
67 ms |
38664 KB |
Output is correct |
65 |
Correct |
78 ms |
26440 KB |
Output is correct |
66 |
Correct |
72 ms |
13708 KB |
Output is correct |
67 |
Correct |
84 ms |
13564 KB |
Output is correct |
68 |
Correct |
66 ms |
38736 KB |
Output is correct |
69 |
Correct |
74 ms |
22352 KB |
Output is correct |
70 |
Correct |
62 ms |
13876 KB |
Output is correct |