#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using vi = vector <int>;
using pii = pair <int, int>;
#define pb push_back
#define F first
#define S second
#define N 200005
int n, k, ans = N, sz[N], mx[N], centroid;
map <int, int> dist;
bool used[N];
vector <pii> edg[N];
void get_size(int u, int p){
sz[u] = 1;
mx[u] = 0;
for (pii v : edg[u])
if (v.F != p && !used[v.F]){
get_size(v.F, u);
mx[u] = max(mx[u], sz[v.F]);
sz[u] += sz[v.F];
}
mx[u] = max(mx[u], n - sz[u]);
}
void get_centroid(int u, int p){
for (pii v : edg[u])
if (v.F != p && !used[v.F])
get_centroid(v.F, u);
if (mx[u] <= (n + 1) / 2) centroid = u;
}
void calc(int u, int p, int lenght, int distance){
if(distance > k) return;
for (pii v : edg[u])
if (v.F != p && !used[v.F])
calc(v.F, u, lenght + 1, distance + v.S);
if (dist.count(k - distance))
ans = min (ans, lenght + dist[k - distance]);
}
void add(int u, int p, int lenght, int distance){
if(distance > k) return;
for (pii v : edg[u])
if (v.F != p && !used[v.F])
add(v.F, u, lenght + 1, distance + v.S);
if (!dist.count(distance)) dist[distance] = lenght;
else dist[distance] = min(dist[distance], lenght);
}
void solve(int u){
dist.clear();
dist[0] = 0;
get_size(u, -1);
n = sz[u];
get_centroid(u, -1);
used[centroid] = 1;
for (pii v : edg[centroid])
if (!used[v.F]){
calc(v.F, centroid, 1, v.S);
add(v.F, centroid, 1, v.S);
}
for (pii v : edg[centroid])
if (!used[v.F])
solve(v.F);
}
int best_path(int s, int K, int H[][2], int L[]){
n = s;
k = K;
for (int i = 0;i < n - 1;i++){
edg[H[i][0]].pb({H[i][1], L[i]});
edg[H[i][1]].pb({H[i][0], L[i]});
}
solve(0);
return ans == N ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5096 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5096 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
13 ms |
5120 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5120 KB |
Output is correct |
29 |
Correct |
11 ms |
5120 KB |
Output is correct |
30 |
Correct |
11 ms |
5120 KB |
Output is correct |
31 |
Correct |
10 ms |
5120 KB |
Output is correct |
32 |
Correct |
10 ms |
5120 KB |
Output is correct |
33 |
Correct |
10 ms |
5120 KB |
Output is correct |
34 |
Correct |
10 ms |
5120 KB |
Output is correct |
35 |
Correct |
9 ms |
5120 KB |
Output is correct |
36 |
Correct |
10 ms |
5120 KB |
Output is correct |
37 |
Correct |
10 ms |
5120 KB |
Output is correct |
38 |
Correct |
11 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5096 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
564 ms |
11016 KB |
Output is correct |
20 |
Correct |
466 ms |
10872 KB |
Output is correct |
21 |
Correct |
609 ms |
11128 KB |
Output is correct |
22 |
Correct |
624 ms |
11000 KB |
Output is correct |
23 |
Correct |
258 ms |
11256 KB |
Output is correct |
24 |
Correct |
166 ms |
11000 KB |
Output is correct |
25 |
Correct |
425 ms |
13816 KB |
Output is correct |
26 |
Correct |
225 ms |
16632 KB |
Output is correct |
27 |
Correct |
371 ms |
16944 KB |
Output is correct |
28 |
Correct |
810 ms |
28152 KB |
Output is correct |
29 |
Correct |
922 ms |
27088 KB |
Output is correct |
30 |
Correct |
346 ms |
16760 KB |
Output is correct |
31 |
Correct |
367 ms |
16860 KB |
Output is correct |
32 |
Correct |
398 ms |
16760 KB |
Output is correct |
33 |
Correct |
1172 ms |
15760 KB |
Output is correct |
34 |
Correct |
799 ms |
15736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5096 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
13 ms |
5120 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5120 KB |
Output is correct |
29 |
Correct |
11 ms |
5120 KB |
Output is correct |
30 |
Correct |
11 ms |
5120 KB |
Output is correct |
31 |
Correct |
10 ms |
5120 KB |
Output is correct |
32 |
Correct |
10 ms |
5120 KB |
Output is correct |
33 |
Correct |
10 ms |
5120 KB |
Output is correct |
34 |
Correct |
10 ms |
5120 KB |
Output is correct |
35 |
Correct |
9 ms |
5120 KB |
Output is correct |
36 |
Correct |
10 ms |
5120 KB |
Output is correct |
37 |
Correct |
10 ms |
5120 KB |
Output is correct |
38 |
Correct |
11 ms |
5248 KB |
Output is correct |
39 |
Correct |
564 ms |
11016 KB |
Output is correct |
40 |
Correct |
466 ms |
10872 KB |
Output is correct |
41 |
Correct |
609 ms |
11128 KB |
Output is correct |
42 |
Correct |
624 ms |
11000 KB |
Output is correct |
43 |
Correct |
258 ms |
11256 KB |
Output is correct |
44 |
Correct |
166 ms |
11000 KB |
Output is correct |
45 |
Correct |
425 ms |
13816 KB |
Output is correct |
46 |
Correct |
225 ms |
16632 KB |
Output is correct |
47 |
Correct |
371 ms |
16944 KB |
Output is correct |
48 |
Correct |
810 ms |
28152 KB |
Output is correct |
49 |
Correct |
922 ms |
27088 KB |
Output is correct |
50 |
Correct |
346 ms |
16760 KB |
Output is correct |
51 |
Correct |
367 ms |
16860 KB |
Output is correct |
52 |
Correct |
398 ms |
16760 KB |
Output is correct |
53 |
Correct |
1172 ms |
15760 KB |
Output is correct |
54 |
Correct |
799 ms |
15736 KB |
Output is correct |
55 |
Correct |
52 ms |
5920 KB |
Output is correct |
56 |
Correct |
37 ms |
5760 KB |
Output is correct |
57 |
Correct |
282 ms |
11284 KB |
Output is correct |
58 |
Correct |
79 ms |
11116 KB |
Output is correct |
59 |
Correct |
648 ms |
20208 KB |
Output is correct |
60 |
Correct |
2768 ms |
36296 KB |
Output is correct |
61 |
Correct |
538 ms |
17116 KB |
Output is correct |
62 |
Correct |
632 ms |
17024 KB |
Output is correct |
63 |
Correct |
780 ms |
17144 KB |
Output is correct |
64 |
Execution timed out |
3037 ms |
21368 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |