#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
const ll MAXN = 1E6+16;
vector <pair <ll, ll> > adj[MAXN];
vector <char> used(MAXN, false);
vll sz(MAXN);
ll dfs_subsz (ll u, ll par) {
sz[u] = 1;
for (auto [v, w] : adj[u]) {
if (v == par) continue;
if (used[v]) continue;
sz[u] += dfs_subsz(v, u);
}
return sz[u];
}
ll dfs_ctd (ll u, ll par, ll n) {
for (auto [v, w] : adj[u]) {
if (v == par) continue;
if (used[v]) continue;
if (sz[v] > n/2) return dfs_ctd(v, u, n);
}
return u;
}
vll freq(MAXN, MAXN);
vll vis(MAXN, 0);
ll timer = 1;
void sett (ll dis, ll dep) {
if (vis[dis] != timer) { freq[dis] = MAXN; vis[dis] = timer; }
freq[dis] = min(freq[dis], dep);
}
ll get (ll dis) {
if (0 <= dis && dis < MAXN-8 && vis[dis] == timer) return freq[dis];
return MAXN;
}
ll k;
ll ans = MAXN;
void dfs_calc (ll u, ll par, ll dis, ll dep) {
if (dis > k) return;
ans = min(ans, get(k-dis)+dep);
for (auto [v, w] : adj[u]) {
if (v == par) continue;
if (used[v]) continue;
dfs_calc(v, u, dis+w, dep+1);
}
}
void dfs_dis (ll u, ll par, ll dis, ll dep) {
if (dis > k) return;
sett(dis, dep);
for (auto [v, w] : adj[u]) {
if (v == par) continue;
if (used[v]) continue;
dfs_dis(v, u, dis+w, dep+1);
}
}
void solve (ll u) {
ll n = dfs_subsz(u, u);
ll ctd = dfs_ctd(u, u, n);
used[ctd] = true;
timer++;
sett(0, 0);
for (auto [v, w] : adj[ctd]) {
if (used[v]) continue;
dfs_calc(v, ctd, w, 1);
dfs_dis(v, ctd, w, 1);
}
for (auto [v, w] : adj[ctd]) {
if (used[v]) continue;
solve(v);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
ll n = N;
k = K;
for (ll i = 0; i < n-1; i++) {
ll u = H[i][0], v = H[i][1];
ll w = L[i];
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
solve(0);
return (ans == MAXN ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51788 KB |
Output is correct |
2 |
Correct |
16 ms |
52060 KB |
Output is correct |
3 |
Correct |
12 ms |
51804 KB |
Output is correct |
4 |
Correct |
11 ms |
51804 KB |
Output is correct |
5 |
Correct |
12 ms |
51804 KB |
Output is correct |
6 |
Correct |
12 ms |
51960 KB |
Output is correct |
7 |
Correct |
12 ms |
51800 KB |
Output is correct |
8 |
Correct |
11 ms |
51804 KB |
Output is correct |
9 |
Correct |
11 ms |
52060 KB |
Output is correct |
10 |
Correct |
13 ms |
52060 KB |
Output is correct |
11 |
Correct |
11 ms |
52212 KB |
Output is correct |
12 |
Correct |
12 ms |
52056 KB |
Output is correct |
13 |
Correct |
12 ms |
52060 KB |
Output is correct |
14 |
Correct |
11 ms |
51800 KB |
Output is correct |
15 |
Correct |
11 ms |
52060 KB |
Output is correct |
16 |
Correct |
12 ms |
51904 KB |
Output is correct |
17 |
Correct |
12 ms |
51804 KB |
Output is correct |
18 |
Correct |
12 ms |
52056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51788 KB |
Output is correct |
2 |
Correct |
16 ms |
52060 KB |
Output is correct |
3 |
Correct |
12 ms |
51804 KB |
Output is correct |
4 |
Correct |
11 ms |
51804 KB |
Output is correct |
5 |
Correct |
12 ms |
51804 KB |
Output is correct |
6 |
Correct |
12 ms |
51960 KB |
Output is correct |
7 |
Correct |
12 ms |
51800 KB |
Output is correct |
8 |
Correct |
11 ms |
51804 KB |
Output is correct |
9 |
Correct |
11 ms |
52060 KB |
Output is correct |
10 |
Correct |
13 ms |
52060 KB |
Output is correct |
11 |
Correct |
11 ms |
52212 KB |
Output is correct |
12 |
Correct |
12 ms |
52056 KB |
Output is correct |
13 |
Correct |
12 ms |
52060 KB |
Output is correct |
14 |
Correct |
11 ms |
51800 KB |
Output is correct |
15 |
Correct |
11 ms |
52060 KB |
Output is correct |
16 |
Correct |
12 ms |
51904 KB |
Output is correct |
17 |
Correct |
12 ms |
51804 KB |
Output is correct |
18 |
Correct |
12 ms |
52056 KB |
Output is correct |
19 |
Correct |
11 ms |
51804 KB |
Output is correct |
20 |
Correct |
11 ms |
52060 KB |
Output is correct |
21 |
Correct |
13 ms |
52060 KB |
Output is correct |
22 |
Correct |
12 ms |
52060 KB |
Output is correct |
23 |
Correct |
12 ms |
52060 KB |
Output is correct |
24 |
Correct |
13 ms |
52060 KB |
Output is correct |
25 |
Correct |
12 ms |
52060 KB |
Output is correct |
26 |
Correct |
12 ms |
52060 KB |
Output is correct |
27 |
Correct |
12 ms |
52056 KB |
Output is correct |
28 |
Correct |
12 ms |
52056 KB |
Output is correct |
29 |
Correct |
13 ms |
52056 KB |
Output is correct |
30 |
Correct |
12 ms |
51904 KB |
Output is correct |
31 |
Correct |
13 ms |
52060 KB |
Output is correct |
32 |
Correct |
12 ms |
52060 KB |
Output is correct |
33 |
Correct |
12 ms |
52056 KB |
Output is correct |
34 |
Correct |
12 ms |
52056 KB |
Output is correct |
35 |
Correct |
13 ms |
52060 KB |
Output is correct |
36 |
Correct |
14 ms |
52056 KB |
Output is correct |
37 |
Correct |
12 ms |
52060 KB |
Output is correct |
38 |
Correct |
12 ms |
52060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51788 KB |
Output is correct |
2 |
Correct |
16 ms |
52060 KB |
Output is correct |
3 |
Correct |
12 ms |
51804 KB |
Output is correct |
4 |
Correct |
11 ms |
51804 KB |
Output is correct |
5 |
Correct |
12 ms |
51804 KB |
Output is correct |
6 |
Correct |
12 ms |
51960 KB |
Output is correct |
7 |
Correct |
12 ms |
51800 KB |
Output is correct |
8 |
Correct |
11 ms |
51804 KB |
Output is correct |
9 |
Correct |
11 ms |
52060 KB |
Output is correct |
10 |
Correct |
13 ms |
52060 KB |
Output is correct |
11 |
Correct |
11 ms |
52212 KB |
Output is correct |
12 |
Correct |
12 ms |
52056 KB |
Output is correct |
13 |
Correct |
12 ms |
52060 KB |
Output is correct |
14 |
Correct |
11 ms |
51800 KB |
Output is correct |
15 |
Correct |
11 ms |
52060 KB |
Output is correct |
16 |
Correct |
12 ms |
51904 KB |
Output is correct |
17 |
Correct |
12 ms |
51804 KB |
Output is correct |
18 |
Correct |
12 ms |
52056 KB |
Output is correct |
19 |
Correct |
85 ms |
59352 KB |
Output is correct |
20 |
Correct |
87 ms |
60428 KB |
Output is correct |
21 |
Correct |
98 ms |
59988 KB |
Output is correct |
22 |
Correct |
90 ms |
60736 KB |
Output is correct |
23 |
Correct |
58 ms |
61012 KB |
Output is correct |
24 |
Correct |
49 ms |
60240 KB |
Output is correct |
25 |
Correct |
89 ms |
62548 KB |
Output is correct |
26 |
Correct |
73 ms |
64336 KB |
Output is correct |
27 |
Correct |
129 ms |
66384 KB |
Output is correct |
28 |
Correct |
177 ms |
74580 KB |
Output is correct |
29 |
Correct |
166 ms |
74320 KB |
Output is correct |
30 |
Correct |
117 ms |
66936 KB |
Output is correct |
31 |
Correct |
126 ms |
67156 KB |
Output is correct |
32 |
Correct |
138 ms |
66900 KB |
Output is correct |
33 |
Correct |
149 ms |
66156 KB |
Output is correct |
34 |
Correct |
122 ms |
66268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51788 KB |
Output is correct |
2 |
Correct |
16 ms |
52060 KB |
Output is correct |
3 |
Correct |
12 ms |
51804 KB |
Output is correct |
4 |
Correct |
11 ms |
51804 KB |
Output is correct |
5 |
Correct |
12 ms |
51804 KB |
Output is correct |
6 |
Correct |
12 ms |
51960 KB |
Output is correct |
7 |
Correct |
12 ms |
51800 KB |
Output is correct |
8 |
Correct |
11 ms |
51804 KB |
Output is correct |
9 |
Correct |
11 ms |
52060 KB |
Output is correct |
10 |
Correct |
13 ms |
52060 KB |
Output is correct |
11 |
Correct |
11 ms |
52212 KB |
Output is correct |
12 |
Correct |
12 ms |
52056 KB |
Output is correct |
13 |
Correct |
12 ms |
52060 KB |
Output is correct |
14 |
Correct |
11 ms |
51800 KB |
Output is correct |
15 |
Correct |
11 ms |
52060 KB |
Output is correct |
16 |
Correct |
12 ms |
51904 KB |
Output is correct |
17 |
Correct |
12 ms |
51804 KB |
Output is correct |
18 |
Correct |
12 ms |
52056 KB |
Output is correct |
19 |
Correct |
11 ms |
51804 KB |
Output is correct |
20 |
Correct |
11 ms |
52060 KB |
Output is correct |
21 |
Correct |
13 ms |
52060 KB |
Output is correct |
22 |
Correct |
12 ms |
52060 KB |
Output is correct |
23 |
Correct |
12 ms |
52060 KB |
Output is correct |
24 |
Correct |
13 ms |
52060 KB |
Output is correct |
25 |
Correct |
12 ms |
52060 KB |
Output is correct |
26 |
Correct |
12 ms |
52060 KB |
Output is correct |
27 |
Correct |
12 ms |
52056 KB |
Output is correct |
28 |
Correct |
12 ms |
52056 KB |
Output is correct |
29 |
Correct |
13 ms |
52056 KB |
Output is correct |
30 |
Correct |
12 ms |
51904 KB |
Output is correct |
31 |
Correct |
13 ms |
52060 KB |
Output is correct |
32 |
Correct |
12 ms |
52060 KB |
Output is correct |
33 |
Correct |
12 ms |
52056 KB |
Output is correct |
34 |
Correct |
12 ms |
52056 KB |
Output is correct |
35 |
Correct |
13 ms |
52060 KB |
Output is correct |
36 |
Correct |
14 ms |
52056 KB |
Output is correct |
37 |
Correct |
12 ms |
52060 KB |
Output is correct |
38 |
Correct |
12 ms |
52060 KB |
Output is correct |
39 |
Correct |
85 ms |
59352 KB |
Output is correct |
40 |
Correct |
87 ms |
60428 KB |
Output is correct |
41 |
Correct |
98 ms |
59988 KB |
Output is correct |
42 |
Correct |
90 ms |
60736 KB |
Output is correct |
43 |
Correct |
58 ms |
61012 KB |
Output is correct |
44 |
Correct |
49 ms |
60240 KB |
Output is correct |
45 |
Correct |
89 ms |
62548 KB |
Output is correct |
46 |
Correct |
73 ms |
64336 KB |
Output is correct |
47 |
Correct |
129 ms |
66384 KB |
Output is correct |
48 |
Correct |
177 ms |
74580 KB |
Output is correct |
49 |
Correct |
166 ms |
74320 KB |
Output is correct |
50 |
Correct |
117 ms |
66936 KB |
Output is correct |
51 |
Correct |
126 ms |
67156 KB |
Output is correct |
52 |
Correct |
138 ms |
66900 KB |
Output is correct |
53 |
Correct |
149 ms |
66156 KB |
Output is correct |
54 |
Correct |
122 ms |
66268 KB |
Output is correct |
55 |
Correct |
18 ms |
52568 KB |
Output is correct |
56 |
Correct |
18 ms |
52568 KB |
Output is correct |
57 |
Correct |
63 ms |
59732 KB |
Output is correct |
58 |
Correct |
43 ms |
60096 KB |
Output is correct |
59 |
Correct |
74 ms |
64340 KB |
Output is correct |
60 |
Correct |
275 ms |
74352 KB |
Output is correct |
61 |
Correct |
127 ms |
66388 KB |
Output is correct |
62 |
Correct |
138 ms |
66892 KB |
Output is correct |
63 |
Correct |
169 ms |
66900 KB |
Output is correct |
64 |
Correct |
296 ms |
65440 KB |
Output is correct |
65 |
Correct |
140 ms |
67664 KB |
Output is correct |
66 |
Correct |
282 ms |
70740 KB |
Output is correct |
67 |
Correct |
83 ms |
67392 KB |
Output is correct |
68 |
Correct |
166 ms |
66896 KB |
Output is correct |
69 |
Correct |
177 ms |
64336 KB |
Output is correct |
70 |
Correct |
163 ms |
66644 KB |
Output is correct |