#include <bits/stdc++.h>
#define maxn 200010
using namespace std;
int n, k, ans, minSz, curCn, minDistance[10*maxn];
bool isC[maxn];
vector<vector<pair<int, int> > > gr;
vector<int> subSz;
int findCentroid(int x, int p, int dist, int price, int n){
subSz[x] = 1;
int tmpM = 0;
for(auto pr : gr[x]){
int i = pr.first,c = pr.second;
if(isC[i] or i == p){
continue;
}
subSz[x] += findCentroid(i, x, dist+1, price+c, n);
tmpM = max(subSz[i], tmpM);
}
tmpM = max(tmpM, n-subSz[x]);
if(minSz > tmpM){
minSz = tmpM;
curCn = x;
}
return subSz[x];
}
vector<int> chPrices;
void dfs(int x, int p, int dist, int price, bool isAdding){
if(price>=maxn*10){
return;
}
if(isAdding and price < maxn*10){
minDistance[price] = min(minDistance[price],dist);
chPrices.push_back(price);
} else {
if(k - price >= 0){
ans = min(ans, dist + minDistance[k-price]);
}
}
for(auto tmp:gr[x]){
int i = tmp.first, j = tmp.second;
if(isC[i] or i ==p){
continue;
}
dfs(i, x, dist+1, price + j, isAdding);
}
}
void findR(int x, int n){
minSz = 1e9;
curCn = x;
findCentroid(x, -1, 0, 0, n);
isC[curCn] = 1;
for(auto tmp:gr[curCn]){
int i = tmp.first, j = tmp.second;
if(!isC[i]){
dfs(i, curCn, 1, j, 0);
dfs(i, curCn, 1, j, 1);
}
}
unique(chPrices.begin(), chPrices.end());
for(auto x : chPrices){
minDistance[x] = 1e9;
}
minDistance[0] = 0;
chPrices.clear();
for(auto tmp:gr[curCn]){
int i = tmp.first, j = tmp.second;
if(!isC[i]){
findR(i, subSz[i]);
}
}
}
int best_path(int n, int k1, int h[][2], int l[]){
k = k1;
gr.resize(n, vector<pair<int, int> >());
for(int i = 0; i < n-1; ++i){
int x, y, w;
x = h[i][0];
y = h[i][1];
w = l[i];
gr[x].push_back({y, w});
gr[y].push_back({x, w});
}
subSz.resize(n, 0);
for(int i = 1; i < maxn*10; ++i){
minDistance[i] = 1e9;
}
ans = 1e9;
findR(0, n);
if(ans != 1e9){
return ans;
} else {
return -1;
}
}
Compilation message
race.cpp: In function 'void findR(int, int)':
race.cpp:72:22: warning: unused variable 'j' [-Wunused-variable]
72 | int i = tmp.first, j = tmp.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8192 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
7 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8172 KB |
Output is correct |
11 |
Correct |
6 ms |
8172 KB |
Output is correct |
12 |
Correct |
6 ms |
8172 KB |
Output is correct |
13 |
Correct |
6 ms |
8172 KB |
Output is correct |
14 |
Correct |
5 ms |
8172 KB |
Output is correct |
15 |
Correct |
6 ms |
8172 KB |
Output is correct |
16 |
Correct |
5 ms |
8172 KB |
Output is correct |
17 |
Correct |
5 ms |
8172 KB |
Output is correct |
18 |
Correct |
5 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8192 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
7 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8172 KB |
Output is correct |
11 |
Correct |
6 ms |
8172 KB |
Output is correct |
12 |
Correct |
6 ms |
8172 KB |
Output is correct |
13 |
Correct |
6 ms |
8172 KB |
Output is correct |
14 |
Correct |
5 ms |
8172 KB |
Output is correct |
15 |
Correct |
6 ms |
8172 KB |
Output is correct |
16 |
Correct |
5 ms |
8172 KB |
Output is correct |
17 |
Correct |
5 ms |
8172 KB |
Output is correct |
18 |
Correct |
5 ms |
8172 KB |
Output is correct |
19 |
Correct |
5 ms |
8172 KB |
Output is correct |
20 |
Correct |
5 ms |
8172 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
9 ms |
8172 KB |
Output is correct |
23 |
Correct |
6 ms |
8172 KB |
Output is correct |
24 |
Correct |
8 ms |
8300 KB |
Output is correct |
25 |
Correct |
6 ms |
8300 KB |
Output is correct |
26 |
Correct |
6 ms |
8300 KB |
Output is correct |
27 |
Correct |
6 ms |
8172 KB |
Output is correct |
28 |
Correct |
7 ms |
8300 KB |
Output is correct |
29 |
Correct |
7 ms |
8300 KB |
Output is correct |
30 |
Correct |
7 ms |
8300 KB |
Output is correct |
31 |
Correct |
7 ms |
8300 KB |
Output is correct |
32 |
Correct |
7 ms |
8300 KB |
Output is correct |
33 |
Correct |
7 ms |
8300 KB |
Output is correct |
34 |
Correct |
9 ms |
8300 KB |
Output is correct |
35 |
Correct |
6 ms |
8300 KB |
Output is correct |
36 |
Correct |
7 ms |
8300 KB |
Output is correct |
37 |
Correct |
6 ms |
8300 KB |
Output is correct |
38 |
Correct |
6 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8192 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
7 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8172 KB |
Output is correct |
11 |
Correct |
6 ms |
8172 KB |
Output is correct |
12 |
Correct |
6 ms |
8172 KB |
Output is correct |
13 |
Correct |
6 ms |
8172 KB |
Output is correct |
14 |
Correct |
5 ms |
8172 KB |
Output is correct |
15 |
Correct |
6 ms |
8172 KB |
Output is correct |
16 |
Correct |
5 ms |
8172 KB |
Output is correct |
17 |
Correct |
5 ms |
8172 KB |
Output is correct |
18 |
Correct |
5 ms |
8172 KB |
Output is correct |
19 |
Correct |
251 ms |
16364 KB |
Output is correct |
20 |
Correct |
230 ms |
16364 KB |
Output is correct |
21 |
Correct |
210 ms |
16364 KB |
Output is correct |
22 |
Correct |
214 ms |
16616 KB |
Output is correct |
23 |
Correct |
191 ms |
16620 KB |
Output is correct |
24 |
Correct |
85 ms |
16492 KB |
Output is correct |
25 |
Correct |
229 ms |
20848 KB |
Output is correct |
26 |
Correct |
126 ms |
25328 KB |
Output is correct |
27 |
Correct |
259 ms |
27628 KB |
Output is correct |
28 |
Correct |
523 ms |
44772 KB |
Output is correct |
29 |
Correct |
539 ms |
43408 KB |
Output is correct |
30 |
Correct |
238 ms |
27628 KB |
Output is correct |
31 |
Correct |
243 ms |
27628 KB |
Output is correct |
32 |
Correct |
333 ms |
27788 KB |
Output is correct |
33 |
Correct |
380 ms |
26468 KB |
Output is correct |
34 |
Correct |
297 ms |
26640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8192 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
7 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8172 KB |
Output is correct |
11 |
Correct |
6 ms |
8172 KB |
Output is correct |
12 |
Correct |
6 ms |
8172 KB |
Output is correct |
13 |
Correct |
6 ms |
8172 KB |
Output is correct |
14 |
Correct |
5 ms |
8172 KB |
Output is correct |
15 |
Correct |
6 ms |
8172 KB |
Output is correct |
16 |
Correct |
5 ms |
8172 KB |
Output is correct |
17 |
Correct |
5 ms |
8172 KB |
Output is correct |
18 |
Correct |
5 ms |
8172 KB |
Output is correct |
19 |
Correct |
5 ms |
8172 KB |
Output is correct |
20 |
Correct |
5 ms |
8172 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
9 ms |
8172 KB |
Output is correct |
23 |
Correct |
6 ms |
8172 KB |
Output is correct |
24 |
Correct |
8 ms |
8300 KB |
Output is correct |
25 |
Correct |
6 ms |
8300 KB |
Output is correct |
26 |
Correct |
6 ms |
8300 KB |
Output is correct |
27 |
Correct |
6 ms |
8172 KB |
Output is correct |
28 |
Correct |
7 ms |
8300 KB |
Output is correct |
29 |
Correct |
7 ms |
8300 KB |
Output is correct |
30 |
Correct |
7 ms |
8300 KB |
Output is correct |
31 |
Correct |
7 ms |
8300 KB |
Output is correct |
32 |
Correct |
7 ms |
8300 KB |
Output is correct |
33 |
Correct |
7 ms |
8300 KB |
Output is correct |
34 |
Correct |
9 ms |
8300 KB |
Output is correct |
35 |
Correct |
6 ms |
8300 KB |
Output is correct |
36 |
Correct |
7 ms |
8300 KB |
Output is correct |
37 |
Correct |
6 ms |
8300 KB |
Output is correct |
38 |
Correct |
6 ms |
8320 KB |
Output is correct |
39 |
Correct |
251 ms |
16364 KB |
Output is correct |
40 |
Correct |
230 ms |
16364 KB |
Output is correct |
41 |
Correct |
210 ms |
16364 KB |
Output is correct |
42 |
Correct |
214 ms |
16616 KB |
Output is correct |
43 |
Correct |
191 ms |
16620 KB |
Output is correct |
44 |
Correct |
85 ms |
16492 KB |
Output is correct |
45 |
Correct |
229 ms |
20848 KB |
Output is correct |
46 |
Correct |
126 ms |
25328 KB |
Output is correct |
47 |
Correct |
259 ms |
27628 KB |
Output is correct |
48 |
Correct |
523 ms |
44772 KB |
Output is correct |
49 |
Correct |
539 ms |
43408 KB |
Output is correct |
50 |
Correct |
238 ms |
27628 KB |
Output is correct |
51 |
Correct |
243 ms |
27628 KB |
Output is correct |
52 |
Correct |
333 ms |
27788 KB |
Output is correct |
53 |
Correct |
380 ms |
26468 KB |
Output is correct |
54 |
Correct |
297 ms |
26640 KB |
Output is correct |
55 |
Correct |
15 ms |
9196 KB |
Output is correct |
56 |
Correct |
16 ms |
9196 KB |
Output is correct |
57 |
Correct |
107 ms |
18028 KB |
Output is correct |
58 |
Correct |
49 ms |
18016 KB |
Output is correct |
59 |
Correct |
124 ms |
26608 KB |
Output is correct |
60 |
Correct |
509 ms |
44232 KB |
Output is correct |
61 |
Correct |
252 ms |
27780 KB |
Output is correct |
62 |
Correct |
247 ms |
27624 KB |
Output is correct |
63 |
Correct |
329 ms |
27752 KB |
Output is correct |
64 |
Correct |
716 ms |
27980 KB |
Output is correct |
65 |
Correct |
388 ms |
27652 KB |
Output is correct |
66 |
Correct |
487 ms |
40044 KB |
Output is correct |
67 |
Correct |
148 ms |
29276 KB |
Output is correct |
68 |
Correct |
368 ms |
28388 KB |
Output is correct |
69 |
Correct |
353 ms |
28648 KB |
Output is correct |
70 |
Correct |
318 ms |
27620 KB |
Output is correct |