#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int MAXA = 1000010;
const int INF = 1e8;
int n, k;
vector<int> ar[MAXN];
vector<int> wg[MAXN];
int tam;
int sub[MAXN];
int marc[MAXN];
int minProf[MAXA];
int ans;
void centDecomp(int cur);
void preCalc(int cur, int pai);
int getCent(int cur, int pai);
void getAns(int cur, int pai, int prof, int dist);
void addProf(int cur, int pai, int prof, int dist);
void removeProf(int cur, int pai, int dist);
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0; i < n - 1; i++){
ar[H[i][0]].push_back(H[i][1]);
ar[H[i][1]].push_back(H[i][0]);
wg[H[i][0]].push_back(L[i]);
wg[H[i][1]].push_back(L[i]);
}
for(int i = 0; i <= k; i++)
minProf[i] = INF;
ans = INF;
centDecomp(1);
if(ans >= INF) ans = -1;
return ans;
}
void centDecomp(int cur){
tam = 0;
preCalc(cur, cur);
int cent = getCent(cur, cur);
marc[cent] = 1;
minProf[0] = 0;
for(int i = 0; i < (int)ar[cent].size(); i++){
int viz = ar[cent][i];
if(marc[viz]) continue;
getAns(viz, cent, 1, wg[cent][i]);
addProf(viz, cent, 1, wg[cent][i]);
}
removeProf(cent, cent, 0);
for(int i = 0; i < (int)ar[cent].size(); i++){
int viz = ar[cent][i];
if(marc[viz]) continue;
centDecomp(viz);
}
}
void preCalc(int cur, int pai){
tam++;
sub[cur] = 1;
for(int i = 0; i < (int)ar[cur].size(); i++){
int viz = ar[cur][i];
if(viz == pai || marc[viz]) continue;
preCalc(viz, cur);
sub[cur] += sub[viz];
}
}
int getCent(int cur, int pai){
for(int i = 0; i < (int)ar[cur].size(); i++){
int viz = ar[cur][i];
if(viz == pai || marc[viz] || sub[viz] < tam / 2) continue;
return getCent(viz, cur);
}
return cur;
}
void getAns(int cur, int pai, int prof, int dist){
dist = min(dist, INF);
if(dist <= k) ans = min(ans, minProf[k - dist] + prof);
for(int i = 0; i < (int)ar[cur].size(); i++){
int viz = ar[cur][i];
if(viz == pai || marc[viz]) continue;
getAns(viz, cur, prof + 1, dist + wg[cur][i]);
}
}
void addProf(int cur, int pai, int prof, int dist){
dist = min(dist, INF);
if(dist <= k) minProf[dist] = min(minProf[dist], prof);
for(int i = 0; i < (int)ar[cur].size(); i++){
int viz = ar[cur][i];
if(viz == pai || marc[viz]) continue;
addProf(viz, cur, prof + 1, dist + wg[cur][i]);
}
}
void removeProf(int cur, int pai, int dist){
dist = min(dist, INF);
if(dist <= k) minProf[dist] = INF;
for(int i = 0; i < (int)ar[cur].size(); i++){
int viz = ar[cur][i];
if(viz == pai || marc[viz]) continue;
removeProf(viz, cur, dist + wg[cur][i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
8 ms |
9776 KB |
Output is correct |
5 |
Correct |
8 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
8 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
8 ms |
9708 KB |
Output is correct |
17 |
Correct |
8 ms |
9708 KB |
Output is correct |
18 |
Correct |
8 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
8 ms |
9776 KB |
Output is correct |
5 |
Correct |
8 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
8 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
8 ms |
9708 KB |
Output is correct |
17 |
Correct |
8 ms |
9708 KB |
Output is correct |
18 |
Correct |
8 ms |
9708 KB |
Output is correct |
19 |
Correct |
8 ms |
9708 KB |
Output is correct |
20 |
Correct |
8 ms |
9708 KB |
Output is correct |
21 |
Correct |
9 ms |
9964 KB |
Output is correct |
22 |
Correct |
11 ms |
13420 KB |
Output is correct |
23 |
Correct |
13 ms |
12780 KB |
Output is correct |
24 |
Correct |
11 ms |
13164 KB |
Output is correct |
25 |
Correct |
11 ms |
13164 KB |
Output is correct |
26 |
Correct |
10 ms |
11116 KB |
Output is correct |
27 |
Correct |
11 ms |
13036 KB |
Output is correct |
28 |
Correct |
11 ms |
10604 KB |
Output is correct |
29 |
Correct |
9 ms |
11116 KB |
Output is correct |
30 |
Correct |
10 ms |
11244 KB |
Output is correct |
31 |
Correct |
10 ms |
12396 KB |
Output is correct |
32 |
Correct |
10 ms |
12652 KB |
Output is correct |
33 |
Correct |
11 ms |
12908 KB |
Output is correct |
34 |
Correct |
10 ms |
12156 KB |
Output is correct |
35 |
Correct |
12 ms |
13036 KB |
Output is correct |
36 |
Correct |
11 ms |
13420 KB |
Output is correct |
37 |
Correct |
11 ms |
12908 KB |
Output is correct |
38 |
Correct |
10 ms |
11884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
8 ms |
9776 KB |
Output is correct |
5 |
Correct |
8 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
8 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
8 ms |
9708 KB |
Output is correct |
17 |
Correct |
8 ms |
9708 KB |
Output is correct |
18 |
Correct |
8 ms |
9708 KB |
Output is correct |
19 |
Correct |
315 ms |
19436 KB |
Output is correct |
20 |
Correct |
379 ms |
19472 KB |
Output is correct |
21 |
Correct |
333 ms |
19436 KB |
Output is correct |
22 |
Correct |
304 ms |
19820 KB |
Output is correct |
23 |
Correct |
203 ms |
19436 KB |
Output is correct |
24 |
Correct |
129 ms |
20076 KB |
Output is correct |
25 |
Correct |
311 ms |
26348 KB |
Output is correct |
26 |
Correct |
140 ms |
27372 KB |
Output is correct |
27 |
Correct |
425 ms |
30188 KB |
Output is correct |
28 |
Correct |
904 ms |
43376 KB |
Output is correct |
29 |
Correct |
871 ms |
38228 KB |
Output is correct |
30 |
Correct |
430 ms |
30188 KB |
Output is correct |
31 |
Correct |
386 ms |
29932 KB |
Output is correct |
32 |
Correct |
544 ms |
29932 KB |
Output is correct |
33 |
Correct |
637 ms |
29016 KB |
Output is correct |
34 |
Correct |
623 ms |
28908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9728 KB |
Output is correct |
4 |
Correct |
8 ms |
9776 KB |
Output is correct |
5 |
Correct |
8 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
8 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
8 ms |
9708 KB |
Output is correct |
17 |
Correct |
8 ms |
9708 KB |
Output is correct |
18 |
Correct |
8 ms |
9708 KB |
Output is correct |
19 |
Correct |
8 ms |
9708 KB |
Output is correct |
20 |
Correct |
8 ms |
9708 KB |
Output is correct |
21 |
Correct |
9 ms |
9964 KB |
Output is correct |
22 |
Correct |
11 ms |
13420 KB |
Output is correct |
23 |
Correct |
13 ms |
12780 KB |
Output is correct |
24 |
Correct |
11 ms |
13164 KB |
Output is correct |
25 |
Correct |
11 ms |
13164 KB |
Output is correct |
26 |
Correct |
10 ms |
11116 KB |
Output is correct |
27 |
Correct |
11 ms |
13036 KB |
Output is correct |
28 |
Correct |
11 ms |
10604 KB |
Output is correct |
29 |
Correct |
9 ms |
11116 KB |
Output is correct |
30 |
Correct |
10 ms |
11244 KB |
Output is correct |
31 |
Correct |
10 ms |
12396 KB |
Output is correct |
32 |
Correct |
10 ms |
12652 KB |
Output is correct |
33 |
Correct |
11 ms |
12908 KB |
Output is correct |
34 |
Correct |
10 ms |
12156 KB |
Output is correct |
35 |
Correct |
12 ms |
13036 KB |
Output is correct |
36 |
Correct |
11 ms |
13420 KB |
Output is correct |
37 |
Correct |
11 ms |
12908 KB |
Output is correct |
38 |
Correct |
10 ms |
11884 KB |
Output is correct |
39 |
Correct |
315 ms |
19436 KB |
Output is correct |
40 |
Correct |
379 ms |
19472 KB |
Output is correct |
41 |
Correct |
333 ms |
19436 KB |
Output is correct |
42 |
Correct |
304 ms |
19820 KB |
Output is correct |
43 |
Correct |
203 ms |
19436 KB |
Output is correct |
44 |
Correct |
129 ms |
20076 KB |
Output is correct |
45 |
Correct |
311 ms |
26348 KB |
Output is correct |
46 |
Correct |
140 ms |
27372 KB |
Output is correct |
47 |
Correct |
425 ms |
30188 KB |
Output is correct |
48 |
Correct |
904 ms |
43376 KB |
Output is correct |
49 |
Correct |
871 ms |
38228 KB |
Output is correct |
50 |
Correct |
430 ms |
30188 KB |
Output is correct |
51 |
Correct |
386 ms |
29932 KB |
Output is correct |
52 |
Correct |
544 ms |
29932 KB |
Output is correct |
53 |
Correct |
637 ms |
29016 KB |
Output is correct |
54 |
Correct |
623 ms |
28908 KB |
Output is correct |
55 |
Correct |
21 ms |
10732 KB |
Output is correct |
56 |
Correct |
21 ms |
10732 KB |
Output is correct |
57 |
Correct |
129 ms |
19436 KB |
Output is correct |
58 |
Correct |
57 ms |
19804 KB |
Output is correct |
59 |
Correct |
157 ms |
27820 KB |
Output is correct |
60 |
Correct |
961 ms |
40300 KB |
Output is correct |
61 |
Correct |
402 ms |
29676 KB |
Output is correct |
62 |
Correct |
412 ms |
33176 KB |
Output is correct |
63 |
Correct |
542 ms |
33388 KB |
Output is correct |
64 |
Correct |
1112 ms |
30432 KB |
Output is correct |
65 |
Correct |
809 ms |
28652 KB |
Output is correct |
66 |
Correct |
975 ms |
45292 KB |
Output is correct |
67 |
Correct |
235 ms |
33492 KB |
Output is correct |
68 |
Correct |
588 ms |
33388 KB |
Output is correct |
69 |
Correct |
616 ms |
33532 KB |
Output is correct |
70 |
Correct |
542 ms |
32620 KB |
Output is correct |