#include "race.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define nmax 200008
#define kmax 1000008
const int inf = 1e9;
using namespace std;
typedef pair<int, int> pii;
int n, k, lim, ans = inf, sz[nmax], f[kmax];
bool kt[nmax];
vector<pii>l[nmax];
int dfs(int x, int pre){
sz[x] = 1;
for (pii it : l[x]){
if (it.fi == pre || kt[it.fi])
continue;
sz[x] += dfs(it.fi, x);
}
return sz[x];
}
int centroid(int rt, int x, int pre){
for (pii it : l[x]){
if (it.fi == pre || kt[it.fi])
continue;
if (sz[it.fi] * 2 > sz[rt])
return centroid(rt, it.fi, x);
}
return x;
}
void calc(int x, int pre, int dis, int d, int flag){
if (dis > k)
return;
switch(flag){
case 0:
ans = min(ans, f[k - dis] + d);
break;
case 1:
f[dis] = min(f[dis], d);
break;
default:
f[dis] = inf;
break;
}
for (pii it : l[x]){
if (it.fi == pre || kt[it.fi])
continue;
calc(it.fi, x, dis + it.se, d + 1, flag);
}
}
void centroid(int x){
dfs(x, x);
x = centroid(x, x, x);
f[0] = 0;
for (pii it : l[x])
if (!kt[it.fi]){
calc(it.fi, x, it.se, 1, 0);
calc(it.fi, x, it.se, 1, 1);
}
for (pii it : l[x])
if (!kt[it.fi])
calc(it.fi, x, it.se, 1, 2);
kt[x] = 1;
f[0] = inf;
for (pii it : l[x])
if (!kt[it.fi])
centroid(it.fi);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for (int i = 0; i < n - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
l[u].push_back({v, w});
l[v].push_back({u, w});
}
memset(kt, false, sizeof(kt));
fill(f, f + k + 1, inf);
centroid(1);
if (ans >= n)
ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
2 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
2 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
6 ms |
8148 KB |
Output is correct |
24 |
Correct |
5 ms |
8660 KB |
Output is correct |
25 |
Correct |
5 ms |
8532 KB |
Output is correct |
26 |
Correct |
4 ms |
6484 KB |
Output is correct |
27 |
Correct |
5 ms |
8352 KB |
Output is correct |
28 |
Correct |
4 ms |
6056 KB |
Output is correct |
29 |
Correct |
4 ms |
6484 KB |
Output is correct |
30 |
Correct |
5 ms |
6612 KB |
Output is correct |
31 |
Correct |
4 ms |
7764 KB |
Output is correct |
32 |
Correct |
5 ms |
8020 KB |
Output is correct |
33 |
Correct |
5 ms |
8232 KB |
Output is correct |
34 |
Correct |
5 ms |
7588 KB |
Output is correct |
35 |
Correct |
6 ms |
8444 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8404 KB |
Output is correct |
38 |
Correct |
4 ms |
7204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
2 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
125 ms |
10572 KB |
Output is correct |
20 |
Correct |
121 ms |
10432 KB |
Output is correct |
21 |
Correct |
153 ms |
10444 KB |
Output is correct |
22 |
Correct |
114 ms |
10424 KB |
Output is correct |
23 |
Correct |
71 ms |
10684 KB |
Output is correct |
24 |
Correct |
48 ms |
10580 KB |
Output is correct |
25 |
Correct |
117 ms |
15540 KB |
Output is correct |
26 |
Correct |
87 ms |
16100 KB |
Output is correct |
27 |
Correct |
155 ms |
15824 KB |
Output is correct |
28 |
Correct |
239 ms |
26292 KB |
Output is correct |
29 |
Correct |
247 ms |
21964 KB |
Output is correct |
30 |
Correct |
160 ms |
15764 KB |
Output is correct |
31 |
Correct |
165 ms |
15868 KB |
Output is correct |
32 |
Correct |
175 ms |
15884 KB |
Output is correct |
33 |
Correct |
208 ms |
14672 KB |
Output is correct |
34 |
Correct |
175 ms |
14660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
2 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
6 ms |
8148 KB |
Output is correct |
24 |
Correct |
5 ms |
8660 KB |
Output is correct |
25 |
Correct |
5 ms |
8532 KB |
Output is correct |
26 |
Correct |
4 ms |
6484 KB |
Output is correct |
27 |
Correct |
5 ms |
8352 KB |
Output is correct |
28 |
Correct |
4 ms |
6056 KB |
Output is correct |
29 |
Correct |
4 ms |
6484 KB |
Output is correct |
30 |
Correct |
5 ms |
6612 KB |
Output is correct |
31 |
Correct |
4 ms |
7764 KB |
Output is correct |
32 |
Correct |
5 ms |
8020 KB |
Output is correct |
33 |
Correct |
5 ms |
8232 KB |
Output is correct |
34 |
Correct |
5 ms |
7588 KB |
Output is correct |
35 |
Correct |
6 ms |
8444 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8404 KB |
Output is correct |
38 |
Correct |
4 ms |
7204 KB |
Output is correct |
39 |
Correct |
125 ms |
10572 KB |
Output is correct |
40 |
Correct |
121 ms |
10432 KB |
Output is correct |
41 |
Correct |
153 ms |
10444 KB |
Output is correct |
42 |
Correct |
114 ms |
10424 KB |
Output is correct |
43 |
Correct |
71 ms |
10684 KB |
Output is correct |
44 |
Correct |
48 ms |
10580 KB |
Output is correct |
45 |
Correct |
117 ms |
15540 KB |
Output is correct |
46 |
Correct |
87 ms |
16100 KB |
Output is correct |
47 |
Correct |
155 ms |
15824 KB |
Output is correct |
48 |
Correct |
239 ms |
26292 KB |
Output is correct |
49 |
Correct |
247 ms |
21964 KB |
Output is correct |
50 |
Correct |
160 ms |
15764 KB |
Output is correct |
51 |
Correct |
165 ms |
15868 KB |
Output is correct |
52 |
Correct |
175 ms |
15884 KB |
Output is correct |
53 |
Correct |
208 ms |
14672 KB |
Output is correct |
54 |
Correct |
175 ms |
14660 KB |
Output is correct |
55 |
Correct |
11 ms |
5800 KB |
Output is correct |
56 |
Correct |
12 ms |
5864 KB |
Output is correct |
57 |
Correct |
78 ms |
12020 KB |
Output is correct |
58 |
Correct |
33 ms |
11664 KB |
Output is correct |
59 |
Correct |
93 ms |
18316 KB |
Output is correct |
60 |
Correct |
485 ms |
29228 KB |
Output is correct |
61 |
Correct |
177 ms |
18908 KB |
Output is correct |
62 |
Correct |
206 ms |
22748 KB |
Output is correct |
63 |
Correct |
263 ms |
22752 KB |
Output is correct |
64 |
Correct |
542 ms |
21024 KB |
Output is correct |
65 |
Correct |
197 ms |
19668 KB |
Output is correct |
66 |
Correct |
369 ms |
32280 KB |
Output is correct |
67 |
Correct |
110 ms |
23344 KB |
Output is correct |
68 |
Correct |
255 ms |
23508 KB |
Output is correct |
69 |
Correct |
255 ms |
23660 KB |
Output is correct |
70 |
Correct |
238 ms |
22916 KB |
Output is correct |