#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 = 1e6 + 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];
void find_size(int x, int p) {
sub[x] = 1;
for(int i : adj[x]) {
if(i != p) {
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) {
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) {
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) {
find_size(x, -1);
int centroid = find_centroid(x, -1, sub[x]);
process_centroid(centroid);
for(int i : adj[centroid]) {
adj[i].erase(centroid);
}
for(int i : adj[centroid]) {
decompo(i);
}
}
int best_path(int N, int Ked, int H[][2], int L[]) {
fill(A, A + MAXK, 0);
fill(BOOK_K, BOOK_K + MAXK, 0);
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);
if(ANS != 1e9) {
return ANS;
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22608 KB |
Output is correct |
2 |
Correct |
9 ms |
26144 KB |
Output is correct |
3 |
Correct |
9 ms |
22608 KB |
Output is correct |
4 |
Correct |
9 ms |
22608 KB |
Output is correct |
5 |
Correct |
8 ms |
26192 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
10 ms |
26192 KB |
Output is correct |
8 |
Correct |
7 ms |
26192 KB |
Output is correct |
9 |
Correct |
6 ms |
26312 KB |
Output is correct |
10 |
Correct |
7 ms |
26192 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26192 KB |
Output is correct |
13 |
Correct |
8 ms |
26192 KB |
Output is correct |
14 |
Correct |
7 ms |
24144 KB |
Output is correct |
15 |
Correct |
9 ms |
22608 KB |
Output is correct |
16 |
Correct |
9 ms |
22520 KB |
Output is correct |
17 |
Correct |
10 ms |
22776 KB |
Output is correct |
18 |
Correct |
10 ms |
22608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22608 KB |
Output is correct |
2 |
Correct |
9 ms |
26144 KB |
Output is correct |
3 |
Correct |
9 ms |
22608 KB |
Output is correct |
4 |
Correct |
9 ms |
22608 KB |
Output is correct |
5 |
Correct |
8 ms |
26192 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
10 ms |
26192 KB |
Output is correct |
8 |
Correct |
7 ms |
26192 KB |
Output is correct |
9 |
Correct |
6 ms |
26312 KB |
Output is correct |
10 |
Correct |
7 ms |
26192 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26192 KB |
Output is correct |
13 |
Correct |
8 ms |
26192 KB |
Output is correct |
14 |
Correct |
7 ms |
24144 KB |
Output is correct |
15 |
Correct |
9 ms |
22608 KB |
Output is correct |
16 |
Correct |
9 ms |
22520 KB |
Output is correct |
17 |
Correct |
10 ms |
22776 KB |
Output is correct |
18 |
Correct |
10 ms |
22608 KB |
Output is correct |
19 |
Correct |
9 ms |
24156 KB |
Output is correct |
20 |
Correct |
8 ms |
26192 KB |
Output is correct |
21 |
Correct |
11 ms |
22864 KB |
Output is correct |
22 |
Correct |
10 ms |
22864 KB |
Output is correct |
23 |
Correct |
10 ms |
22864 KB |
Output is correct |
24 |
Correct |
11 ms |
22864 KB |
Output is correct |
25 |
Correct |
14 ms |
23032 KB |
Output is correct |
26 |
Correct |
11 ms |
22864 KB |
Output is correct |
27 |
Correct |
9 ms |
22836 KB |
Output is correct |
28 |
Correct |
9 ms |
24400 KB |
Output is correct |
29 |
Correct |
13 ms |
22864 KB |
Output is correct |
30 |
Correct |
15 ms |
22864 KB |
Output is correct |
31 |
Correct |
10 ms |
22864 KB |
Output is correct |
32 |
Correct |
13 ms |
22892 KB |
Output is correct |
33 |
Correct |
13 ms |
23036 KB |
Output is correct |
34 |
Correct |
13 ms |
22864 KB |
Output is correct |
35 |
Correct |
11 ms |
23120 KB |
Output is correct |
36 |
Correct |
12 ms |
22864 KB |
Output is correct |
37 |
Correct |
10 ms |
22880 KB |
Output is correct |
38 |
Correct |
10 ms |
26576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22608 KB |
Output is correct |
2 |
Correct |
9 ms |
26144 KB |
Output is correct |
3 |
Correct |
9 ms |
22608 KB |
Output is correct |
4 |
Correct |
9 ms |
22608 KB |
Output is correct |
5 |
Correct |
8 ms |
26192 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
10 ms |
26192 KB |
Output is correct |
8 |
Correct |
7 ms |
26192 KB |
Output is correct |
9 |
Correct |
6 ms |
26312 KB |
Output is correct |
10 |
Correct |
7 ms |
26192 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26192 KB |
Output is correct |
13 |
Correct |
8 ms |
26192 KB |
Output is correct |
14 |
Correct |
7 ms |
24144 KB |
Output is correct |
15 |
Correct |
9 ms |
22608 KB |
Output is correct |
16 |
Correct |
9 ms |
22520 KB |
Output is correct |
17 |
Correct |
10 ms |
22776 KB |
Output is correct |
18 |
Correct |
10 ms |
22608 KB |
Output is correct |
19 |
Correct |
633 ms |
47780 KB |
Output is correct |
20 |
Correct |
622 ms |
50868 KB |
Output is correct |
21 |
Correct |
820 ms |
50760 KB |
Output is correct |
22 |
Correct |
795 ms |
45964 KB |
Output is correct |
23 |
Correct |
309 ms |
46152 KB |
Output is correct |
24 |
Correct |
290 ms |
46152 KB |
Output is correct |
25 |
Correct |
828 ms |
50248 KB |
Output is correct |
26 |
Correct |
612 ms |
53832 KB |
Output is correct |
27 |
Correct |
552 ms |
69192 KB |
Output is correct |
28 |
Correct |
732 ms |
85052 KB |
Output is correct |
29 |
Correct |
718 ms |
83928 KB |
Output is correct |
30 |
Correct |
477 ms |
69416 KB |
Output is correct |
31 |
Correct |
484 ms |
69532 KB |
Output is correct |
32 |
Correct |
720 ms |
69400 KB |
Output is correct |
33 |
Correct |
1177 ms |
69448 KB |
Output is correct |
34 |
Correct |
567 ms |
69448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22608 KB |
Output is correct |
2 |
Correct |
9 ms |
26144 KB |
Output is correct |
3 |
Correct |
9 ms |
22608 KB |
Output is correct |
4 |
Correct |
9 ms |
22608 KB |
Output is correct |
5 |
Correct |
8 ms |
26192 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
10 ms |
26192 KB |
Output is correct |
8 |
Correct |
7 ms |
26192 KB |
Output is correct |
9 |
Correct |
6 ms |
26312 KB |
Output is correct |
10 |
Correct |
7 ms |
26192 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26192 KB |
Output is correct |
13 |
Correct |
8 ms |
26192 KB |
Output is correct |
14 |
Correct |
7 ms |
24144 KB |
Output is correct |
15 |
Correct |
9 ms |
22608 KB |
Output is correct |
16 |
Correct |
9 ms |
22520 KB |
Output is correct |
17 |
Correct |
10 ms |
22776 KB |
Output is correct |
18 |
Correct |
10 ms |
22608 KB |
Output is correct |
19 |
Correct |
9 ms |
24156 KB |
Output is correct |
20 |
Correct |
8 ms |
26192 KB |
Output is correct |
21 |
Correct |
11 ms |
22864 KB |
Output is correct |
22 |
Correct |
10 ms |
22864 KB |
Output is correct |
23 |
Correct |
10 ms |
22864 KB |
Output is correct |
24 |
Correct |
11 ms |
22864 KB |
Output is correct |
25 |
Correct |
14 ms |
23032 KB |
Output is correct |
26 |
Correct |
11 ms |
22864 KB |
Output is correct |
27 |
Correct |
9 ms |
22836 KB |
Output is correct |
28 |
Correct |
9 ms |
24400 KB |
Output is correct |
29 |
Correct |
13 ms |
22864 KB |
Output is correct |
30 |
Correct |
15 ms |
22864 KB |
Output is correct |
31 |
Correct |
10 ms |
22864 KB |
Output is correct |
32 |
Correct |
13 ms |
22892 KB |
Output is correct |
33 |
Correct |
13 ms |
23036 KB |
Output is correct |
34 |
Correct |
13 ms |
22864 KB |
Output is correct |
35 |
Correct |
11 ms |
23120 KB |
Output is correct |
36 |
Correct |
12 ms |
22864 KB |
Output is correct |
37 |
Correct |
10 ms |
22880 KB |
Output is correct |
38 |
Correct |
10 ms |
26576 KB |
Output is correct |
39 |
Correct |
633 ms |
47780 KB |
Output is correct |
40 |
Correct |
622 ms |
50868 KB |
Output is correct |
41 |
Correct |
820 ms |
50760 KB |
Output is correct |
42 |
Correct |
795 ms |
45964 KB |
Output is correct |
43 |
Correct |
309 ms |
46152 KB |
Output is correct |
44 |
Correct |
290 ms |
46152 KB |
Output is correct |
45 |
Correct |
828 ms |
50248 KB |
Output is correct |
46 |
Correct |
612 ms |
53832 KB |
Output is correct |
47 |
Correct |
552 ms |
69192 KB |
Output is correct |
48 |
Correct |
732 ms |
85052 KB |
Output is correct |
49 |
Correct |
718 ms |
83928 KB |
Output is correct |
50 |
Correct |
477 ms |
69416 KB |
Output is correct |
51 |
Correct |
484 ms |
69532 KB |
Output is correct |
52 |
Correct |
720 ms |
69400 KB |
Output is correct |
53 |
Correct |
1177 ms |
69448 KB |
Output is correct |
54 |
Correct |
567 ms |
69448 KB |
Output is correct |
55 |
Correct |
41 ms |
28496 KB |
Output is correct |
56 |
Correct |
44 ms |
26448 KB |
Output is correct |
57 |
Correct |
551 ms |
47768 KB |
Output is correct |
58 |
Correct |
128 ms |
46152 KB |
Output is correct |
59 |
Correct |
608 ms |
53840 KB |
Output is correct |
60 |
Execution timed out |
3063 ms |
85300 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |