#pragma GCC optimize ("Ofast")
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+1;
const ll inf = LLONG_MAX;
int n, k, sz[N];
ll ans;
vector<pair<int, ll>> adj[N];
bitset<N> r;
void dfs(int u, int par) {
sz[u] = 1;
for (auto &[v, w] : adj[u]) {
if (v == par || r[v]) continue;
dfs(v, u);
sz[u] += sz[v];
}
}
int find_centroid(int u, int par, int rt) {
for (auto &[v, w] : adj[u]) {
if (v != par && !r[v] && sz[v] > sz[rt]/2) {
return find_centroid(v, u, rt);
}
}
return u;
}
void dfs2(int u, int par, ll d, ll dw, map<ll, ll> &dis) {
if (dw > k || d >= ans) return;
auto it = dis.find(dw);
if (it == dis.end()) dis[dw] = d;
else it->second = min(it->second, d);
for (auto &[v, w] : adj[u]) {
if (v == par || r[v]) continue;
dfs2(v, u, d+1, dw+w, dis);
}
}
void solve_centroid(int u) {
map<ll, ll> mnD;
mnD[0] = 0;
for (auto &[v, w] : adj[u]) {
if (r[v]) continue;
map<ll, ll> mnD2;
dfs2(v, u, 1, w, mnD2);
if (mnD2.size() > mnD.size()) swap(mnD, mnD2);
for (auto &[dw, d] : mnD2) {
auto it = mnD.find(k-dw);
if (it == mnD.end()) continue;
ans = min(ans, d+it->second);
}
for (auto &[dw, d] : mnD2) {
auto it = mnD.find(dw);
if (it == mnD.end()) mnD[dw] = d;
else it->second = min(it->second, d);
}
}
}
void dec(int u) {
dfs(u, u);
int ct = find_centroid(u, u, u);
r[ct] = 1;
solve_centroid(ct);
for (auto &[v, w] : adj[ct]) {
if (r[v]) continue;
dec(v);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
ans = inf;
n = N;
k = K;
for (int i = 0; i < n-1; i++) {
adj[H[i][0]].push_back(make_pair(H[i][1], L[i]));
adj[H[i][1]].push_back(make_pair(H[i][0], L[i]));
}
dec(0);
return (ans == inf ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5012 KB |
Output is correct |
13 |
Correct |
5 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5012 KB |
Output is correct |
13 |
Correct |
5 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5152 KB |
Output is correct |
26 |
Correct |
4 ms |
5044 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5024 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5088 KB |
Output is correct |
37 |
Correct |
4 ms |
5144 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5012 KB |
Output is correct |
13 |
Correct |
5 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
150 ms |
13032 KB |
Output is correct |
20 |
Correct |
158 ms |
13140 KB |
Output is correct |
21 |
Correct |
174 ms |
13044 KB |
Output is correct |
22 |
Correct |
162 ms |
13024 KB |
Output is correct |
23 |
Correct |
69 ms |
13492 KB |
Output is correct |
24 |
Correct |
61 ms |
12716 KB |
Output is correct |
25 |
Correct |
109 ms |
14376 KB |
Output is correct |
26 |
Correct |
84 ms |
16404 KB |
Output is correct |
27 |
Correct |
198 ms |
21764 KB |
Output is correct |
28 |
Correct |
268 ms |
27200 KB |
Output is correct |
29 |
Correct |
278 ms |
26648 KB |
Output is correct |
30 |
Correct |
200 ms |
21708 KB |
Output is correct |
31 |
Correct |
204 ms |
21824 KB |
Output is correct |
32 |
Correct |
239 ms |
21864 KB |
Output is correct |
33 |
Correct |
278 ms |
20372 KB |
Output is correct |
34 |
Correct |
166 ms |
21324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5012 KB |
Output is correct |
13 |
Correct |
5 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5152 KB |
Output is correct |
26 |
Correct |
4 ms |
5044 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5024 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5088 KB |
Output is correct |
37 |
Correct |
4 ms |
5144 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
150 ms |
13032 KB |
Output is correct |
40 |
Correct |
158 ms |
13140 KB |
Output is correct |
41 |
Correct |
174 ms |
13044 KB |
Output is correct |
42 |
Correct |
162 ms |
13024 KB |
Output is correct |
43 |
Correct |
69 ms |
13492 KB |
Output is correct |
44 |
Correct |
61 ms |
12716 KB |
Output is correct |
45 |
Correct |
109 ms |
14376 KB |
Output is correct |
46 |
Correct |
84 ms |
16404 KB |
Output is correct |
47 |
Correct |
198 ms |
21764 KB |
Output is correct |
48 |
Correct |
268 ms |
27200 KB |
Output is correct |
49 |
Correct |
278 ms |
26648 KB |
Output is correct |
50 |
Correct |
200 ms |
21708 KB |
Output is correct |
51 |
Correct |
204 ms |
21824 KB |
Output is correct |
52 |
Correct |
239 ms |
21864 KB |
Output is correct |
53 |
Correct |
278 ms |
20372 KB |
Output is correct |
54 |
Correct |
166 ms |
21324 KB |
Output is correct |
55 |
Correct |
19 ms |
6016 KB |
Output is correct |
56 |
Correct |
17 ms |
5804 KB |
Output is correct |
57 |
Correct |
113 ms |
13316 KB |
Output is correct |
58 |
Correct |
43 ms |
12248 KB |
Output is correct |
59 |
Correct |
367 ms |
24360 KB |
Output is correct |
60 |
Correct |
954 ms |
46040 KB |
Output is correct |
61 |
Correct |
267 ms |
22084 KB |
Output is correct |
62 |
Correct |
281 ms |
22112 KB |
Output is correct |
63 |
Correct |
356 ms |
22104 KB |
Output is correct |
64 |
Correct |
1034 ms |
30840 KB |
Output is correct |
65 |
Correct |
236 ms |
22284 KB |
Output is correct |
66 |
Correct |
623 ms |
26312 KB |
Output is correct |
67 |
Correct |
137 ms |
20924 KB |
Output is correct |
68 |
Correct |
201 ms |
22348 KB |
Output is correct |
69 |
Correct |
202 ms |
22084 KB |
Output is correct |
70 |
Correct |
223 ms |
21552 KB |
Output is correct |