#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;
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;
memset(& del, 0, sizeof(del));
for (int i = 0; i < n - 1; i++) {
adj[i].clear();
}
ans = max_N;
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 |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9856 KB |
Output is correct |
22 |
Correct |
11 ms |
9856 KB |
Output is correct |
23 |
Correct |
10 ms |
9856 KB |
Output is correct |
24 |
Correct |
10 ms |
9728 KB |
Output is correct |
25 |
Correct |
11 ms |
9856 KB |
Output is correct |
26 |
Correct |
10 ms |
9728 KB |
Output is correct |
27 |
Correct |
10 ms |
9728 KB |
Output is correct |
28 |
Correct |
12 ms |
9728 KB |
Output is correct |
29 |
Correct |
11 ms |
9728 KB |
Output is correct |
30 |
Correct |
10 ms |
9856 KB |
Output is correct |
31 |
Correct |
11 ms |
9856 KB |
Output is correct |
32 |
Correct |
11 ms |
9856 KB |
Output is correct |
33 |
Correct |
11 ms |
9728 KB |
Output is correct |
34 |
Correct |
10 ms |
9856 KB |
Output is correct |
35 |
Correct |
10 ms |
9728 KB |
Output is correct |
36 |
Correct |
10 ms |
9856 KB |
Output is correct |
37 |
Correct |
11 ms |
9728 KB |
Output is correct |
38 |
Correct |
11 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
179 ms |
15480 KB |
Output is correct |
20 |
Correct |
179 ms |
16760 KB |
Output is correct |
21 |
Correct |
200 ms |
16760 KB |
Output is correct |
22 |
Correct |
192 ms |
16760 KB |
Output is correct |
23 |
Correct |
111 ms |
17016 KB |
Output is correct |
24 |
Correct |
76 ms |
16888 KB |
Output is correct |
25 |
Correct |
161 ms |
20344 KB |
Output is correct |
26 |
Correct |
113 ms |
23928 KB |
Output is correct |
27 |
Correct |
203 ms |
24124 KB |
Output is correct |
28 |
Correct |
332 ms |
38520 KB |
Output is correct |
29 |
Correct |
343 ms |
37472 KB |
Output is correct |
30 |
Correct |
200 ms |
24056 KB |
Output is correct |
31 |
Correct |
198 ms |
24056 KB |
Output is correct |
32 |
Correct |
259 ms |
24324 KB |
Output is correct |
33 |
Correct |
280 ms |
23032 KB |
Output is correct |
34 |
Correct |
241 ms |
24008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9856 KB |
Output is correct |
22 |
Correct |
11 ms |
9856 KB |
Output is correct |
23 |
Correct |
10 ms |
9856 KB |
Output is correct |
24 |
Correct |
10 ms |
9728 KB |
Output is correct |
25 |
Correct |
11 ms |
9856 KB |
Output is correct |
26 |
Correct |
10 ms |
9728 KB |
Output is correct |
27 |
Correct |
10 ms |
9728 KB |
Output is correct |
28 |
Correct |
12 ms |
9728 KB |
Output is correct |
29 |
Correct |
11 ms |
9728 KB |
Output is correct |
30 |
Correct |
10 ms |
9856 KB |
Output is correct |
31 |
Correct |
11 ms |
9856 KB |
Output is correct |
32 |
Correct |
11 ms |
9856 KB |
Output is correct |
33 |
Correct |
11 ms |
9728 KB |
Output is correct |
34 |
Correct |
10 ms |
9856 KB |
Output is correct |
35 |
Correct |
10 ms |
9728 KB |
Output is correct |
36 |
Correct |
10 ms |
9856 KB |
Output is correct |
37 |
Correct |
11 ms |
9728 KB |
Output is correct |
38 |
Correct |
11 ms |
9728 KB |
Output is correct |
39 |
Correct |
179 ms |
15480 KB |
Output is correct |
40 |
Correct |
179 ms |
16760 KB |
Output is correct |
41 |
Correct |
200 ms |
16760 KB |
Output is correct |
42 |
Correct |
192 ms |
16760 KB |
Output is correct |
43 |
Correct |
111 ms |
17016 KB |
Output is correct |
44 |
Correct |
76 ms |
16888 KB |
Output is correct |
45 |
Correct |
161 ms |
20344 KB |
Output is correct |
46 |
Correct |
113 ms |
23928 KB |
Output is correct |
47 |
Correct |
203 ms |
24124 KB |
Output is correct |
48 |
Correct |
332 ms |
38520 KB |
Output is correct |
49 |
Correct |
343 ms |
37472 KB |
Output is correct |
50 |
Correct |
200 ms |
24056 KB |
Output is correct |
51 |
Correct |
198 ms |
24056 KB |
Output is correct |
52 |
Correct |
259 ms |
24324 KB |
Output is correct |
53 |
Correct |
280 ms |
23032 KB |
Output is correct |
54 |
Correct |
241 ms |
24008 KB |
Output is correct |
55 |
Correct |
20 ms |
10496 KB |
Output is correct |
56 |
Correct |
21 ms |
10496 KB |
Output is correct |
57 |
Correct |
102 ms |
17016 KB |
Output is correct |
58 |
Correct |
49 ms |
16620 KB |
Output is correct |
59 |
Correct |
114 ms |
24056 KB |
Output is correct |
60 |
Correct |
616 ms |
37876 KB |
Output is correct |
61 |
Correct |
215 ms |
24312 KB |
Output is correct |
62 |
Correct |
246 ms |
24184 KB |
Output is correct |
63 |
Correct |
355 ms |
24184 KB |
Output is correct |
64 |
Correct |
696 ms |
24440 KB |
Output is correct |
65 |
Correct |
287 ms |
24952 KB |
Output is correct |
66 |
Correct |
475 ms |
35064 KB |
Output is correct |
67 |
Correct |
141 ms |
24932 KB |
Output is correct |
68 |
Correct |
324 ms |
25080 KB |
Output is correct |
69 |
Correct |
346 ms |
25208 KB |
Output is correct |
70 |
Correct |
306 ms |
24312 KB |
Output is correct |