#include <bits/stdc++.h>
using namespace std;
#define ll long long
#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];
void modify(int p,ll v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
ll getval(int s,int e){
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);
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){
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 |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7652 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
35 ms |
13136 KB |
Output is correct |
6 |
Correct |
45 ms |
37768 KB |
Output is correct |
7 |
Correct |
58 ms |
29248 KB |
Output is correct |
8 |
Correct |
28 ms |
12636 KB |
Output is correct |
9 |
Correct |
52 ms |
21300 KB |
Output is correct |
10 |
Correct |
27 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
75 ms |
39868 KB |
Output is correct |
5 |
Correct |
69 ms |
40020 KB |
Output is correct |
6 |
Correct |
83 ms |
39920 KB |
Output is correct |
7 |
Correct |
75 ms |
40016 KB |
Output is correct |
8 |
Correct |
69 ms |
39864 KB |
Output is correct |
9 |
Correct |
79 ms |
39964 KB |
Output is correct |
10 |
Correct |
78 ms |
39760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
75 ms |
39868 KB |
Output is correct |
5 |
Correct |
69 ms |
40020 KB |
Output is correct |
6 |
Correct |
83 ms |
39920 KB |
Output is correct |
7 |
Correct |
75 ms |
40016 KB |
Output is correct |
8 |
Correct |
69 ms |
39864 KB |
Output is correct |
9 |
Correct |
79 ms |
39964 KB |
Output is correct |
10 |
Correct |
78 ms |
39760 KB |
Output is correct |
11 |
Correct |
9 ms |
8284 KB |
Output is correct |
12 |
Correct |
77 ms |
39888 KB |
Output is correct |
13 |
Correct |
80 ms |
39904 KB |
Output is correct |
14 |
Correct |
73 ms |
40016 KB |
Output is correct |
15 |
Correct |
78 ms |
40132 KB |
Output is correct |
16 |
Correct |
80 ms |
39944 KB |
Output is correct |
17 |
Correct |
79 ms |
39972 KB |
Output is correct |
18 |
Correct |
73 ms |
39924 KB |
Output is correct |
19 |
Correct |
81 ms |
40004 KB |
Output is correct |
20 |
Correct |
81 ms |
40344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
14604 KB |
Output is correct |
2 |
Correct |
83 ms |
40028 KB |
Output is correct |
3 |
Execution timed out |
1070 ms |
22504 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7652 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
35 ms |
13136 KB |
Output is correct |
6 |
Correct |
45 ms |
37768 KB |
Output is correct |
7 |
Correct |
58 ms |
29248 KB |
Output is correct |
8 |
Correct |
28 ms |
12636 KB |
Output is correct |
9 |
Correct |
52 ms |
21300 KB |
Output is correct |
10 |
Correct |
27 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7772 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7600 KB |
Output is correct |
16 |
Correct |
2 ms |
7772 KB |
Output is correct |
17 |
Correct |
2 ms |
7756 KB |
Output is correct |
18 |
Correct |
3 ms |
7772 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
7768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7652 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
35 ms |
13136 KB |
Output is correct |
6 |
Correct |
45 ms |
37768 KB |
Output is correct |
7 |
Correct |
58 ms |
29248 KB |
Output is correct |
8 |
Correct |
28 ms |
12636 KB |
Output is correct |
9 |
Correct |
52 ms |
21300 KB |
Output is correct |
10 |
Correct |
27 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
75 ms |
39868 KB |
Output is correct |
15 |
Correct |
69 ms |
40020 KB |
Output is correct |
16 |
Correct |
83 ms |
39920 KB |
Output is correct |
17 |
Correct |
75 ms |
40016 KB |
Output is correct |
18 |
Correct |
69 ms |
39864 KB |
Output is correct |
19 |
Correct |
79 ms |
39964 KB |
Output is correct |
20 |
Correct |
78 ms |
39760 KB |
Output is correct |
21 |
Correct |
9 ms |
8284 KB |
Output is correct |
22 |
Correct |
77 ms |
39888 KB |
Output is correct |
23 |
Correct |
80 ms |
39904 KB |
Output is correct |
24 |
Correct |
73 ms |
40016 KB |
Output is correct |
25 |
Correct |
78 ms |
40132 KB |
Output is correct |
26 |
Correct |
80 ms |
39944 KB |
Output is correct |
27 |
Correct |
79 ms |
39972 KB |
Output is correct |
28 |
Correct |
73 ms |
39924 KB |
Output is correct |
29 |
Correct |
81 ms |
40004 KB |
Output is correct |
30 |
Correct |
81 ms |
40344 KB |
Output is correct |
31 |
Correct |
207 ms |
14604 KB |
Output is correct |
32 |
Correct |
83 ms |
40028 KB |
Output is correct |
33 |
Execution timed out |
1070 ms |
22504 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |