#pragma GCC optimize ("Ofast")
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define map unordered_map
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 |
4948 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 |
5032 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 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 |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 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 |
4948 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 |
5032 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 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 |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 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 |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
6 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5096 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5028 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5092 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 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 |
4948 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 |
5032 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 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 |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
143 ms |
11624 KB |
Output is correct |
20 |
Correct |
145 ms |
11736 KB |
Output is correct |
21 |
Correct |
136 ms |
11596 KB |
Output is correct |
22 |
Correct |
142 ms |
11824 KB |
Output is correct |
23 |
Correct |
78 ms |
12016 KB |
Output is correct |
24 |
Correct |
73 ms |
11276 KB |
Output is correct |
25 |
Correct |
124 ms |
12920 KB |
Output is correct |
26 |
Correct |
97 ms |
15180 KB |
Output is correct |
27 |
Correct |
206 ms |
18820 KB |
Output is correct |
28 |
Correct |
267 ms |
24200 KB |
Output is correct |
29 |
Correct |
262 ms |
23524 KB |
Output is correct |
30 |
Correct |
223 ms |
18760 KB |
Output is correct |
31 |
Correct |
223 ms |
18944 KB |
Output is correct |
32 |
Correct |
233 ms |
18860 KB |
Output is correct |
33 |
Correct |
277 ms |
17588 KB |
Output is correct |
34 |
Correct |
217 ms |
17596 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 |
4948 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 |
5032 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 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 |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 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 |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
6 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5096 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5028 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5092 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
143 ms |
11624 KB |
Output is correct |
40 |
Correct |
145 ms |
11736 KB |
Output is correct |
41 |
Correct |
136 ms |
11596 KB |
Output is correct |
42 |
Correct |
142 ms |
11824 KB |
Output is correct |
43 |
Correct |
78 ms |
12016 KB |
Output is correct |
44 |
Correct |
73 ms |
11276 KB |
Output is correct |
45 |
Correct |
124 ms |
12920 KB |
Output is correct |
46 |
Correct |
97 ms |
15180 KB |
Output is correct |
47 |
Correct |
206 ms |
18820 KB |
Output is correct |
48 |
Correct |
267 ms |
24200 KB |
Output is correct |
49 |
Correct |
262 ms |
23524 KB |
Output is correct |
50 |
Correct |
223 ms |
18760 KB |
Output is correct |
51 |
Correct |
223 ms |
18944 KB |
Output is correct |
52 |
Correct |
233 ms |
18860 KB |
Output is correct |
53 |
Correct |
277 ms |
17588 KB |
Output is correct |
54 |
Correct |
217 ms |
17596 KB |
Output is correct |
55 |
Correct |
15 ms |
5844 KB |
Output is correct |
56 |
Correct |
15 ms |
5588 KB |
Output is correct |
57 |
Correct |
99 ms |
11980 KB |
Output is correct |
58 |
Correct |
47 ms |
11216 KB |
Output is correct |
59 |
Correct |
230 ms |
20364 KB |
Output is correct |
60 |
Correct |
747 ms |
39340 KB |
Output is correct |
61 |
Correct |
282 ms |
18932 KB |
Output is correct |
62 |
Correct |
268 ms |
18868 KB |
Output is correct |
63 |
Correct |
324 ms |
18948 KB |
Output is correct |
64 |
Correct |
668 ms |
25188 KB |
Output is correct |
65 |
Correct |
253 ms |
18420 KB |
Output is correct |
66 |
Correct |
623 ms |
22816 KB |
Output is correct |
67 |
Correct |
177 ms |
17632 KB |
Output is correct |
68 |
Correct |
231 ms |
18720 KB |
Output is correct |
69 |
Correct |
214 ms |
18328 KB |
Output is correct |
70 |
Correct |
214 ms |
18084 KB |
Output is correct |