#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
vector <int> L[DIM];
pair <int,int> rmq[25][DIM*3],poz[DIM];
int E[DIM*3],p[DIM*3],level[DIM],first[DIM],aib[DIM],sp[DIM],dp[DIM];
struct idk{
int x,y,cost;
};
vector <idk> v[DIM];
int n,m,i,j,x,y,cost,k,idx;
void dfs (int nod, int tata){
level[nod] = 1 + level[tata];
E[++k] = nod;
first[nod] = k;
poz[nod].first = ++idx;
for (auto vecin : L[nod])
if (vecin != tata){
dfs (vecin,nod);
E[++k] = nod;
}
poz[nod].second = idx;
}
int get_lca (int x, int y){
int posx = first[x], posy = first[y];
if (posx > posy)
swap (posx,posy);
int nr = p[posy-posx+1];
pair <int,int> sol = min (rmq[nr][posx],rmq[nr][posy-(1<<nr)+1]);
return E[sol.second];
}
void update (int p, int val){
for (;p<=n;p+=(p&-p))
aib[p] += val;
}
int query (int p){
int sol = 0;
for (;p;p-=(p&-p))
sol += aib[p];
return sol;
}
void dfs2 (int nod, int tata){
for (auto vecin : L[nod])
if (vecin != tata){
dfs2 (vecin,nod);
sp[nod] += dp[vecin];
}
dp[nod] = sp[nod]; /// inca nu iau un lant care are lca in nod
for (auto it : v[nod]){
int x = it.x, y = it.y, cost = it.cost;
dp[nod] = max (dp[nod],query(poz[x].first) + query(poz[y].first) + cost + sp[nod]);
}
update (poz[nod].first, sp[nod] - dp[nod]);
update (poz[nod].second+1, dp[nod] - sp[nod]);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<n;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
dfs (1,0);
for (i=1;i<=k;i++)
rmq[0][i] = make_pair(level[E[i]],i);
for (i=1;(1<<i)<=k;i++)
for (j=1;j<=k;j++){
rmq[i][j] = rmq[i-1][j];
if (j + (1<<(i-1)) <= k && rmq[i-1][j + (1<<(i-1))].first < rmq[i][j].first)
rmq[i][j] = rmq[i-1][j + (1<<(i-1))];
}
for (i=2;i<=k;i++)
p[i] = p[i/2] + 1;
cin>>m;
for (i=1;i<=m;i++){
cin>>x>>y>>cost;
v[get_lca(x,y)].push_back({x,y,cost});
}
dfs2 (1,0);
cout<<dp[1];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4996 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
141 ms |
41924 KB |
Output is correct |
6 |
Correct |
130 ms |
51168 KB |
Output is correct |
7 |
Correct |
131 ms |
47860 KB |
Output is correct |
8 |
Correct |
122 ms |
41964 KB |
Output is correct |
9 |
Correct |
125 ms |
46048 KB |
Output is correct |
10 |
Correct |
122 ms |
41960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5324 KB |
Output is correct |
4 |
Correct |
215 ms |
54488 KB |
Output is correct |
5 |
Correct |
209 ms |
54468 KB |
Output is correct |
6 |
Correct |
224 ms |
54424 KB |
Output is correct |
7 |
Correct |
209 ms |
54424 KB |
Output is correct |
8 |
Correct |
214 ms |
54492 KB |
Output is correct |
9 |
Correct |
208 ms |
54464 KB |
Output is correct |
10 |
Correct |
215 ms |
54420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5324 KB |
Output is correct |
4 |
Correct |
215 ms |
54488 KB |
Output is correct |
5 |
Correct |
209 ms |
54468 KB |
Output is correct |
6 |
Correct |
224 ms |
54424 KB |
Output is correct |
7 |
Correct |
209 ms |
54424 KB |
Output is correct |
8 |
Correct |
214 ms |
54492 KB |
Output is correct |
9 |
Correct |
208 ms |
54464 KB |
Output is correct |
10 |
Correct |
215 ms |
54420 KB |
Output is correct |
11 |
Correct |
27 ms |
6168 KB |
Output is correct |
12 |
Correct |
225 ms |
54780 KB |
Output is correct |
13 |
Correct |
233 ms |
54760 KB |
Output is correct |
14 |
Correct |
232 ms |
54756 KB |
Output is correct |
15 |
Correct |
216 ms |
54724 KB |
Output is correct |
16 |
Correct |
242 ms |
54760 KB |
Output is correct |
17 |
Correct |
223 ms |
54724 KB |
Output is correct |
18 |
Correct |
289 ms |
54708 KB |
Output is correct |
19 |
Correct |
223 ms |
54724 KB |
Output is correct |
20 |
Correct |
218 ms |
54816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
44796 KB |
Output is correct |
2 |
Correct |
220 ms |
54632 KB |
Output is correct |
3 |
Correct |
261 ms |
50860 KB |
Output is correct |
4 |
Correct |
245 ms |
44864 KB |
Output is correct |
5 |
Correct |
266 ms |
50348 KB |
Output is correct |
6 |
Correct |
225 ms |
44792 KB |
Output is correct |
7 |
Correct |
253 ms |
49956 KB |
Output is correct |
8 |
Correct |
270 ms |
45008 KB |
Output is correct |
9 |
Correct |
213 ms |
54524 KB |
Output is correct |
10 |
Correct |
254 ms |
48696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4996 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
141 ms |
41924 KB |
Output is correct |
6 |
Correct |
130 ms |
51168 KB |
Output is correct |
7 |
Correct |
131 ms |
47860 KB |
Output is correct |
8 |
Correct |
122 ms |
41964 KB |
Output is correct |
9 |
Correct |
125 ms |
46048 KB |
Output is correct |
10 |
Correct |
122 ms |
41960 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
5324 KB |
Output is correct |
14 |
Correct |
5 ms |
5324 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
5 ms |
5396 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
6 ms |
5396 KB |
Output is correct |
19 |
Correct |
5 ms |
5276 KB |
Output is correct |
20 |
Correct |
7 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4996 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
141 ms |
41924 KB |
Output is correct |
6 |
Correct |
130 ms |
51168 KB |
Output is correct |
7 |
Correct |
131 ms |
47860 KB |
Output is correct |
8 |
Correct |
122 ms |
41964 KB |
Output is correct |
9 |
Correct |
125 ms |
46048 KB |
Output is correct |
10 |
Correct |
122 ms |
41960 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5324 KB |
Output is correct |
14 |
Correct |
215 ms |
54488 KB |
Output is correct |
15 |
Correct |
209 ms |
54468 KB |
Output is correct |
16 |
Correct |
224 ms |
54424 KB |
Output is correct |
17 |
Correct |
209 ms |
54424 KB |
Output is correct |
18 |
Correct |
214 ms |
54492 KB |
Output is correct |
19 |
Correct |
208 ms |
54464 KB |
Output is correct |
20 |
Correct |
215 ms |
54420 KB |
Output is correct |
21 |
Correct |
27 ms |
6168 KB |
Output is correct |
22 |
Correct |
225 ms |
54780 KB |
Output is correct |
23 |
Correct |
233 ms |
54760 KB |
Output is correct |
24 |
Correct |
232 ms |
54756 KB |
Output is correct |
25 |
Correct |
216 ms |
54724 KB |
Output is correct |
26 |
Correct |
242 ms |
54760 KB |
Output is correct |
27 |
Correct |
223 ms |
54724 KB |
Output is correct |
28 |
Correct |
289 ms |
54708 KB |
Output is correct |
29 |
Correct |
223 ms |
54724 KB |
Output is correct |
30 |
Correct |
218 ms |
54816 KB |
Output is correct |
31 |
Correct |
254 ms |
44796 KB |
Output is correct |
32 |
Correct |
220 ms |
54632 KB |
Output is correct |
33 |
Correct |
261 ms |
50860 KB |
Output is correct |
34 |
Correct |
245 ms |
44864 KB |
Output is correct |
35 |
Correct |
266 ms |
50348 KB |
Output is correct |
36 |
Correct |
225 ms |
44792 KB |
Output is correct |
37 |
Correct |
253 ms |
49956 KB |
Output is correct |
38 |
Correct |
270 ms |
45008 KB |
Output is correct |
39 |
Correct |
213 ms |
54524 KB |
Output is correct |
40 |
Correct |
254 ms |
48696 KB |
Output is correct |
41 |
Correct |
6 ms |
5324 KB |
Output is correct |
42 |
Correct |
5 ms |
5452 KB |
Output is correct |
43 |
Correct |
5 ms |
5324 KB |
Output is correct |
44 |
Correct |
5 ms |
5324 KB |
Output is correct |
45 |
Correct |
6 ms |
5324 KB |
Output is correct |
46 |
Correct |
5 ms |
5396 KB |
Output is correct |
47 |
Correct |
5 ms |
5324 KB |
Output is correct |
48 |
Correct |
6 ms |
5396 KB |
Output is correct |
49 |
Correct |
5 ms |
5276 KB |
Output is correct |
50 |
Correct |
7 ms |
5452 KB |
Output is correct |
51 |
Correct |
317 ms |
45380 KB |
Output is correct |
52 |
Correct |
274 ms |
54692 KB |
Output is correct |
53 |
Correct |
262 ms |
49168 KB |
Output is correct |
54 |
Correct |
230 ms |
45236 KB |
Output is correct |
55 |
Correct |
254 ms |
45032 KB |
Output is correct |
56 |
Correct |
236 ms |
54704 KB |
Output is correct |
57 |
Correct |
283 ms |
49984 KB |
Output is correct |
58 |
Correct |
251 ms |
45016 KB |
Output is correct |
59 |
Correct |
320 ms |
45216 KB |
Output is correct |
60 |
Correct |
260 ms |
54748 KB |
Output is correct |
61 |
Correct |
265 ms |
50140 KB |
Output is correct |
62 |
Correct |
236 ms |
44840 KB |
Output is correct |
63 |
Correct |
278 ms |
44800 KB |
Output is correct |
64 |
Correct |
267 ms |
54716 KB |
Output is correct |
65 |
Correct |
266 ms |
49896 KB |
Output is correct |
66 |
Correct |
261 ms |
45108 KB |
Output is correct |
67 |
Correct |
267 ms |
45008 KB |
Output is correct |
68 |
Correct |
222 ms |
54724 KB |
Output is correct |
69 |
Correct |
266 ms |
48548 KB |
Output is correct |
70 |
Correct |
248 ms |
45192 KB |
Output is correct |