#include <bits/stdc++.h>
#include "race.h"
#define F first
#define S second
using namespace std;
const int N = 200005;
int n, k;
vector <int> g[N];
vector <pair <int, int>> gw[N]; /// just for simplicity in the code
int del[N], sub[N];
int res = N;
void calc(int v, int p = -1) {
sub[v] = 1;
for (int i : g[v]) {
if (i != p && del[i] == 0) {
calc(i, v);
sub[v] += sub[i];
}
}
}
int centroid(int v, int r, int p = -1) {
for (int i : g[v]) {
if (i != p && del[i] == 0 && sub[i] > r) {
return centroid(i, r, v);
}
}
return v;
}
map<int, int> dp;
vector <pair <int, int>> vec;
void dfs(int v, int p = -1, int cur = 0, int dis = 0) {
if (cur > k) return;
if (dp.count(k - cur)) {
res = min(res, dp[k - cur] + dis);
}
for (auto i : gw[v]) {
if (i.F != p && del[i.F] == 0) {
dfs(i.F, v, cur + i.S, dis + 1);
}
}
vec.push_back({cur, dis});
}
void decompose(int v, int p = -1) {
calc(v);
int c = centroid(v, sub[v] >> 1);
del[c] = 1;
dp.clear();
dp[0] = 0;
for (auto i : gw[c]) {
if (del[i.F] == 1) continue;
vec.clear();
dfs(i.F, c, i.S, 1);
for (auto j : vec) {
if (!dp.count(j.F)) dp[j.F] = j.S;
else dp[j.F] = min(dp[j.F], j.S);
}
}
for (int i : g[c]) {
if (i != p && del[i] == 0) {
decompose(i, c);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
g[H[i][0]].push_back(H[i][1]);
g[H[i][1]].push_back(H[i][0]);
gw[H[i][0]].push_back({H[i][1], L[i]});
gw[H[i][1]].push_back({H[i][0], L[i]});
}
decompose(0);
if (res > n) res = -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
8 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
6 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
8 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
6 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9728 KB |
Output is correct |
19 |
Correct |
8 ms |
9856 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
9 ms |
10112 KB |
Output is correct |
22 |
Correct |
7 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9856 KB |
Output is correct |
24 |
Correct |
8 ms |
9856 KB |
Output is correct |
25 |
Correct |
9 ms |
9856 KB |
Output is correct |
26 |
Correct |
8 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
9856 KB |
Output is correct |
28 |
Correct |
9 ms |
9856 KB |
Output is correct |
29 |
Correct |
8 ms |
9856 KB |
Output is correct |
30 |
Correct |
10 ms |
9856 KB |
Output is correct |
31 |
Correct |
9 ms |
9856 KB |
Output is correct |
32 |
Correct |
8 ms |
9856 KB |
Output is correct |
33 |
Correct |
9 ms |
9856 KB |
Output is correct |
34 |
Correct |
8 ms |
9856 KB |
Output is correct |
35 |
Correct |
10 ms |
9856 KB |
Output is correct |
36 |
Correct |
8 ms |
9856 KB |
Output is correct |
37 |
Correct |
8 ms |
9856 KB |
Output is correct |
38 |
Correct |
8 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
8 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
6 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9728 KB |
Output is correct |
19 |
Correct |
333 ms |
20044 KB |
Output is correct |
20 |
Correct |
288 ms |
19960 KB |
Output is correct |
21 |
Correct |
340 ms |
20212 KB |
Output is correct |
22 |
Correct |
322 ms |
20468 KB |
Output is correct |
23 |
Correct |
177 ms |
20220 KB |
Output is correct |
24 |
Correct |
166 ms |
20316 KB |
Output is correct |
25 |
Correct |
295 ms |
22780 KB |
Output is correct |
26 |
Correct |
165 ms |
26224 KB |
Output is correct |
27 |
Correct |
369 ms |
30968 KB |
Output is correct |
28 |
Correct |
622 ms |
41724 KB |
Output is correct |
29 |
Correct |
557 ms |
40956 KB |
Output is correct |
30 |
Correct |
382 ms |
30968 KB |
Output is correct |
31 |
Correct |
376 ms |
31096 KB |
Output is correct |
32 |
Correct |
455 ms |
31080 KB |
Output is correct |
33 |
Correct |
508 ms |
29432 KB |
Output is correct |
34 |
Correct |
363 ms |
30072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
6 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9728 KB |
Output is correct |
9 |
Correct |
6 ms |
9728 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
8 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
6 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9728 KB |
Output is correct |
19 |
Correct |
8 ms |
9856 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
9 ms |
10112 KB |
Output is correct |
22 |
Correct |
7 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9856 KB |
Output is correct |
24 |
Correct |
8 ms |
9856 KB |
Output is correct |
25 |
Correct |
9 ms |
9856 KB |
Output is correct |
26 |
Correct |
8 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
9856 KB |
Output is correct |
28 |
Correct |
9 ms |
9856 KB |
Output is correct |
29 |
Correct |
8 ms |
9856 KB |
Output is correct |
30 |
Correct |
10 ms |
9856 KB |
Output is correct |
31 |
Correct |
9 ms |
9856 KB |
Output is correct |
32 |
Correct |
8 ms |
9856 KB |
Output is correct |
33 |
Correct |
9 ms |
9856 KB |
Output is correct |
34 |
Correct |
8 ms |
9856 KB |
Output is correct |
35 |
Correct |
10 ms |
9856 KB |
Output is correct |
36 |
Correct |
8 ms |
9856 KB |
Output is correct |
37 |
Correct |
8 ms |
9856 KB |
Output is correct |
38 |
Correct |
8 ms |
9856 KB |
Output is correct |
39 |
Correct |
333 ms |
20044 KB |
Output is correct |
40 |
Correct |
288 ms |
19960 KB |
Output is correct |
41 |
Correct |
340 ms |
20212 KB |
Output is correct |
42 |
Correct |
322 ms |
20468 KB |
Output is correct |
43 |
Correct |
177 ms |
20220 KB |
Output is correct |
44 |
Correct |
166 ms |
20316 KB |
Output is correct |
45 |
Correct |
295 ms |
22780 KB |
Output is correct |
46 |
Correct |
165 ms |
26224 KB |
Output is correct |
47 |
Correct |
369 ms |
30968 KB |
Output is correct |
48 |
Correct |
622 ms |
41724 KB |
Output is correct |
49 |
Correct |
557 ms |
40956 KB |
Output is correct |
50 |
Correct |
382 ms |
30968 KB |
Output is correct |
51 |
Correct |
376 ms |
31096 KB |
Output is correct |
52 |
Correct |
455 ms |
31080 KB |
Output is correct |
53 |
Correct |
508 ms |
29432 KB |
Output is correct |
54 |
Correct |
363 ms |
30072 KB |
Output is correct |
55 |
Correct |
33 ms |
11008 KB |
Output is correct |
56 |
Correct |
23 ms |
10880 KB |
Output is correct |
57 |
Correct |
180 ms |
20464 KB |
Output is correct |
58 |
Correct |
72 ms |
20324 KB |
Output is correct |
59 |
Correct |
351 ms |
29408 KB |
Output is correct |
60 |
Correct |
1198 ms |
50412 KB |
Output is correct |
61 |
Correct |
469 ms |
31480 KB |
Output is correct |
62 |
Correct |
486 ms |
31312 KB |
Output is correct |
63 |
Correct |
604 ms |
31228 KB |
Output is correct |
64 |
Correct |
1350 ms |
36328 KB |
Output is correct |
65 |
Correct |
438 ms |
31480 KB |
Output is correct |
66 |
Correct |
909 ms |
39304 KB |
Output is correct |
67 |
Correct |
302 ms |
32088 KB |
Output is correct |
68 |
Correct |
697 ms |
35576 KB |
Output is correct |
69 |
Correct |
728 ms |
35576 KB |
Output is correct |
70 |
Correct |
608 ms |
34424 KB |
Output is correct |