#include <bits/stdc++.h>
#include "race.h"
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC target("avx2")
using namespace std;
#define pb push_back
#define sz size()
#define all(x) x.begin(), x.end()
#define F first
#define S second
typedef pair < int, int > pii;
typedef vector < int > vi;
typedef vector < vi > vvi;
const int N = 200200, A = 1000001;
vector <vector <pii> > g(N);
int _sz[N], used[N];
int mn[A];
void calc_sz (int v, int pr) {
_sz[v] = 1;
for (auto i : g[v]) {
int to = i.F;
if (used[to] || to == pr) {
continue;
}
calc_sz(to, v);
_sz[v] += _sz[to];
}
}
int get_centroid (int v, int pr, int n) {
for (auto i : g[v]) {
int to = i.F;
if (!used[to] && to != pr && _sz[to] > n / 2) {
return get_centroid(to, v, n);
}
}
return v;
}
int ans = 1e9;
vector <pii> vec;
void dfs (int v, int w, int pr, int d, int k) {
if (w > k) {
return;
}
int need = k - w;
if (need == 0) {
ans = min(ans, d);
}
if (mn[need] != 1e9) {
ans = min(ans, mn[need] + d);
}
vec.pb({w, d});
for (auto i : g[v]) {
int to = i.F, w1 = i.S;
if (used[to] || to == pr) {
continue;
}
dfs (to, w + w1, v, d + 1, k);
}
}
void calc_ans (int c, int k) {
calc_sz(c, c);
int v = get_centroid(c, c, _sz[c]);
used[v] = 1;
vector <pii> cur;
for (auto i : g[v]) {
int to = i.F, w = i.S;
if (used[to]) {
continue;
}
dfs (to, w, v, 1, k);
for(auto j : vec) {
mn[j.F] = min(mn[j.F], j.S);
cur.pb(j);
}
vec.clear();
}
for (auto i : cur) {
mn[i.F] = 1e9;
}
for (auto i : g[v]) {
int to = i.F;
if (used[to]) {
continue;
}
calc_ans(to, k);
}
}
int best_path (int N, int K, int h[][2], int l[]) {
int n = N;
int k = K;
for (int i = 0; i < n - 1; i++) {
int u = h[i][0], v = h[i][1], w = l[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
for (int i = 0; i < A; i++) {
mn[i] = 1e9;
}
calc_ans(0, k);
if (ans == 1e9) {
ans = -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8872 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8872 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
4 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
9016 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
9044 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
9004 KB |
Output is correct |
29 |
Correct |
5 ms |
9044 KB |
Output is correct |
30 |
Correct |
5 ms |
9044 KB |
Output is correct |
31 |
Correct |
5 ms |
9044 KB |
Output is correct |
32 |
Correct |
5 ms |
9044 KB |
Output is correct |
33 |
Correct |
6 ms |
9044 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
9004 KB |
Output is correct |
36 |
Correct |
7 ms |
8916 KB |
Output is correct |
37 |
Correct |
6 ms |
9044 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8872 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
110 ms |
14888 KB |
Output is correct |
20 |
Correct |
120 ms |
16172 KB |
Output is correct |
21 |
Correct |
119 ms |
16812 KB |
Output is correct |
22 |
Correct |
111 ms |
17660 KB |
Output is correct |
23 |
Correct |
75 ms |
16220 KB |
Output is correct |
24 |
Correct |
62 ms |
16076 KB |
Output is correct |
25 |
Correct |
111 ms |
17216 KB |
Output is correct |
26 |
Correct |
89 ms |
22084 KB |
Output is correct |
27 |
Correct |
160 ms |
23444 KB |
Output is correct |
28 |
Correct |
234 ms |
28768 KB |
Output is correct |
29 |
Correct |
231 ms |
28236 KB |
Output is correct |
30 |
Correct |
174 ms |
23484 KB |
Output is correct |
31 |
Correct |
158 ms |
23376 KB |
Output is correct |
32 |
Correct |
188 ms |
23360 KB |
Output is correct |
33 |
Correct |
232 ms |
22232 KB |
Output is correct |
34 |
Correct |
175 ms |
22860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8872 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
4 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
9016 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
9044 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
9004 KB |
Output is correct |
29 |
Correct |
5 ms |
9044 KB |
Output is correct |
30 |
Correct |
5 ms |
9044 KB |
Output is correct |
31 |
Correct |
5 ms |
9044 KB |
Output is correct |
32 |
Correct |
5 ms |
9044 KB |
Output is correct |
33 |
Correct |
6 ms |
9044 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
9004 KB |
Output is correct |
36 |
Correct |
7 ms |
8916 KB |
Output is correct |
37 |
Correct |
6 ms |
9044 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
39 |
Correct |
110 ms |
14888 KB |
Output is correct |
40 |
Correct |
120 ms |
16172 KB |
Output is correct |
41 |
Correct |
119 ms |
16812 KB |
Output is correct |
42 |
Correct |
111 ms |
17660 KB |
Output is correct |
43 |
Correct |
75 ms |
16220 KB |
Output is correct |
44 |
Correct |
62 ms |
16076 KB |
Output is correct |
45 |
Correct |
111 ms |
17216 KB |
Output is correct |
46 |
Correct |
89 ms |
22084 KB |
Output is correct |
47 |
Correct |
160 ms |
23444 KB |
Output is correct |
48 |
Correct |
234 ms |
28768 KB |
Output is correct |
49 |
Correct |
231 ms |
28236 KB |
Output is correct |
50 |
Correct |
174 ms |
23484 KB |
Output is correct |
51 |
Correct |
158 ms |
23376 KB |
Output is correct |
52 |
Correct |
188 ms |
23360 KB |
Output is correct |
53 |
Correct |
232 ms |
22232 KB |
Output is correct |
54 |
Correct |
175 ms |
22860 KB |
Output is correct |
55 |
Correct |
12 ms |
9812 KB |
Output is correct |
56 |
Correct |
12 ms |
9772 KB |
Output is correct |
57 |
Correct |
78 ms |
17696 KB |
Output is correct |
58 |
Correct |
36 ms |
16896 KB |
Output is correct |
59 |
Correct |
95 ms |
22128 KB |
Output is correct |
60 |
Correct |
355 ms |
35516 KB |
Output is correct |
61 |
Correct |
187 ms |
23792 KB |
Output is correct |
62 |
Correct |
186 ms |
25032 KB |
Output is correct |
63 |
Correct |
224 ms |
24968 KB |
Output is correct |
64 |
Correct |
409 ms |
27708 KB |
Output is correct |
65 |
Correct |
208 ms |
24196 KB |
Output is correct |
66 |
Correct |
315 ms |
27628 KB |
Output is correct |
67 |
Correct |
101 ms |
26272 KB |
Output is correct |
68 |
Correct |
226 ms |
26400 KB |
Output is correct |
69 |
Correct |
220 ms |
26460 KB |
Output is correct |
70 |
Correct |
223 ms |
25680 KB |
Output is correct |