#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int MN=2e5+2;
int N,K,stsz[MN],ans=INT_MAX;
set<pii> adj[MN],minlen;
void find_stsz(int u, int p) {
stsz[u] = 1;
for(auto [v, l] : adj[u]) {
if(v == p) continue;
find_stsz(v, u);
stsz[u] += stsz[v];
}
}
int find_centroid(int u, int p, int root) {
for(auto [v, l] : adj[u]) {
if(v == p) continue;
if(stsz[v] > stsz[root]/2) return find_centroid(v, u, root);
}
return u;
}
void dfs_upd(int u, int p, int dist, int dep) {
if(dist > K) return;
auto it = minlen.lower_bound(pii(dist,0));
if(it == minlen.end() || (*it).first != dist) minlen.insert(pii(dist,dep));
else if((*it).second > dep) {
minlen.erase(it);
minlen.insert(pii(dist,dep));
}
for(auto [v, l] : adj[u]) {
if(v == p) continue;
dfs_upd(v, u, dist+l, dep+1);
}
}
void dfs_search(int u, int p, int dist, int dep) {
if(dist > K) return;
auto it = minlen.lower_bound(pii(K-dist,0));
if(it != minlen.end() && (*it).first == K-dist) ans = min(ans, (*it).second+dep);
for(auto [v, l] : adj[u]) {
if(v == p) continue;
dfs_search(v, u, dist+l, dep+1);
}
}
int best_path(int n, int k, int H[][2], int L[]) {
N = n;
K = k;
for(int i = 0; i < N-1; ++i) {
adj[H[i][0]].insert(pii(H[i][1], L[i]));
adj[H[i][1]].insert(pii(H[i][0], L[i]));
}
queue<int> q;
q.push(0);
while(!q.empty()) {
int u = q.front();
q.pop();
minlen.clear();
minlen.insert(pii(0,0));
find_stsz(u, -1);
int cen = find_centroid(u, -1, u);
for(auto [v, l] : adj[cen]) {
dfs_search(v, cen, l, 1);
dfs_upd(v, cen, l, 1);
adj[v].erase(pii(cen, l));
q.push(v);
}
}
return (ans==INT_MAX?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13404 KB |
Output is correct |
6 |
Correct |
2 ms |
13400 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13496 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13504 KB |
Output is correct |
18 |
Correct |
3 ms |
13400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13404 KB |
Output is correct |
6 |
Correct |
2 ms |
13400 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13496 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13504 KB |
Output is correct |
18 |
Correct |
3 ms |
13400 KB |
Output is correct |
19 |
Correct |
3 ms |
13404 KB |
Output is correct |
20 |
Correct |
2 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13592 KB |
Output is correct |
22 |
Correct |
3 ms |
13404 KB |
Output is correct |
23 |
Correct |
4 ms |
13404 KB |
Output is correct |
24 |
Correct |
3 ms |
13404 KB |
Output is correct |
25 |
Correct |
4 ms |
13400 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
4 ms |
13480 KB |
Output is correct |
31 |
Correct |
4 ms |
13484 KB |
Output is correct |
32 |
Correct |
4 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
6 ms |
13404 KB |
Output is correct |
35 |
Correct |
4 ms |
13404 KB |
Output is correct |
36 |
Correct |
4 ms |
13404 KB |
Output is correct |
37 |
Correct |
4 ms |
13404 KB |
Output is correct |
38 |
Correct |
6 ms |
13500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13404 KB |
Output is correct |
6 |
Correct |
2 ms |
13400 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13496 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13504 KB |
Output is correct |
18 |
Correct |
3 ms |
13400 KB |
Output is correct |
19 |
Correct |
269 ms |
25636 KB |
Output is correct |
20 |
Correct |
301 ms |
26648 KB |
Output is correct |
21 |
Correct |
268 ms |
26304 KB |
Output is correct |
22 |
Correct |
238 ms |
26756 KB |
Output is correct |
23 |
Correct |
143 ms |
26904 KB |
Output is correct |
24 |
Correct |
118 ms |
26968 KB |
Output is correct |
25 |
Correct |
236 ms |
30796 KB |
Output is correct |
26 |
Correct |
111 ms |
34060 KB |
Output is correct |
27 |
Correct |
347 ms |
36972 KB |
Output is correct |
28 |
Correct |
545 ms |
53048 KB |
Output is correct |
29 |
Correct |
503 ms |
51796 KB |
Output is correct |
30 |
Correct |
348 ms |
37332 KB |
Output is correct |
31 |
Correct |
322 ms |
37592 KB |
Output is correct |
32 |
Correct |
533 ms |
37344 KB |
Output is correct |
33 |
Correct |
517 ms |
37968 KB |
Output is correct |
34 |
Correct |
371 ms |
37972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13656 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13404 KB |
Output is correct |
6 |
Correct |
2 ms |
13400 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13496 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13504 KB |
Output is correct |
18 |
Correct |
3 ms |
13400 KB |
Output is correct |
19 |
Correct |
3 ms |
13404 KB |
Output is correct |
20 |
Correct |
2 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13592 KB |
Output is correct |
22 |
Correct |
3 ms |
13404 KB |
Output is correct |
23 |
Correct |
4 ms |
13404 KB |
Output is correct |
24 |
Correct |
3 ms |
13404 KB |
Output is correct |
25 |
Correct |
4 ms |
13400 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
4 ms |
13480 KB |
Output is correct |
31 |
Correct |
4 ms |
13484 KB |
Output is correct |
32 |
Correct |
4 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
6 ms |
13404 KB |
Output is correct |
35 |
Correct |
4 ms |
13404 KB |
Output is correct |
36 |
Correct |
4 ms |
13404 KB |
Output is correct |
37 |
Correct |
4 ms |
13404 KB |
Output is correct |
38 |
Correct |
6 ms |
13500 KB |
Output is correct |
39 |
Correct |
269 ms |
25636 KB |
Output is correct |
40 |
Correct |
301 ms |
26648 KB |
Output is correct |
41 |
Correct |
268 ms |
26304 KB |
Output is correct |
42 |
Correct |
238 ms |
26756 KB |
Output is correct |
43 |
Correct |
143 ms |
26904 KB |
Output is correct |
44 |
Correct |
118 ms |
26968 KB |
Output is correct |
45 |
Correct |
236 ms |
30796 KB |
Output is correct |
46 |
Correct |
111 ms |
34060 KB |
Output is correct |
47 |
Correct |
347 ms |
36972 KB |
Output is correct |
48 |
Correct |
545 ms |
53048 KB |
Output is correct |
49 |
Correct |
503 ms |
51796 KB |
Output is correct |
50 |
Correct |
348 ms |
37332 KB |
Output is correct |
51 |
Correct |
322 ms |
37592 KB |
Output is correct |
52 |
Correct |
533 ms |
37344 KB |
Output is correct |
53 |
Correct |
517 ms |
37968 KB |
Output is correct |
54 |
Correct |
371 ms |
37972 KB |
Output is correct |
55 |
Correct |
21 ms |
14684 KB |
Output is correct |
56 |
Correct |
15 ms |
14428 KB |
Output is correct |
57 |
Correct |
131 ms |
25664 KB |
Output is correct |
58 |
Correct |
61 ms |
26872 KB |
Output is correct |
59 |
Correct |
248 ms |
37460 KB |
Output is correct |
60 |
Correct |
1201 ms |
60628 KB |
Output is correct |
61 |
Correct |
378 ms |
36868 KB |
Output is correct |
62 |
Correct |
412 ms |
37564 KB |
Output is correct |
63 |
Correct |
624 ms |
37456 KB |
Output is correct |
64 |
Correct |
1266 ms |
40792 KB |
Output is correct |
65 |
Correct |
459 ms |
36832 KB |
Output is correct |
66 |
Correct |
836 ms |
47428 KB |
Output is correct |
67 |
Correct |
295 ms |
39248 KB |
Output is correct |
68 |
Correct |
682 ms |
40796 KB |
Output is correct |
69 |
Correct |
607 ms |
38780 KB |
Output is correct |
70 |
Correct |
533 ms |
40016 KB |
Output is correct |