#include <bits/stdc++.h>
//#include "race.h"
#define ll long long
#define pii pair<ll, ll>
#define fi first
#define se second
#define pb push_back
using namespace std;
const ll MX=200007, INF=1e9+7;
ll N, K, ret=INF;
map<ll, ll> ada[MX];
vector<pii> adj[MX];
pii lazy[MX];
void DFS(int par, int idx){
lazy[idx]={0, 0};
ada[idx][0]=0;
for(auto i:adj[idx]){
int v=i.fi, w=i.se;
if(v==par)
continue;
DFS(idx, v);
lazy[v].fi+=w;
lazy[v].se++;
if(ada[idx].size()<ada[v].size()){
swap(ada[idx], ada[v]);
swap(lazy[idx], lazy[v]);
}
for(auto jj:ada[v]){
pii j=jj;
j.fi+=lazy[v].fi-lazy[idx].fi;
j.se+=lazy[v].se-lazy[idx].se;
if(ada[idx].count(K-j.fi-lazy[idx].fi*2))
ret=min(ret, lazy[idx].se*2+j.se+ada[idx][K-j.fi-lazy[idx].fi*2]);
}
for(auto jj:ada[v]){
pii j=jj;
j.fi+=lazy[v].fi-lazy[idx].fi;
j.se+=lazy[v].se-lazy[idx].se;
if(ada[idx].count(j.fi))
ada[idx][j.fi]=min(ada[idx][j.fi], j.se);
else
ada[idx][j.fi]=j.se;
}
}
if(ada[idx].count(K-lazy[idx].fi))
ret=min(ret, lazy[idx].se+ada[idx][K-lazy[idx].fi]);
}
int best_path(int n, int k, int H[][2], int L[]){
N=n, K=k;
for(int i=0, u, v, w; i<N-1; i++){
u=H[i][0], v=H[i][1], w=L[i];
adj[u].pb({v, w});
adj[v].pb({u, w});
}
DFS(0, 0);
return (ret>=INF?-1:ret);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14440 KB |
Output is correct |
2 |
Correct |
12 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14440 KB |
Output is correct |
4 |
Correct |
12 ms |
14352 KB |
Output is correct |
5 |
Correct |
10 ms |
14388 KB |
Output is correct |
6 |
Correct |
10 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
10 ms |
14332 KB |
Output is correct |
9 |
Correct |
10 ms |
14424 KB |
Output is correct |
10 |
Correct |
9 ms |
14392 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14332 KB |
Output is correct |
13 |
Correct |
9 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14392 KB |
Output is correct |
15 |
Correct |
10 ms |
14352 KB |
Output is correct |
16 |
Correct |
11 ms |
14416 KB |
Output is correct |
17 |
Correct |
10 ms |
14412 KB |
Output is correct |
18 |
Correct |
12 ms |
14364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14440 KB |
Output is correct |
2 |
Correct |
12 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14440 KB |
Output is correct |
4 |
Correct |
12 ms |
14352 KB |
Output is correct |
5 |
Correct |
10 ms |
14388 KB |
Output is correct |
6 |
Correct |
10 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
10 ms |
14332 KB |
Output is correct |
9 |
Correct |
10 ms |
14424 KB |
Output is correct |
10 |
Correct |
9 ms |
14392 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14332 KB |
Output is correct |
13 |
Correct |
9 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14392 KB |
Output is correct |
15 |
Correct |
10 ms |
14352 KB |
Output is correct |
16 |
Correct |
11 ms |
14416 KB |
Output is correct |
17 |
Correct |
10 ms |
14412 KB |
Output is correct |
18 |
Correct |
12 ms |
14364 KB |
Output is correct |
19 |
Correct |
10 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14396 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14660 KB |
Output is correct |
23 |
Correct |
11 ms |
14756 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
14 ms |
14668 KB |
Output is correct |
26 |
Correct |
11 ms |
14668 KB |
Output is correct |
27 |
Correct |
10 ms |
14540 KB |
Output is correct |
28 |
Correct |
11 ms |
14668 KB |
Output is correct |
29 |
Correct |
11 ms |
14728 KB |
Output is correct |
30 |
Correct |
13 ms |
14660 KB |
Output is correct |
31 |
Correct |
14 ms |
14660 KB |
Output is correct |
32 |
Correct |
12 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
14692 KB |
Output is correct |
34 |
Correct |
10 ms |
14616 KB |
Output is correct |
35 |
Correct |
11 ms |
14620 KB |
Output is correct |
36 |
Correct |
10 ms |
14564 KB |
Output is correct |
37 |
Correct |
10 ms |
14636 KB |
Output is correct |
38 |
Correct |
10 ms |
14656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14440 KB |
Output is correct |
2 |
Correct |
12 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14440 KB |
Output is correct |
4 |
Correct |
12 ms |
14352 KB |
Output is correct |
5 |
Correct |
10 ms |
14388 KB |
Output is correct |
6 |
Correct |
10 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
10 ms |
14332 KB |
Output is correct |
9 |
Correct |
10 ms |
14424 KB |
Output is correct |
10 |
Correct |
9 ms |
14392 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14332 KB |
Output is correct |
13 |
Correct |
9 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14392 KB |
Output is correct |
15 |
Correct |
10 ms |
14352 KB |
Output is correct |
16 |
Correct |
11 ms |
14416 KB |
Output is correct |
17 |
Correct |
10 ms |
14412 KB |
Output is correct |
18 |
Correct |
12 ms |
14364 KB |
Output is correct |
19 |
Correct |
154 ms |
39200 KB |
Output is correct |
20 |
Correct |
145 ms |
39108 KB |
Output is correct |
21 |
Correct |
129 ms |
38828 KB |
Output is correct |
22 |
Correct |
142 ms |
38488 KB |
Output is correct |
23 |
Correct |
171 ms |
51396 KB |
Output is correct |
24 |
Correct |
136 ms |
41340 KB |
Output is correct |
25 |
Correct |
127 ms |
40004 KB |
Output is correct |
26 |
Correct |
64 ms |
48220 KB |
Output is correct |
27 |
Correct |
213 ms |
52604 KB |
Output is correct |
28 |
Correct |
320 ms |
94844 KB |
Output is correct |
29 |
Correct |
326 ms |
92992 KB |
Output is correct |
30 |
Correct |
212 ms |
52592 KB |
Output is correct |
31 |
Correct |
191 ms |
52548 KB |
Output is correct |
32 |
Correct |
261 ms |
52660 KB |
Output is correct |
33 |
Correct |
254 ms |
57536 KB |
Output is correct |
34 |
Correct |
363 ms |
90404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14440 KB |
Output is correct |
2 |
Correct |
12 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14440 KB |
Output is correct |
4 |
Correct |
12 ms |
14352 KB |
Output is correct |
5 |
Correct |
10 ms |
14388 KB |
Output is correct |
6 |
Correct |
10 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
10 ms |
14332 KB |
Output is correct |
9 |
Correct |
10 ms |
14424 KB |
Output is correct |
10 |
Correct |
9 ms |
14392 KB |
Output is correct |
11 |
Correct |
9 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14332 KB |
Output is correct |
13 |
Correct |
9 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14392 KB |
Output is correct |
15 |
Correct |
10 ms |
14352 KB |
Output is correct |
16 |
Correct |
11 ms |
14416 KB |
Output is correct |
17 |
Correct |
10 ms |
14412 KB |
Output is correct |
18 |
Correct |
12 ms |
14364 KB |
Output is correct |
19 |
Correct |
10 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14396 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14660 KB |
Output is correct |
23 |
Correct |
11 ms |
14756 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
14 ms |
14668 KB |
Output is correct |
26 |
Correct |
11 ms |
14668 KB |
Output is correct |
27 |
Correct |
10 ms |
14540 KB |
Output is correct |
28 |
Correct |
11 ms |
14668 KB |
Output is correct |
29 |
Correct |
11 ms |
14728 KB |
Output is correct |
30 |
Correct |
13 ms |
14660 KB |
Output is correct |
31 |
Correct |
14 ms |
14660 KB |
Output is correct |
32 |
Correct |
12 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
14692 KB |
Output is correct |
34 |
Correct |
10 ms |
14616 KB |
Output is correct |
35 |
Correct |
11 ms |
14620 KB |
Output is correct |
36 |
Correct |
10 ms |
14564 KB |
Output is correct |
37 |
Correct |
10 ms |
14636 KB |
Output is correct |
38 |
Correct |
10 ms |
14656 KB |
Output is correct |
39 |
Correct |
154 ms |
39200 KB |
Output is correct |
40 |
Correct |
145 ms |
39108 KB |
Output is correct |
41 |
Correct |
129 ms |
38828 KB |
Output is correct |
42 |
Correct |
142 ms |
38488 KB |
Output is correct |
43 |
Correct |
171 ms |
51396 KB |
Output is correct |
44 |
Correct |
136 ms |
41340 KB |
Output is correct |
45 |
Correct |
127 ms |
40004 KB |
Output is correct |
46 |
Correct |
64 ms |
48220 KB |
Output is correct |
47 |
Correct |
213 ms |
52604 KB |
Output is correct |
48 |
Correct |
320 ms |
94844 KB |
Output is correct |
49 |
Correct |
326 ms |
92992 KB |
Output is correct |
50 |
Correct |
212 ms |
52592 KB |
Output is correct |
51 |
Correct |
191 ms |
52548 KB |
Output is correct |
52 |
Correct |
261 ms |
52660 KB |
Output is correct |
53 |
Correct |
254 ms |
57536 KB |
Output is correct |
54 |
Correct |
363 ms |
90404 KB |
Output is correct |
55 |
Correct |
25 ms |
17868 KB |
Output is correct |
56 |
Correct |
25 ms |
16448 KB |
Output is correct |
57 |
Correct |
88 ms |
36676 KB |
Output is correct |
58 |
Correct |
54 ms |
29136 KB |
Output is correct |
59 |
Correct |
95 ms |
54460 KB |
Output is correct |
60 |
Correct |
299 ms |
93456 KB |
Output is correct |
61 |
Correct |
243 ms |
56112 KB |
Output is correct |
62 |
Correct |
185 ms |
52656 KB |
Output is correct |
63 |
Correct |
260 ms |
52676 KB |
Output is correct |
64 |
Correct |
478 ms |
107520 KB |
Output is correct |
65 |
Correct |
482 ms |
105604 KB |
Output is correct |
66 |
Correct |
347 ms |
88868 KB |
Output is correct |
67 |
Correct |
177 ms |
45432 KB |
Output is correct |
68 |
Correct |
371 ms |
69020 KB |
Output is correct |
69 |
Correct |
365 ms |
73800 KB |
Output is correct |
70 |
Correct |
369 ms |
66600 KB |
Output is correct |