#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=1e5+3;
const int logn=19;
int n,m,lv[maxn],par[maxn],f[maxn][logn];
int timeIn[maxn],timeOut[maxn];
long long dp[maxn],bit[maxn];
vector<int> adj[maxn],road[maxn];
struct Query{int a,b,c;}q[maxn];
inline void Dfs(const int &u, const int &p){
static int nVisits=0;
f[u][0]=p; par[u]=p;
timeIn[u]=++nVisits;
for(int i=1;i<logn;i++){
int v=f[u][i-1];
if(v==-1) f[u][i]=-1;
else f[u][i]=f[v][i-1];
}
for(int v: adj[u]) if(v!=p){
lv[v]=lv[u]+1;
Dfs(v,u);
}
timeOut[u]=nVisits;
}
inline int Jump(int u, int k){
int cur=0;
for(;k;k>>=1){
if(k&1) u=f[u][cur];
cur++;
}
return u;
}
inline int Lca(int u, int v){
if(lv[v]<lv[u]) swap(u,v);
v=Jump(v,lv[v]-lv[u]);
if(u==v) return u;
for(int i=logn-1;i>=0;i--) if(f[u][i]!=f[v][i]){
u=f[u][i]; v=f[v][i];
}
return par[u];
}
inline void Update(int x, const int &val){
for(;x<=n;x+=x&-x) bit[x]+=val;
}
inline int Get(int x){
long long res=0;
for(;x;x-=x&-x) res+=bit[x];
return res;
}
inline void Solve(const int &u, const int &p){
long long sumChild=0;
for(int v: adj[u]) if(v!=p){
Solve(v,u);
sumChild+=dp[v];
}
dp[u]=sumChild;
for(int i: road[u]){
dp[u]=max(dp[u],sumChild-Get(timeIn[q[i].a])-Get(timeIn[q[i].b])+q[i].c);
}
Update(timeIn[u],dp[u]-sumChild);
Update(timeOut[u]+1,sumChild-dp[u]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin>>n;
for(int i=1,u,v;i<n;i++){
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
Dfs(1,-1);
cin>>m;
for(int i=1;i<=m;i++){
cin>>q[i].a>>q[i].b>>q[i].c;
road[Lca(q[i].a,q[i].b)].push_back(i);
}
Solve(1,-1);
cout<<dp[1];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5240 KB |
Output is correct |
5 |
Correct |
116 ms |
20000 KB |
Output is correct |
6 |
Correct |
65 ms |
24828 KB |
Output is correct |
7 |
Correct |
119 ms |
23172 KB |
Output is correct |
8 |
Correct |
92 ms |
20344 KB |
Output is correct |
9 |
Correct |
113 ms |
22208 KB |
Output is correct |
10 |
Correct |
82 ms |
20472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
157 ms |
28524 KB |
Output is correct |
5 |
Correct |
145 ms |
28536 KB |
Output is correct |
6 |
Correct |
129 ms |
28536 KB |
Output is correct |
7 |
Correct |
151 ms |
28468 KB |
Output is correct |
8 |
Correct |
149 ms |
28588 KB |
Output is correct |
9 |
Correct |
137 ms |
28644 KB |
Output is correct |
10 |
Correct |
143 ms |
28392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
157 ms |
28524 KB |
Output is correct |
5 |
Correct |
145 ms |
28536 KB |
Output is correct |
6 |
Correct |
129 ms |
28536 KB |
Output is correct |
7 |
Correct |
151 ms |
28468 KB |
Output is correct |
8 |
Correct |
149 ms |
28588 KB |
Output is correct |
9 |
Correct |
137 ms |
28644 KB |
Output is correct |
10 |
Correct |
143 ms |
28392 KB |
Output is correct |
11 |
Correct |
17 ms |
6008 KB |
Output is correct |
12 |
Correct |
146 ms |
28832 KB |
Output is correct |
13 |
Correct |
151 ms |
28792 KB |
Output is correct |
14 |
Correct |
137 ms |
28792 KB |
Output is correct |
15 |
Correct |
152 ms |
28792 KB |
Output is correct |
16 |
Correct |
134 ms |
28792 KB |
Output is correct |
17 |
Correct |
154 ms |
28664 KB |
Output is correct |
18 |
Correct |
151 ms |
28720 KB |
Output is correct |
19 |
Correct |
139 ms |
28792 KB |
Output is correct |
20 |
Correct |
153 ms |
28792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
23076 KB |
Output is correct |
2 |
Correct |
134 ms |
28508 KB |
Output is correct |
3 |
Correct |
278 ms |
26488 KB |
Output is correct |
4 |
Correct |
144 ms |
23348 KB |
Output is correct |
5 |
Correct |
203 ms |
26332 KB |
Output is correct |
6 |
Correct |
136 ms |
23560 KB |
Output is correct |
7 |
Correct |
307 ms |
26112 KB |
Output is correct |
8 |
Correct |
192 ms |
23544 KB |
Output is correct |
9 |
Correct |
141 ms |
28536 KB |
Output is correct |
10 |
Correct |
240 ms |
25404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5240 KB |
Output is correct |
5 |
Correct |
116 ms |
20000 KB |
Output is correct |
6 |
Correct |
65 ms |
24828 KB |
Output is correct |
7 |
Correct |
119 ms |
23172 KB |
Output is correct |
8 |
Correct |
92 ms |
20344 KB |
Output is correct |
9 |
Correct |
113 ms |
22208 KB |
Output is correct |
10 |
Correct |
82 ms |
20472 KB |
Output is correct |
11 |
Correct |
9 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5368 KB |
Output is correct |
13 |
Correct |
8 ms |
5368 KB |
Output is correct |
14 |
Correct |
7 ms |
5240 KB |
Output is correct |
15 |
Correct |
8 ms |
5368 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
7 ms |
5240 KB |
Output is correct |
18 |
Correct |
8 ms |
5368 KB |
Output is correct |
19 |
Correct |
7 ms |
5240 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5240 KB |
Output is correct |
5 |
Correct |
116 ms |
20000 KB |
Output is correct |
6 |
Correct |
65 ms |
24828 KB |
Output is correct |
7 |
Correct |
119 ms |
23172 KB |
Output is correct |
8 |
Correct |
92 ms |
20344 KB |
Output is correct |
9 |
Correct |
113 ms |
22208 KB |
Output is correct |
10 |
Correct |
82 ms |
20472 KB |
Output is correct |
11 |
Correct |
6 ms |
5084 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
5240 KB |
Output is correct |
14 |
Correct |
157 ms |
28524 KB |
Output is correct |
15 |
Correct |
145 ms |
28536 KB |
Output is correct |
16 |
Correct |
129 ms |
28536 KB |
Output is correct |
17 |
Correct |
151 ms |
28468 KB |
Output is correct |
18 |
Correct |
149 ms |
28588 KB |
Output is correct |
19 |
Correct |
137 ms |
28644 KB |
Output is correct |
20 |
Correct |
143 ms |
28392 KB |
Output is correct |
21 |
Correct |
17 ms |
6008 KB |
Output is correct |
22 |
Correct |
146 ms |
28832 KB |
Output is correct |
23 |
Correct |
151 ms |
28792 KB |
Output is correct |
24 |
Correct |
137 ms |
28792 KB |
Output is correct |
25 |
Correct |
152 ms |
28792 KB |
Output is correct |
26 |
Correct |
134 ms |
28792 KB |
Output is correct |
27 |
Correct |
154 ms |
28664 KB |
Output is correct |
28 |
Correct |
151 ms |
28720 KB |
Output is correct |
29 |
Correct |
139 ms |
28792 KB |
Output is correct |
30 |
Correct |
153 ms |
28792 KB |
Output is correct |
31 |
Correct |
209 ms |
23076 KB |
Output is correct |
32 |
Correct |
134 ms |
28508 KB |
Output is correct |
33 |
Correct |
278 ms |
26488 KB |
Output is correct |
34 |
Correct |
144 ms |
23348 KB |
Output is correct |
35 |
Correct |
203 ms |
26332 KB |
Output is correct |
36 |
Correct |
136 ms |
23560 KB |
Output is correct |
37 |
Correct |
307 ms |
26112 KB |
Output is correct |
38 |
Correct |
192 ms |
23544 KB |
Output is correct |
39 |
Correct |
141 ms |
28536 KB |
Output is correct |
40 |
Correct |
240 ms |
25404 KB |
Output is correct |
41 |
Correct |
9 ms |
5240 KB |
Output is correct |
42 |
Correct |
7 ms |
5368 KB |
Output is correct |
43 |
Correct |
8 ms |
5368 KB |
Output is correct |
44 |
Correct |
7 ms |
5240 KB |
Output is correct |
45 |
Correct |
8 ms |
5368 KB |
Output is correct |
46 |
Correct |
7 ms |
5240 KB |
Output is correct |
47 |
Correct |
7 ms |
5240 KB |
Output is correct |
48 |
Correct |
8 ms |
5368 KB |
Output is correct |
49 |
Correct |
7 ms |
5240 KB |
Output is correct |
50 |
Correct |
8 ms |
5368 KB |
Output is correct |
51 |
Correct |
192 ms |
23928 KB |
Output is correct |
52 |
Correct |
179 ms |
28880 KB |
Output is correct |
53 |
Correct |
232 ms |
25716 KB |
Output is correct |
54 |
Correct |
145 ms |
23800 KB |
Output is correct |
55 |
Correct |
177 ms |
23284 KB |
Output is correct |
56 |
Correct |
146 ms |
28664 KB |
Output is correct |
57 |
Correct |
230 ms |
26300 KB |
Output is correct |
58 |
Correct |
167 ms |
23672 KB |
Output is correct |
59 |
Correct |
227 ms |
23800 KB |
Output is correct |
60 |
Correct |
185 ms |
28724 KB |
Output is correct |
61 |
Correct |
228 ms |
26432 KB |
Output is correct |
62 |
Correct |
147 ms |
23732 KB |
Output is correct |
63 |
Correct |
181 ms |
23424 KB |
Output is correct |
64 |
Correct |
163 ms |
28672 KB |
Output is correct |
65 |
Correct |
252 ms |
26124 KB |
Output is correct |
66 |
Correct |
154 ms |
23800 KB |
Output is correct |
67 |
Correct |
191 ms |
23508 KB |
Output is correct |
68 |
Correct |
192 ms |
28884 KB |
Output is correct |
69 |
Correct |
243 ms |
25592 KB |
Output is correct |
70 |
Correct |
148 ms |
23832 KB |
Output is correct |