#include <bits/stdc++.h>
using namespace std;
const int Ni = 200001;
const int Ki = 1e6+1;
vector<pair<int,int>> adj[Ni];
vector<int> cnt;
bool cen[Ni];
int sz[Ni];
int mini = Ni;
int k;
void dsz(int u, int p) {
sz[u] = 1;
for(pair<int,int> i : adj[u]) {
if (i.first != p && !cen[i.first]) {
dsz(i.first,u);
sz[u] += sz[i.first];
}
}
}
int centroid(int u,int p,int n) {
for(pair<int,int> i : adj[u]) {
if (i.first != p && 2*sz[i.first] > n && !cen[i.first]) return centroid(i.first,u,n);
}
return u;
}
void find(int u, int p, int currl, int ps, bool add) {
if (ps > k) return;
if (add) cnt[ps] = cnt[ps]==-1 ? currl : min(cnt[ps],currl);
else if (cnt[k-ps] != -1) mini = min(mini,currl+cnt[k-ps]);
for(pair<int,int> i : adj[u]) {
if (i.first != p && !cen[i.first]) {
find(i.first,u,currl+1,ps+i.second,add);
}
}
}
void del(int u, int p, int ps) {
if (ps > k) return;
cnt[ps] = -1;
for(pair<int,int> i : adj[u]){
if (i.first != p && !cen[i.first]) del(i.first,u,ps+i.second);
}
}
void solve(int u, int centr) {
dsz(u,centr);
int cent = centroid(u,centr,sz[u]);
cen[cent]=true;
for(pair<int,int> i : adj[cent]) {
if (!cen[i.first]) {
find(i.first,cent,1,i.second,0);
find(i.first,cent,1,i.second,1);
}
}
for(pair<int,int> i : adj[cent]) {
if (!cen[i.first]) {
del(i.first,cent,i.second);
}
}
for(pair<int,int> i : adj[cent]) {
if (!cen[i.first]) solve(i.first,cent);
}
}
int best_path(int N,int K,int H[][2],int L[]) {
for(int i = 0; i < N-1; i++) {
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
k=K;
cnt.resize(K+1);
fill(begin(cnt),end(cnt),-1);
cnt[0] = 0;
solve(0,-1);
return mini == Ni ? -1 : mini;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
8652 KB |
Output is correct |
23 |
Correct |
4 ms |
8012 KB |
Output is correct |
24 |
Correct |
5 ms |
8396 KB |
Output is correct |
25 |
Correct |
5 ms |
8268 KB |
Output is correct |
26 |
Correct |
3 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
4 ms |
6476 KB |
Output is correct |
31 |
Correct |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
4 ms |
7756 KB |
Output is correct |
33 |
Correct |
5 ms |
8012 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
4 ms |
8140 KB |
Output is correct |
36 |
Correct |
6 ms |
8672 KB |
Output is correct |
37 |
Correct |
4 ms |
8140 KB |
Output is correct |
38 |
Correct |
4 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
122 ms |
10280 KB |
Output is correct |
20 |
Correct |
120 ms |
10440 KB |
Output is correct |
21 |
Correct |
134 ms |
10252 KB |
Output is correct |
22 |
Correct |
146 ms |
10340 KB |
Output is correct |
23 |
Correct |
69 ms |
10568 KB |
Output is correct |
24 |
Correct |
49 ms |
10372 KB |
Output is correct |
25 |
Correct |
123 ms |
13904 KB |
Output is correct |
26 |
Correct |
90 ms |
17552 KB |
Output is correct |
27 |
Correct |
160 ms |
15764 KB |
Output is correct |
28 |
Correct |
246 ms |
30148 KB |
Output is correct |
29 |
Correct |
238 ms |
29080 KB |
Output is correct |
30 |
Correct |
167 ms |
18392 KB |
Output is correct |
31 |
Correct |
152 ms |
18208 KB |
Output is correct |
32 |
Correct |
191 ms |
18244 KB |
Output is correct |
33 |
Correct |
216 ms |
17172 KB |
Output is correct |
34 |
Correct |
157 ms |
17096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
8652 KB |
Output is correct |
23 |
Correct |
4 ms |
8012 KB |
Output is correct |
24 |
Correct |
5 ms |
8396 KB |
Output is correct |
25 |
Correct |
5 ms |
8268 KB |
Output is correct |
26 |
Correct |
3 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
4 ms |
6476 KB |
Output is correct |
31 |
Correct |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
4 ms |
7756 KB |
Output is correct |
33 |
Correct |
5 ms |
8012 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
4 ms |
8140 KB |
Output is correct |
36 |
Correct |
6 ms |
8672 KB |
Output is correct |
37 |
Correct |
4 ms |
8140 KB |
Output is correct |
38 |
Correct |
4 ms |
6988 KB |
Output is correct |
39 |
Correct |
122 ms |
10280 KB |
Output is correct |
40 |
Correct |
120 ms |
10440 KB |
Output is correct |
41 |
Correct |
134 ms |
10252 KB |
Output is correct |
42 |
Correct |
146 ms |
10340 KB |
Output is correct |
43 |
Correct |
69 ms |
10568 KB |
Output is correct |
44 |
Correct |
49 ms |
10372 KB |
Output is correct |
45 |
Correct |
123 ms |
13904 KB |
Output is correct |
46 |
Correct |
90 ms |
17552 KB |
Output is correct |
47 |
Correct |
160 ms |
15764 KB |
Output is correct |
48 |
Correct |
246 ms |
30148 KB |
Output is correct |
49 |
Correct |
238 ms |
29080 KB |
Output is correct |
50 |
Correct |
167 ms |
18392 KB |
Output is correct |
51 |
Correct |
152 ms |
18208 KB |
Output is correct |
52 |
Correct |
191 ms |
18244 KB |
Output is correct |
53 |
Correct |
216 ms |
17172 KB |
Output is correct |
54 |
Correct |
157 ms |
17096 KB |
Output is correct |
55 |
Correct |
11 ms |
5612 KB |
Output is correct |
56 |
Correct |
12 ms |
5580 KB |
Output is correct |
57 |
Correct |
79 ms |
11908 KB |
Output is correct |
58 |
Correct |
32 ms |
11608 KB |
Output is correct |
59 |
Correct |
92 ms |
19636 KB |
Output is correct |
60 |
Correct |
456 ms |
35800 KB |
Output is correct |
61 |
Correct |
199 ms |
18316 KB |
Output is correct |
62 |
Correct |
194 ms |
22352 KB |
Output is correct |
63 |
Correct |
266 ms |
22144 KB |
Output is correct |
64 |
Correct |
557 ms |
20076 KB |
Output is correct |
65 |
Correct |
211 ms |
18148 KB |
Output is correct |
66 |
Correct |
346 ms |
32776 KB |
Output is correct |
67 |
Correct |
113 ms |
22392 KB |
Output is correct |
68 |
Correct |
276 ms |
22300 KB |
Output is correct |
69 |
Correct |
250 ms |
22464 KB |
Output is correct |
70 |
Correct |
250 ms |
22024 KB |
Output is correct |