#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
const int mxN = 1e6+5;
using pii = pair<int,int>;
set<pii> tr[mxN];
int sbsz[mxN], seen[mxN], N, K, res = LLONG_MAX, within;
int gsb(int x, int p=-1) {
sbsz[x] = 1ll;
#define nn it.second
for(auto it : tr[x]) if(!seen[nn] && nn != p) sbsz[x] += gsb(nn, x);
return sbsz[x];
}
int gc(int x, int p=-1) {
for(auto it : tr[x]) if(!seen[nn] && nn != p && sbsz[nn]*2ll>within) return gc(nn,x);
seen[x]=1; return x;
}
gp_hash_table<int,int> rol, loc;
void pdfs(int x, int p, int dist, int len) {
assert(!seen[x]);
assert(dist >= 0);
if(dist > K) return;
if(loc.find(dist) == loc.end()) loc[dist] = len; else loc[dist] = min(loc[dist], len);
for(auto it : tr[x]) if(!seen[nn] && nn != p) {
pdfs(nn, x, dist + it.first, len+1ll);
}
}
int le = -1;
void de(int x){
within = gsb(x,-1);
int cen = gc(x,-1);
rol.clear(); loc.clear();
rol.insert({0,0});
for(auto it : tr[cen]) if(!seen[nn]) {
loc.clear();
pdfs(nn, cen, it.first, 1ll);
for(auto en : loc) {
if(K-en.first < 0) continue;
if(rol.find(K-en.first) != rol.end()) res = min(res, en.second+rol[K-en.first]);
}
for(auto en : loc) { if(en.first < 0 || en.first > K) continue; if(rol.find(en.first) == rol.end()) rol[en.first] = en.second; else rol[en.first] = min(rol[en.first], en.second); }
}
for(auto it : tr[cen]) if(!seen[nn]) {
de(nn);
}
}
signed best_path(signed n, signed k, signed h[][2], signed l[]) {
N = n, K = k;
for(int i = 0; i < N-1; i++) {
int a = h[i][0], b = h[i][1], w = l[i];
tr[a].insert({w,b});
tr[b].insert({w,a});
}
de(0);
if(res == LLONG_MAX) return -1; else return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
24 ms |
47316 KB |
Output is correct |
3 |
Correct |
24 ms |
47292 KB |
Output is correct |
4 |
Correct |
25 ms |
47272 KB |
Output is correct |
5 |
Correct |
24 ms |
47264 KB |
Output is correct |
6 |
Correct |
28 ms |
47276 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Correct |
26 ms |
47260 KB |
Output is correct |
9 |
Correct |
27 ms |
47212 KB |
Output is correct |
10 |
Correct |
26 ms |
47316 KB |
Output is correct |
11 |
Correct |
29 ms |
47220 KB |
Output is correct |
12 |
Correct |
24 ms |
47312 KB |
Output is correct |
13 |
Correct |
29 ms |
47316 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
24 ms |
47256 KB |
Output is correct |
16 |
Correct |
26 ms |
47224 KB |
Output is correct |
17 |
Correct |
24 ms |
47324 KB |
Output is correct |
18 |
Correct |
23 ms |
47284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
24 ms |
47316 KB |
Output is correct |
3 |
Correct |
24 ms |
47292 KB |
Output is correct |
4 |
Correct |
25 ms |
47272 KB |
Output is correct |
5 |
Correct |
24 ms |
47264 KB |
Output is correct |
6 |
Correct |
28 ms |
47276 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Correct |
26 ms |
47260 KB |
Output is correct |
9 |
Correct |
27 ms |
47212 KB |
Output is correct |
10 |
Correct |
26 ms |
47316 KB |
Output is correct |
11 |
Correct |
29 ms |
47220 KB |
Output is correct |
12 |
Correct |
24 ms |
47312 KB |
Output is correct |
13 |
Correct |
29 ms |
47316 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
24 ms |
47256 KB |
Output is correct |
16 |
Correct |
26 ms |
47224 KB |
Output is correct |
17 |
Correct |
24 ms |
47324 KB |
Output is correct |
18 |
Correct |
23 ms |
47284 KB |
Output is correct |
19 |
Correct |
25 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47224 KB |
Output is correct |
21 |
Correct |
27 ms |
47444 KB |
Output is correct |
22 |
Correct |
27 ms |
47368 KB |
Output is correct |
23 |
Correct |
27 ms |
47392 KB |
Output is correct |
24 |
Correct |
30 ms |
47368 KB |
Output is correct |
25 |
Correct |
26 ms |
47564 KB |
Output is correct |
26 |
Correct |
25 ms |
47348 KB |
Output is correct |
27 |
Correct |
28 ms |
47376 KB |
Output is correct |
28 |
Correct |
30 ms |
47440 KB |
Output is correct |
29 |
Correct |
27 ms |
47556 KB |
Output is correct |
30 |
Correct |
27 ms |
47572 KB |
Output is correct |
31 |
Correct |
25 ms |
47572 KB |
Output is correct |
32 |
Correct |
30 ms |
47572 KB |
Output is correct |
33 |
Correct |
27 ms |
47552 KB |
Output is correct |
34 |
Correct |
28 ms |
47384 KB |
Output is correct |
35 |
Correct |
28 ms |
47436 KB |
Output is correct |
36 |
Correct |
32 ms |
47440 KB |
Output is correct |
37 |
Correct |
27 ms |
47440 KB |
Output is correct |
38 |
Correct |
25 ms |
47476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
24 ms |
47316 KB |
Output is correct |
3 |
Correct |
24 ms |
47292 KB |
Output is correct |
4 |
Correct |
25 ms |
47272 KB |
Output is correct |
5 |
Correct |
24 ms |
47264 KB |
Output is correct |
6 |
Correct |
28 ms |
47276 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Correct |
26 ms |
47260 KB |
Output is correct |
9 |
Correct |
27 ms |
47212 KB |
Output is correct |
10 |
Correct |
26 ms |
47316 KB |
Output is correct |
11 |
Correct |
29 ms |
47220 KB |
Output is correct |
12 |
Correct |
24 ms |
47312 KB |
Output is correct |
13 |
Correct |
29 ms |
47316 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
24 ms |
47256 KB |
Output is correct |
16 |
Correct |
26 ms |
47224 KB |
Output is correct |
17 |
Correct |
24 ms |
47324 KB |
Output is correct |
18 |
Correct |
23 ms |
47284 KB |
Output is correct |
19 |
Correct |
275 ms |
63864 KB |
Output is correct |
20 |
Correct |
272 ms |
63924 KB |
Output is correct |
21 |
Correct |
319 ms |
63920 KB |
Output is correct |
22 |
Correct |
282 ms |
63948 KB |
Output is correct |
23 |
Correct |
186 ms |
63988 KB |
Output is correct |
24 |
Correct |
168 ms |
63996 KB |
Output is correct |
25 |
Correct |
246 ms |
67212 KB |
Output is correct |
26 |
Correct |
150 ms |
70156 KB |
Output is correct |
27 |
Correct |
411 ms |
80600 KB |
Output is correct |
28 |
Correct |
541 ms |
93492 KB |
Output is correct |
29 |
Correct |
526 ms |
92452 KB |
Output is correct |
30 |
Correct |
427 ms |
80584 KB |
Output is correct |
31 |
Correct |
412 ms |
80556 KB |
Output is correct |
32 |
Correct |
457 ms |
80668 KB |
Output is correct |
33 |
Correct |
505 ms |
80760 KB |
Output is correct |
34 |
Correct |
325 ms |
81568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
24 ms |
47316 KB |
Output is correct |
3 |
Correct |
24 ms |
47292 KB |
Output is correct |
4 |
Correct |
25 ms |
47272 KB |
Output is correct |
5 |
Correct |
24 ms |
47264 KB |
Output is correct |
6 |
Correct |
28 ms |
47276 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Correct |
26 ms |
47260 KB |
Output is correct |
9 |
Correct |
27 ms |
47212 KB |
Output is correct |
10 |
Correct |
26 ms |
47316 KB |
Output is correct |
11 |
Correct |
29 ms |
47220 KB |
Output is correct |
12 |
Correct |
24 ms |
47312 KB |
Output is correct |
13 |
Correct |
29 ms |
47316 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
24 ms |
47256 KB |
Output is correct |
16 |
Correct |
26 ms |
47224 KB |
Output is correct |
17 |
Correct |
24 ms |
47324 KB |
Output is correct |
18 |
Correct |
23 ms |
47284 KB |
Output is correct |
19 |
Correct |
25 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47224 KB |
Output is correct |
21 |
Correct |
27 ms |
47444 KB |
Output is correct |
22 |
Correct |
27 ms |
47368 KB |
Output is correct |
23 |
Correct |
27 ms |
47392 KB |
Output is correct |
24 |
Correct |
30 ms |
47368 KB |
Output is correct |
25 |
Correct |
26 ms |
47564 KB |
Output is correct |
26 |
Correct |
25 ms |
47348 KB |
Output is correct |
27 |
Correct |
28 ms |
47376 KB |
Output is correct |
28 |
Correct |
30 ms |
47440 KB |
Output is correct |
29 |
Correct |
27 ms |
47556 KB |
Output is correct |
30 |
Correct |
27 ms |
47572 KB |
Output is correct |
31 |
Correct |
25 ms |
47572 KB |
Output is correct |
32 |
Correct |
30 ms |
47572 KB |
Output is correct |
33 |
Correct |
27 ms |
47552 KB |
Output is correct |
34 |
Correct |
28 ms |
47384 KB |
Output is correct |
35 |
Correct |
28 ms |
47436 KB |
Output is correct |
36 |
Correct |
32 ms |
47440 KB |
Output is correct |
37 |
Correct |
27 ms |
47440 KB |
Output is correct |
38 |
Correct |
25 ms |
47476 KB |
Output is correct |
39 |
Correct |
275 ms |
63864 KB |
Output is correct |
40 |
Correct |
272 ms |
63924 KB |
Output is correct |
41 |
Correct |
319 ms |
63920 KB |
Output is correct |
42 |
Correct |
282 ms |
63948 KB |
Output is correct |
43 |
Correct |
186 ms |
63988 KB |
Output is correct |
44 |
Correct |
168 ms |
63996 KB |
Output is correct |
45 |
Correct |
246 ms |
67212 KB |
Output is correct |
46 |
Correct |
150 ms |
70156 KB |
Output is correct |
47 |
Correct |
411 ms |
80600 KB |
Output is correct |
48 |
Correct |
541 ms |
93492 KB |
Output is correct |
49 |
Correct |
526 ms |
92452 KB |
Output is correct |
50 |
Correct |
427 ms |
80584 KB |
Output is correct |
51 |
Correct |
412 ms |
80556 KB |
Output is correct |
52 |
Correct |
457 ms |
80668 KB |
Output is correct |
53 |
Correct |
505 ms |
80760 KB |
Output is correct |
54 |
Correct |
325 ms |
81568 KB |
Output is correct |
55 |
Correct |
50 ms |
49344 KB |
Output is correct |
56 |
Correct |
40 ms |
48972 KB |
Output is correct |
57 |
Correct |
203 ms |
63836 KB |
Output is correct |
58 |
Correct |
159 ms |
63564 KB |
Output is correct |
59 |
Correct |
350 ms |
83196 KB |
Output is correct |
60 |
Correct |
1212 ms |
118772 KB |
Output is correct |
61 |
Correct |
469 ms |
80952 KB |
Output is correct |
62 |
Correct |
509 ms |
80788 KB |
Output is correct |
63 |
Correct |
577 ms |
80824 KB |
Output is correct |
64 |
Correct |
989 ms |
94752 KB |
Output is correct |
65 |
Correct |
432 ms |
81680 KB |
Output is correct |
66 |
Correct |
963 ms |
91152 KB |
Output is correct |
67 |
Correct |
436 ms |
81212 KB |
Output is correct |
68 |
Correct |
699 ms |
93220 KB |
Output is correct |
69 |
Correct |
677 ms |
93540 KB |
Output is correct |
70 |
Correct |
625 ms |
91636 KB |
Output is correct |