#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef tree <pii, null_type, less <pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int max_N = 200003, max_K = 1000003, INF = 1e9;
int n, k, sz[max_N], mx[max_N], del[max_N], val[max_K], ans = max_N;
vector <pii> adj[max_N];
void dfs(int v, int p) {
sz[v] = 1;
mx[v] = -1;
for (auto i : adj[v]) {
int to = i.F;
if (to == p || del[to]) {
continue;
}
dfs(to, v);
sz[v] += sz[to];
if (mx[v] == -1 || sz[mx[v]] < sz[to]) {
mx[v] = to;
}
}
}
int centroid(int v) {
int u = v;
while (mx[v] != -1 && sz[mx[v]] > sz[u] / 2) {
v = mx[v];
}
return v;
}
void add(int v, int p, int d, int l) {
if (d > k) {
return;
}
val[d] = min(val[d], l);
for (auto i : adj[v]) {
int to = i.F, w = i.S;
if (to == p || del[to]) {
continue;
}
add(to, v, d + w, l + 1);
}
}
void calc(int v, int p, int d, int l) {
if (d > k) {
return;
}
ans = min(ans, val[k - d] + l);
for (auto i : adj[v]) {
int to = i.F, w = i.S;
if (to == p || del[to]) {
continue;
}
calc(to, v, d + w, l + 1);
}
}
void clear(int v, int p, int d, int l) {
if (d > k) {
return;
}
val[d] = INF;
for (auto i : adj[v]) {
int to = i.F, w = i.S;
if (to == p || del[to]) {
continue;
}
clear(to, v, d + w, l + 1);
}
}
void solve(int v) {
val[0] = 0;
for (auto i : adj[v]) {
int to = i.F, w = i.S;
if (del[to]) {
continue;
}
calc(to, v, w, 1);
add(to, v, w, 1);
}
for (auto i : adj[v]) {
int to = i.F, w = i.S;
if (del[to]) {
continue;
}
clear(to, v, w, 1);
}
}
void build(int v, int d) {
dfs(v, -1);
int c = centroid(v);
solve(c);
del[c] = 1;
for (auto i : adj[c]) {
int to = i.F;
if (!del[to]) {
build(to, d + 1);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
for (int i = 0; i < n - 1; i++) {
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
memset(& val, 0x3f, sizeof(val));
build(0, 0);
if (ans == max_N) {
return -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
9 ms |
8960 KB |
Output is correct |
4 |
Correct |
10 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
10 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
10 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
9012 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
9 ms |
8960 KB |
Output is correct |
4 |
Correct |
10 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
10 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
10 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
9012 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
10 ms |
8992 KB |
Output is correct |
21 |
Correct |
11 ms |
8960 KB |
Output is correct |
22 |
Correct |
10 ms |
9088 KB |
Output is correct |
23 |
Correct |
10 ms |
8960 KB |
Output is correct |
24 |
Correct |
10 ms |
8960 KB |
Output is correct |
25 |
Correct |
10 ms |
9088 KB |
Output is correct |
26 |
Correct |
10 ms |
8960 KB |
Output is correct |
27 |
Correct |
10 ms |
8960 KB |
Output is correct |
28 |
Correct |
10 ms |
9088 KB |
Output is correct |
29 |
Correct |
10 ms |
9120 KB |
Output is correct |
30 |
Correct |
10 ms |
8960 KB |
Output is correct |
31 |
Correct |
10 ms |
8960 KB |
Output is correct |
32 |
Correct |
10 ms |
9120 KB |
Output is correct |
33 |
Correct |
11 ms |
9048 KB |
Output is correct |
34 |
Correct |
10 ms |
9088 KB |
Output is correct |
35 |
Correct |
10 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
8960 KB |
Output is correct |
37 |
Correct |
11 ms |
9088 KB |
Output is correct |
38 |
Correct |
10 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
9 ms |
8960 KB |
Output is correct |
4 |
Correct |
10 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
10 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
10 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
9012 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
180 ms |
14968 KB |
Output is correct |
20 |
Correct |
171 ms |
14968 KB |
Output is correct |
21 |
Correct |
210 ms |
14968 KB |
Output is correct |
22 |
Correct |
207 ms |
15132 KB |
Output is correct |
23 |
Correct |
107 ms |
15224 KB |
Output is correct |
24 |
Correct |
78 ms |
15108 KB |
Output is correct |
25 |
Correct |
151 ms |
18552 KB |
Output is correct |
26 |
Correct |
110 ms |
22264 KB |
Output is correct |
27 |
Correct |
191 ms |
21240 KB |
Output is correct |
28 |
Correct |
331 ms |
35576 KB |
Output is correct |
29 |
Correct |
318 ms |
34296 KB |
Output is correct |
30 |
Correct |
204 ms |
21112 KB |
Output is correct |
31 |
Correct |
191 ms |
21216 KB |
Output is correct |
32 |
Correct |
263 ms |
21368 KB |
Output is correct |
33 |
Correct |
284 ms |
20192 KB |
Output is correct |
34 |
Correct |
273 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
9 ms |
8960 KB |
Output is correct |
4 |
Correct |
10 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
10 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
10 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
9012 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
10 ms |
8992 KB |
Output is correct |
21 |
Correct |
11 ms |
8960 KB |
Output is correct |
22 |
Correct |
10 ms |
9088 KB |
Output is correct |
23 |
Correct |
10 ms |
8960 KB |
Output is correct |
24 |
Correct |
10 ms |
8960 KB |
Output is correct |
25 |
Correct |
10 ms |
9088 KB |
Output is correct |
26 |
Correct |
10 ms |
8960 KB |
Output is correct |
27 |
Correct |
10 ms |
8960 KB |
Output is correct |
28 |
Correct |
10 ms |
9088 KB |
Output is correct |
29 |
Correct |
10 ms |
9120 KB |
Output is correct |
30 |
Correct |
10 ms |
8960 KB |
Output is correct |
31 |
Correct |
10 ms |
8960 KB |
Output is correct |
32 |
Correct |
10 ms |
9120 KB |
Output is correct |
33 |
Correct |
11 ms |
9048 KB |
Output is correct |
34 |
Correct |
10 ms |
9088 KB |
Output is correct |
35 |
Correct |
10 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
8960 KB |
Output is correct |
37 |
Correct |
11 ms |
9088 KB |
Output is correct |
38 |
Correct |
10 ms |
8960 KB |
Output is correct |
39 |
Correct |
180 ms |
14968 KB |
Output is correct |
40 |
Correct |
171 ms |
14968 KB |
Output is correct |
41 |
Correct |
210 ms |
14968 KB |
Output is correct |
42 |
Correct |
207 ms |
15132 KB |
Output is correct |
43 |
Correct |
107 ms |
15224 KB |
Output is correct |
44 |
Correct |
78 ms |
15108 KB |
Output is correct |
45 |
Correct |
151 ms |
18552 KB |
Output is correct |
46 |
Correct |
110 ms |
22264 KB |
Output is correct |
47 |
Correct |
191 ms |
21240 KB |
Output is correct |
48 |
Correct |
331 ms |
35576 KB |
Output is correct |
49 |
Correct |
318 ms |
34296 KB |
Output is correct |
50 |
Correct |
204 ms |
21112 KB |
Output is correct |
51 |
Correct |
191 ms |
21216 KB |
Output is correct |
52 |
Correct |
263 ms |
21368 KB |
Output is correct |
53 |
Correct |
284 ms |
20192 KB |
Output is correct |
54 |
Correct |
273 ms |
20088 KB |
Output is correct |
55 |
Correct |
19 ms |
9600 KB |
Output is correct |
56 |
Correct |
20 ms |
9600 KB |
Output is correct |
57 |
Correct |
97 ms |
15192 KB |
Output is correct |
58 |
Correct |
52 ms |
15248 KB |
Output is correct |
59 |
Correct |
112 ms |
22308 KB |
Output is correct |
60 |
Correct |
672 ms |
34912 KB |
Output is correct |
61 |
Correct |
224 ms |
21240 KB |
Output is correct |
62 |
Correct |
269 ms |
21196 KB |
Output is correct |
63 |
Correct |
398 ms |
21240 KB |
Output is correct |
64 |
Correct |
721 ms |
20988 KB |
Output is correct |
65 |
Correct |
304 ms |
21112 KB |
Output is correct |
66 |
Correct |
489 ms |
31776 KB |
Output is correct |
67 |
Correct |
149 ms |
21476 KB |
Output is correct |
68 |
Correct |
339 ms |
21432 KB |
Output is correct |
69 |
Correct |
338 ms |
21624 KB |
Output is correct |
70 |
Correct |
329 ms |
20836 KB |
Output is correct |