#include "race.h"
#include <bits/stdc++.h>
#define ve vector
#define pb push_back
#define ar array
#define ll long long
using namespace std;
const int MAXN = 3e5;
const int MAXK = 2e6 + 1;
set<int> adj[MAXN];
map<ar<int, 2>, int> w;
int K;
int ANS = 1e9;
int BOOK = 1;
int sub[MAXN];
int A[MAXK];
int BOOK_K[MAXK];
bool processed[MAXN];
void find_size(int x, int p) {
sub[x] = 1;
for(int i : adj[x]) {
if(i != p && !processed[i]) {
find_size(i, x);
sub[x] += sub[i];
}
}
}
int find_centroid(int x, int p, int n) {
for(int i : adj[x]) {
if(sub[i] > n / 2 && i != p && !processed[i]) {
return find_centroid(i, x, n);
}
}
return x;
}
void dfs(int x, int p, int we, int num_edge, bool upd) {
if(we > K) {
return;
}
if(upd) {
if(BOOK_K[we] != BOOK) {
BOOK_K[we] = BOOK;
A[we] = num_edge;
} else {
A[we] = min(A[we], num_edge);
}
} else {
if(BOOK_K[K - we] == BOOK) {
ANS = min(ANS, num_edge + A[K - we]);
}
}
for(int i : adj[x]) {
if(i != p && we + w[{i, x}] <= K) {
dfs(i, x, we + w[{i, x}], num_edge + 1, upd);
}
}
}
void process_centroid(int x) {
processed[x] = 1;
BOOK++;
A[0] = 0;
BOOK_K[0] = BOOK;
for(int i : adj[x]) {
dfs(i, x, w[{i, x}], 1, false);
dfs(i, x, w[{i, x}], 1, true);
}
}
void decompo(int x, int p) {
find_size(x, p);
int centroid = find_centroid(x, p, sub[x]);
process_centroid(centroid);
for(int i : adj[centroid]) {
adj[i].erase(centroid);
decompo(i, x);
}
}
int best_path(int N, int Ked, int H[][2], int L[]) {
fill(A, A + MAXK, 0);
fill(BOOK_K, BOOK_K + MAXK, 0);
fill(processed, processed + MAXN, false);
for(int i = 0; i < N - 1; i++) {
int a, b;
a = H[i][0];
b = H[i][1];
adj[a].insert(b);
adj[b].insert(a);
w[{a, b}] = L[i];
w[{b, a}] = L[i];
}
K = Ked;
decompo(0, -1);
if(ANS != 1e9) {
return ANS;
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
34384 KB |
Output is correct |
2 |
Correct |
13 ms |
34440 KB |
Output is correct |
3 |
Correct |
13 ms |
34396 KB |
Output is correct |
4 |
Correct |
12 ms |
34384 KB |
Output is correct |
5 |
Correct |
16 ms |
34384 KB |
Output is correct |
6 |
Correct |
15 ms |
34552 KB |
Output is correct |
7 |
Correct |
14 ms |
32336 KB |
Output is correct |
8 |
Correct |
13 ms |
30748 KB |
Output is correct |
9 |
Correct |
14 ms |
30544 KB |
Output is correct |
10 |
Correct |
19 ms |
30768 KB |
Output is correct |
11 |
Correct |
16 ms |
30544 KB |
Output is correct |
12 |
Correct |
15 ms |
32336 KB |
Output is correct |
13 |
Correct |
15 ms |
32336 KB |
Output is correct |
14 |
Correct |
14 ms |
32336 KB |
Output is correct |
15 |
Correct |
14 ms |
30680 KB |
Output is correct |
16 |
Correct |
14 ms |
32336 KB |
Output is correct |
17 |
Correct |
18 ms |
30544 KB |
Output is correct |
18 |
Correct |
20 ms |
30544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
34384 KB |
Output is correct |
2 |
Correct |
13 ms |
34440 KB |
Output is correct |
3 |
Correct |
13 ms |
34396 KB |
Output is correct |
4 |
Correct |
12 ms |
34384 KB |
Output is correct |
5 |
Correct |
16 ms |
34384 KB |
Output is correct |
6 |
Correct |
15 ms |
34552 KB |
Output is correct |
7 |
Correct |
14 ms |
32336 KB |
Output is correct |
8 |
Correct |
13 ms |
30748 KB |
Output is correct |
9 |
Correct |
14 ms |
30544 KB |
Output is correct |
10 |
Correct |
19 ms |
30768 KB |
Output is correct |
11 |
Correct |
16 ms |
30544 KB |
Output is correct |
12 |
Correct |
15 ms |
32336 KB |
Output is correct |
13 |
Correct |
15 ms |
32336 KB |
Output is correct |
14 |
Correct |
14 ms |
32336 KB |
Output is correct |
15 |
Correct |
14 ms |
30680 KB |
Output is correct |
16 |
Correct |
14 ms |
32336 KB |
Output is correct |
17 |
Correct |
18 ms |
30544 KB |
Output is correct |
18 |
Correct |
20 ms |
30544 KB |
Output is correct |
19 |
Correct |
17 ms |
30544 KB |
Output is correct |
20 |
Correct |
16 ms |
30556 KB |
Output is correct |
21 |
Correct |
18 ms |
30800 KB |
Output is correct |
22 |
Correct |
16 ms |
34384 KB |
Output is correct |
23 |
Correct |
16 ms |
32336 KB |
Output is correct |
24 |
Correct |
16 ms |
30800 KB |
Output is correct |
25 |
Correct |
22 ms |
32336 KB |
Output is correct |
26 |
Correct |
16 ms |
30800 KB |
Output is correct |
27 |
Correct |
17 ms |
30968 KB |
Output is correct |
28 |
Correct |
15 ms |
32588 KB |
Output is correct |
29 |
Correct |
16 ms |
34384 KB |
Output is correct |
30 |
Correct |
17 ms |
31056 KB |
Output is correct |
31 |
Correct |
18 ms |
30800 KB |
Output is correct |
32 |
Correct |
17 ms |
32336 KB |
Output is correct |
33 |
Correct |
19 ms |
34384 KB |
Output is correct |
34 |
Correct |
20 ms |
30800 KB |
Output is correct |
35 |
Correct |
19 ms |
30800 KB |
Output is correct |
36 |
Correct |
18 ms |
30800 KB |
Output is correct |
37 |
Correct |
18 ms |
30800 KB |
Output is correct |
38 |
Correct |
18 ms |
30972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
34384 KB |
Output is correct |
2 |
Correct |
13 ms |
34440 KB |
Output is correct |
3 |
Correct |
13 ms |
34396 KB |
Output is correct |
4 |
Correct |
12 ms |
34384 KB |
Output is correct |
5 |
Correct |
16 ms |
34384 KB |
Output is correct |
6 |
Correct |
15 ms |
34552 KB |
Output is correct |
7 |
Correct |
14 ms |
32336 KB |
Output is correct |
8 |
Correct |
13 ms |
30748 KB |
Output is correct |
9 |
Correct |
14 ms |
30544 KB |
Output is correct |
10 |
Correct |
19 ms |
30768 KB |
Output is correct |
11 |
Correct |
16 ms |
30544 KB |
Output is correct |
12 |
Correct |
15 ms |
32336 KB |
Output is correct |
13 |
Correct |
15 ms |
32336 KB |
Output is correct |
14 |
Correct |
14 ms |
32336 KB |
Output is correct |
15 |
Correct |
14 ms |
30680 KB |
Output is correct |
16 |
Correct |
14 ms |
32336 KB |
Output is correct |
17 |
Correct |
18 ms |
30544 KB |
Output is correct |
18 |
Correct |
20 ms |
30544 KB |
Output is correct |
19 |
Correct |
622 ms |
55112 KB |
Output is correct |
20 |
Correct |
610 ms |
54344 KB |
Output is correct |
21 |
Correct |
789 ms |
54216 KB |
Output is correct |
22 |
Correct |
884 ms |
58380 KB |
Output is correct |
23 |
Correct |
296 ms |
56788 KB |
Output is correct |
24 |
Correct |
342 ms |
58696 KB |
Output is correct |
25 |
Correct |
823 ms |
59720 KB |
Output is correct |
26 |
Correct |
603 ms |
67740 KB |
Output is correct |
27 |
Correct |
610 ms |
81912 KB |
Output is correct |
28 |
Correct |
797 ms |
97824 KB |
Output is correct |
29 |
Correct |
848 ms |
92176 KB |
Output is correct |
30 |
Correct |
543 ms |
80612 KB |
Output is correct |
31 |
Correct |
530 ms |
81736 KB |
Output is correct |
32 |
Correct |
798 ms |
80968 KB |
Output is correct |
33 |
Correct |
1131 ms |
83032 KB |
Output is correct |
34 |
Correct |
540 ms |
79824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
34384 KB |
Output is correct |
2 |
Correct |
13 ms |
34440 KB |
Output is correct |
3 |
Correct |
13 ms |
34396 KB |
Output is correct |
4 |
Correct |
12 ms |
34384 KB |
Output is correct |
5 |
Correct |
16 ms |
34384 KB |
Output is correct |
6 |
Correct |
15 ms |
34552 KB |
Output is correct |
7 |
Correct |
14 ms |
32336 KB |
Output is correct |
8 |
Correct |
13 ms |
30748 KB |
Output is correct |
9 |
Correct |
14 ms |
30544 KB |
Output is correct |
10 |
Correct |
19 ms |
30768 KB |
Output is correct |
11 |
Correct |
16 ms |
30544 KB |
Output is correct |
12 |
Correct |
15 ms |
32336 KB |
Output is correct |
13 |
Correct |
15 ms |
32336 KB |
Output is correct |
14 |
Correct |
14 ms |
32336 KB |
Output is correct |
15 |
Correct |
14 ms |
30680 KB |
Output is correct |
16 |
Correct |
14 ms |
32336 KB |
Output is correct |
17 |
Correct |
18 ms |
30544 KB |
Output is correct |
18 |
Correct |
20 ms |
30544 KB |
Output is correct |
19 |
Correct |
17 ms |
30544 KB |
Output is correct |
20 |
Correct |
16 ms |
30556 KB |
Output is correct |
21 |
Correct |
18 ms |
30800 KB |
Output is correct |
22 |
Correct |
16 ms |
34384 KB |
Output is correct |
23 |
Correct |
16 ms |
32336 KB |
Output is correct |
24 |
Correct |
16 ms |
30800 KB |
Output is correct |
25 |
Correct |
22 ms |
32336 KB |
Output is correct |
26 |
Correct |
16 ms |
30800 KB |
Output is correct |
27 |
Correct |
17 ms |
30968 KB |
Output is correct |
28 |
Correct |
15 ms |
32588 KB |
Output is correct |
29 |
Correct |
16 ms |
34384 KB |
Output is correct |
30 |
Correct |
17 ms |
31056 KB |
Output is correct |
31 |
Correct |
18 ms |
30800 KB |
Output is correct |
32 |
Correct |
17 ms |
32336 KB |
Output is correct |
33 |
Correct |
19 ms |
34384 KB |
Output is correct |
34 |
Correct |
20 ms |
30800 KB |
Output is correct |
35 |
Correct |
19 ms |
30800 KB |
Output is correct |
36 |
Correct |
18 ms |
30800 KB |
Output is correct |
37 |
Correct |
18 ms |
30800 KB |
Output is correct |
38 |
Correct |
18 ms |
30972 KB |
Output is correct |
39 |
Correct |
622 ms |
55112 KB |
Output is correct |
40 |
Correct |
610 ms |
54344 KB |
Output is correct |
41 |
Correct |
789 ms |
54216 KB |
Output is correct |
42 |
Correct |
884 ms |
58380 KB |
Output is correct |
43 |
Correct |
296 ms |
56788 KB |
Output is correct |
44 |
Correct |
342 ms |
58696 KB |
Output is correct |
45 |
Correct |
823 ms |
59720 KB |
Output is correct |
46 |
Correct |
603 ms |
67740 KB |
Output is correct |
47 |
Correct |
610 ms |
81912 KB |
Output is correct |
48 |
Correct |
797 ms |
97824 KB |
Output is correct |
49 |
Correct |
848 ms |
92176 KB |
Output is correct |
50 |
Correct |
543 ms |
80612 KB |
Output is correct |
51 |
Correct |
530 ms |
81736 KB |
Output is correct |
52 |
Correct |
798 ms |
80968 KB |
Output is correct |
53 |
Correct |
1131 ms |
83032 KB |
Output is correct |
54 |
Correct |
540 ms |
79824 KB |
Output is correct |
55 |
Correct |
52 ms |
33236 KB |
Output is correct |
56 |
Correct |
51 ms |
33072 KB |
Output is correct |
57 |
Correct |
611 ms |
55260 KB |
Output is correct |
58 |
Correct |
141 ms |
54104 KB |
Output is correct |
59 |
Correct |
615 ms |
61856 KB |
Output is correct |
60 |
Execution timed out |
3061 ms |
92492 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |