#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define sep ' '
#define debug(x) cerr << #x << ": " << x << endl;
const ll INF = 1e9;
const ll MAXN = 1e6 + 10;
int n, k, ans, sz[MAXN];
vector<pll> adj[MAXN];
bool B[MAXN];
map<ll, int> mp;
vector<pll> vec;
int sub_sz(int v, int p) {
sz[v] = 1;
for (auto [u, w] : adj[v])
if (!B[u] && u != p)
sz[v] += sub_sz(u, v);
return sz[v];
}
int centroid(int v, int p, int n) {
for (auto [u, w] : adj[v])
if (!B[u] && u != p && sz[u] * 2 > n)
return centroid(u, v, n);
return v;
}
void dfs(int v, int p, ll w, int l) {
vec.push_back({w, l});
auto it = mp.find(k - w);
if (it != mp.end()) ans = min(ans, l + it -> Y);
for (auto [u, e_w] : adj[v])
if (u != p && !B[u])
dfs(u, v, w + e_w, l + 1);
}
void solve(int v) {
B[v = centroid(v, 0, sub_sz(v, 0))] = true;
mp.clear();
mp[0] = 0;
for (auto [u, w] : adj[v]) {
if (!B[u]) {
dfs(u, v, w, 1);
for (pll e : vec) {
auto it = mp.find(e.X);
if (it == mp.end() || it -> Y > e.Y)
mp[e.X] = e.Y;
}
vec.clear();
}
}
for (auto [u, w] : adj[v])
if (!B[u])
solve(u);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
int u = H[i][0], v = H[i][1], l = L[i];
u++, v++;
adj[u].push_back({v, l});
adj[v].push_back({u, l});
}
ans = INF;
solve(1);
return (ans == INF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23728 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
14 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23764 KB |
Output is correct |
16 |
Correct |
15 ms |
23748 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
16 ms |
23740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23728 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
14 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23764 KB |
Output is correct |
16 |
Correct |
15 ms |
23748 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
16 ms |
23740 KB |
Output is correct |
19 |
Correct |
16 ms |
23856 KB |
Output is correct |
20 |
Correct |
15 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23892 KB |
Output is correct |
22 |
Correct |
15 ms |
23892 KB |
Output is correct |
23 |
Correct |
16 ms |
23912 KB |
Output is correct |
24 |
Correct |
15 ms |
23908 KB |
Output is correct |
25 |
Correct |
15 ms |
23968 KB |
Output is correct |
26 |
Correct |
14 ms |
23944 KB |
Output is correct |
27 |
Correct |
14 ms |
23816 KB |
Output is correct |
28 |
Correct |
14 ms |
23928 KB |
Output is correct |
29 |
Correct |
14 ms |
23868 KB |
Output is correct |
30 |
Correct |
14 ms |
23892 KB |
Output is correct |
31 |
Correct |
14 ms |
23856 KB |
Output is correct |
32 |
Correct |
14 ms |
23956 KB |
Output is correct |
33 |
Correct |
15 ms |
23892 KB |
Output is correct |
34 |
Correct |
15 ms |
23892 KB |
Output is correct |
35 |
Correct |
18 ms |
24012 KB |
Output is correct |
36 |
Correct |
19 ms |
23892 KB |
Output is correct |
37 |
Correct |
15 ms |
23892 KB |
Output is correct |
38 |
Correct |
14 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23728 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
14 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23764 KB |
Output is correct |
16 |
Correct |
15 ms |
23748 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
16 ms |
23740 KB |
Output is correct |
19 |
Correct |
202 ms |
31724 KB |
Output is correct |
20 |
Correct |
225 ms |
31804 KB |
Output is correct |
21 |
Correct |
211 ms |
31748 KB |
Output is correct |
22 |
Correct |
184 ms |
31856 KB |
Output is correct |
23 |
Correct |
249 ms |
32108 KB |
Output is correct |
24 |
Correct |
110 ms |
30800 KB |
Output is correct |
25 |
Correct |
185 ms |
36140 KB |
Output is correct |
26 |
Correct |
111 ms |
37656 KB |
Output is correct |
27 |
Correct |
260 ms |
38540 KB |
Output is correct |
28 |
Correct |
884 ms |
63352 KB |
Output is correct |
29 |
Correct |
893 ms |
62408 KB |
Output is correct |
30 |
Correct |
294 ms |
38416 KB |
Output is correct |
31 |
Correct |
277 ms |
38512 KB |
Output is correct |
32 |
Correct |
379 ms |
38492 KB |
Output is correct |
33 |
Correct |
391 ms |
39036 KB |
Output is correct |
34 |
Correct |
834 ms |
50716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23728 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
14 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23764 KB |
Output is correct |
16 |
Correct |
15 ms |
23748 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
16 ms |
23740 KB |
Output is correct |
19 |
Correct |
16 ms |
23856 KB |
Output is correct |
20 |
Correct |
15 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23892 KB |
Output is correct |
22 |
Correct |
15 ms |
23892 KB |
Output is correct |
23 |
Correct |
16 ms |
23912 KB |
Output is correct |
24 |
Correct |
15 ms |
23908 KB |
Output is correct |
25 |
Correct |
15 ms |
23968 KB |
Output is correct |
26 |
Correct |
14 ms |
23944 KB |
Output is correct |
27 |
Correct |
14 ms |
23816 KB |
Output is correct |
28 |
Correct |
14 ms |
23928 KB |
Output is correct |
29 |
Correct |
14 ms |
23868 KB |
Output is correct |
30 |
Correct |
14 ms |
23892 KB |
Output is correct |
31 |
Correct |
14 ms |
23856 KB |
Output is correct |
32 |
Correct |
14 ms |
23956 KB |
Output is correct |
33 |
Correct |
15 ms |
23892 KB |
Output is correct |
34 |
Correct |
15 ms |
23892 KB |
Output is correct |
35 |
Correct |
18 ms |
24012 KB |
Output is correct |
36 |
Correct |
19 ms |
23892 KB |
Output is correct |
37 |
Correct |
15 ms |
23892 KB |
Output is correct |
38 |
Correct |
14 ms |
23892 KB |
Output is correct |
39 |
Correct |
202 ms |
31724 KB |
Output is correct |
40 |
Correct |
225 ms |
31804 KB |
Output is correct |
41 |
Correct |
211 ms |
31748 KB |
Output is correct |
42 |
Correct |
184 ms |
31856 KB |
Output is correct |
43 |
Correct |
249 ms |
32108 KB |
Output is correct |
44 |
Correct |
110 ms |
30800 KB |
Output is correct |
45 |
Correct |
185 ms |
36140 KB |
Output is correct |
46 |
Correct |
111 ms |
37656 KB |
Output is correct |
47 |
Correct |
260 ms |
38540 KB |
Output is correct |
48 |
Correct |
884 ms |
63352 KB |
Output is correct |
49 |
Correct |
893 ms |
62408 KB |
Output is correct |
50 |
Correct |
294 ms |
38416 KB |
Output is correct |
51 |
Correct |
277 ms |
38512 KB |
Output is correct |
52 |
Correct |
379 ms |
38492 KB |
Output is correct |
53 |
Correct |
391 ms |
39036 KB |
Output is correct |
54 |
Correct |
834 ms |
50716 KB |
Output is correct |
55 |
Correct |
38 ms |
24916 KB |
Output is correct |
56 |
Correct |
28 ms |
24960 KB |
Output is correct |
57 |
Correct |
129 ms |
32224 KB |
Output is correct |
58 |
Correct |
52 ms |
30304 KB |
Output is correct |
59 |
Correct |
272 ms |
42096 KB |
Output is correct |
60 |
Correct |
898 ms |
61808 KB |
Output is correct |
61 |
Correct |
328 ms |
40096 KB |
Output is correct |
62 |
Correct |
306 ms |
38504 KB |
Output is correct |
63 |
Correct |
331 ms |
38356 KB |
Output is correct |
64 |
Correct |
920 ms |
45208 KB |
Output is correct |
65 |
Correct |
900 ms |
51508 KB |
Output is correct |
66 |
Correct |
901 ms |
60340 KB |
Output is correct |
67 |
Correct |
143 ms |
36912 KB |
Output is correct |
68 |
Correct |
511 ms |
49824 KB |
Output is correct |
69 |
Correct |
485 ms |
49784 KB |
Output is correct |
70 |
Correct |
448 ms |
48720 KB |
Output is correct |