#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
vector <pair <int, int> > adj[200005];
map <int, int> global_path, local_path;
bool isCentroid[200005] = {false};
int subtr_size[200005], localCentroid, k, ans;
inline void SubTreeSize(int v, int p) {
subtr_size[v] = 1;
for(auto &it: adj[v])
if(it.F != p && !isCentroid[it.F]) {
SubTreeSize(it.F, v);
subtr_size[v] += subtr_size[it.F];
}
return;
}
inline void traverse(int v, int p, int w, int l) {
if(w > k) return;
if(w == k) ans = min(ans, l);
int temp = global_path[k - w];
if(temp) ans = min(ans, l + temp);
if(!local_path[w] || local_path[w] > l) local_path[w] = l;
for(auto &it: adj[v])
if(it.F != p && !isCentroid[it.F])
traverse(it.F, v, w + it.S, l + 1);
return;
}
inline void findCentroid(int v, int p, int size) {
bool flag = (size - subtr_size[v] <= size / 2);
for(auto &it: adj[v])
if(it.F != p && !isCentroid[it.F]) {
flag &= (subtr_size[it.F] <= size / 2);
findCentroid(it.F, v, size);
}
if(flag) localCentroid = v;
return;
}
inline void go(int v) {
SubTreeSize(v, -1);
findCentroid(v, -1, subtr_size[v]);
global_path.clear();
isCentroid[localCentroid] = true;
for(auto &it: adj[localCentroid])
if(!isCentroid[it.F]) {
local_path.clear();
traverse(it.F, localCentroid, it.S, 1);
for(auto &localit: local_path)
if(!global_path[localit.F] || global_path[localit.F] > localit.S)
global_path[localit.F] = localit.S;
}
for(auto &it: adj[localCentroid])
if(!isCentroid[it.F])
go(it.F);
return;
}
int best_path(int N, int K, int H[][2], int L[]) {
int i;
k = K;
for(i = 0; i < N - 1; i++) {
adj[H[i][0]].pb(mp(H[i][1], L[i]));
adj[H[i][1]].pb(mp(H[i][0], L[i]));
}
ans = 1e9;
go(0);
if(ans == 1e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 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 |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
4996 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 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 |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
4996 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
4992 KB |
Output is correct |
20 |
Correct |
6 ms |
4992 KB |
Output is correct |
21 |
Correct |
10 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
8 ms |
5120 KB |
Output is correct |
25 |
Correct |
9 ms |
5248 KB |
Output is correct |
26 |
Correct |
9 ms |
5120 KB |
Output is correct |
27 |
Correct |
8 ms |
5120 KB |
Output is correct |
28 |
Correct |
10 ms |
5120 KB |
Output is correct |
29 |
Correct |
11 ms |
5248 KB |
Output is correct |
30 |
Correct |
10 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5120 KB |
Output is correct |
32 |
Correct |
10 ms |
5120 KB |
Output is correct |
33 |
Correct |
13 ms |
5248 KB |
Output is correct |
34 |
Correct |
9 ms |
5120 KB |
Output is correct |
35 |
Correct |
9 ms |
5120 KB |
Output is correct |
36 |
Correct |
8 ms |
5120 KB |
Output is correct |
37 |
Correct |
9 ms |
5120 KB |
Output is correct |
38 |
Correct |
12 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 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 |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
4996 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
393 ms |
11684 KB |
Output is correct |
20 |
Correct |
424 ms |
11640 KB |
Output is correct |
21 |
Correct |
362 ms |
11768 KB |
Output is correct |
22 |
Correct |
387 ms |
11896 KB |
Output is correct |
23 |
Correct |
284 ms |
12152 KB |
Output is correct |
24 |
Correct |
154 ms |
11896 KB |
Output is correct |
25 |
Correct |
298 ms |
13788 KB |
Output is correct |
26 |
Correct |
139 ms |
15864 KB |
Output is correct |
27 |
Correct |
456 ms |
17864 KB |
Output is correct |
28 |
Correct |
712 ms |
26000 KB |
Output is correct |
29 |
Correct |
686 ms |
25204 KB |
Output is correct |
30 |
Correct |
428 ms |
17912 KB |
Output is correct |
31 |
Correct |
407 ms |
17784 KB |
Output is correct |
32 |
Correct |
435 ms |
17784 KB |
Output is correct |
33 |
Correct |
882 ms |
16540 KB |
Output is correct |
34 |
Correct |
546 ms |
16656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 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 |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
4996 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
4992 KB |
Output is correct |
20 |
Correct |
6 ms |
4992 KB |
Output is correct |
21 |
Correct |
10 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
8 ms |
5120 KB |
Output is correct |
25 |
Correct |
9 ms |
5248 KB |
Output is correct |
26 |
Correct |
9 ms |
5120 KB |
Output is correct |
27 |
Correct |
8 ms |
5120 KB |
Output is correct |
28 |
Correct |
10 ms |
5120 KB |
Output is correct |
29 |
Correct |
11 ms |
5248 KB |
Output is correct |
30 |
Correct |
10 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5120 KB |
Output is correct |
32 |
Correct |
10 ms |
5120 KB |
Output is correct |
33 |
Correct |
13 ms |
5248 KB |
Output is correct |
34 |
Correct |
9 ms |
5120 KB |
Output is correct |
35 |
Correct |
9 ms |
5120 KB |
Output is correct |
36 |
Correct |
8 ms |
5120 KB |
Output is correct |
37 |
Correct |
9 ms |
5120 KB |
Output is correct |
38 |
Correct |
12 ms |
5248 KB |
Output is correct |
39 |
Correct |
393 ms |
11684 KB |
Output is correct |
40 |
Correct |
424 ms |
11640 KB |
Output is correct |
41 |
Correct |
362 ms |
11768 KB |
Output is correct |
42 |
Correct |
387 ms |
11896 KB |
Output is correct |
43 |
Correct |
284 ms |
12152 KB |
Output is correct |
44 |
Correct |
154 ms |
11896 KB |
Output is correct |
45 |
Correct |
298 ms |
13788 KB |
Output is correct |
46 |
Correct |
139 ms |
15864 KB |
Output is correct |
47 |
Correct |
456 ms |
17864 KB |
Output is correct |
48 |
Correct |
712 ms |
26000 KB |
Output is correct |
49 |
Correct |
686 ms |
25204 KB |
Output is correct |
50 |
Correct |
428 ms |
17912 KB |
Output is correct |
51 |
Correct |
407 ms |
17784 KB |
Output is correct |
52 |
Correct |
435 ms |
17784 KB |
Output is correct |
53 |
Correct |
882 ms |
16540 KB |
Output is correct |
54 |
Correct |
546 ms |
16656 KB |
Output is correct |
55 |
Correct |
41 ms |
6016 KB |
Output is correct |
56 |
Correct |
29 ms |
5732 KB |
Output is correct |
57 |
Correct |
253 ms |
12024 KB |
Output is correct |
58 |
Correct |
61 ms |
11628 KB |
Output is correct |
59 |
Correct |
936 ms |
24972 KB |
Output is correct |
60 |
Execution timed out |
3106 ms |
46552 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |