#include <bits/stdc++.h>
#define eb emplace_back
#define Size(v) (int)v.size()
using namespace std;
// Always
const int nax = 2e5 + 5, INF = 1e9 + 7;
int n, k;
vector<pair<int, int>> adj[nax];
unordered_map<int, int> vec[nax];
int dist[nax], ans[nax], sz[nax], dep[nax];
void getsz(int v, int p = 0) {
sz[v] = 1;
for (auto &q : adj[v]) {
int c = q.first, w = q.second;
if (c == p) continue;
dep[c] = dep[v] + 1;
dist[c] = dist[v] + w;
getsz(c, v);
sz[v] += sz[c];
}
}
void dfs(int v, int p = 0) {
int maxi = -1, bigchild = -1;
for (auto &q : adj[v]) {
int u = q.first;
if (u == p) continue;
if (maxi < sz[u]) {
maxi = sz[u];
bigchild = u;
}
}
for (auto &q : adj[v]) {
int u = q.first;
if (u == p || u == bigchild) continue;
dfs(u, v);
}
if (bigchild != -1) {
dfs(bigchild, v);
swap(vec[v], vec[bigchild]);
}
vec[v][dist[v]] = dep[v];
int subtract = dist[v];
if (vec[v].find(k + dist[v]) != vec[v].end()) {
ans[v] = min(ans[v], vec[v][k + dist[v]] - dep[v]);
}
int target = k + 2 * subtract;
for (auto &q : adj[v]) {
int c = q.first;
if (c == p || c == bigchild) continue;
if (vec[c].find(k + dist[v]) != vec[c].end()) {
ans[v] = min(ans[v], vec[c][k + dist[v]] - dep[v]);
}
for (auto &u : vec[c]) {
if (vec[v].find(target - u.first) != vec[v].end()) {
ans[v] = min(ans[v], u.second + vec[v][target - u.first] - 2 * dep[v]);
}
}
for (auto &u : vec[c]) {
if (vec[v].find(u.first) != vec[v].end()) vec[v][u.first] = min(vec[v][u.first], u.second);
else vec[v][u.first] = u.second;
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
fill(ans, ans + nax, INF);
n = N;
k = K;
for (int i = 0; i < n - 1; ++i) {
int u = H[i][0], v = H[i][1];
++u, ++v;
adj[u].eb(v, L[i]);
adj[v].eb(u, L[i]);
}
getsz(1);
dfs(1);
int res = INF;
for (int i = 1; i <= n; ++i) res = min(res, ans[i]);
if (res == INF) return -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
12 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
12 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16768 KB |
Output is correct |
17 |
Correct |
11 ms |
16896 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
12 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
12 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16768 KB |
Output is correct |
17 |
Correct |
11 ms |
16896 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
20 |
Correct |
12 ms |
16768 KB |
Output is correct |
21 |
Correct |
13 ms |
16896 KB |
Output is correct |
22 |
Correct |
13 ms |
17024 KB |
Output is correct |
23 |
Correct |
12 ms |
17024 KB |
Output is correct |
24 |
Correct |
12 ms |
17024 KB |
Output is correct |
25 |
Correct |
12 ms |
17024 KB |
Output is correct |
26 |
Correct |
12 ms |
17024 KB |
Output is correct |
27 |
Correct |
13 ms |
16896 KB |
Output is correct |
28 |
Correct |
13 ms |
17024 KB |
Output is correct |
29 |
Correct |
12 ms |
17024 KB |
Output is correct |
30 |
Correct |
12 ms |
17008 KB |
Output is correct |
31 |
Correct |
12 ms |
17024 KB |
Output is correct |
32 |
Correct |
13 ms |
17024 KB |
Output is correct |
33 |
Correct |
13 ms |
17024 KB |
Output is correct |
34 |
Correct |
12 ms |
16896 KB |
Output is correct |
35 |
Correct |
12 ms |
16896 KB |
Output is correct |
36 |
Correct |
12 ms |
16896 KB |
Output is correct |
37 |
Correct |
12 ms |
16896 KB |
Output is correct |
38 |
Correct |
13 ms |
17024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
12 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
12 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16768 KB |
Output is correct |
17 |
Correct |
11 ms |
16896 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
19 |
Correct |
146 ms |
30968 KB |
Output is correct |
20 |
Correct |
134 ms |
30972 KB |
Output is correct |
21 |
Correct |
139 ms |
30964 KB |
Output is correct |
22 |
Correct |
141 ms |
31224 KB |
Output is correct |
23 |
Correct |
166 ms |
40696 KB |
Output is correct |
24 |
Correct |
145 ms |
36312 KB |
Output is correct |
25 |
Correct |
97 ms |
29304 KB |
Output is correct |
26 |
Correct |
65 ms |
34808 KB |
Output is correct |
27 |
Correct |
246 ms |
39800 KB |
Output is correct |
28 |
Correct |
299 ms |
60992 KB |
Output is correct |
29 |
Correct |
301 ms |
59840 KB |
Output is correct |
30 |
Correct |
238 ms |
39800 KB |
Output is correct |
31 |
Correct |
240 ms |
39800 KB |
Output is correct |
32 |
Correct |
311 ms |
39800 KB |
Output is correct |
33 |
Correct |
227 ms |
36728 KB |
Output is correct |
34 |
Correct |
375 ms |
58048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
12 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
12 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16768 KB |
Output is correct |
17 |
Correct |
11 ms |
16896 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
20 |
Correct |
12 ms |
16768 KB |
Output is correct |
21 |
Correct |
13 ms |
16896 KB |
Output is correct |
22 |
Correct |
13 ms |
17024 KB |
Output is correct |
23 |
Correct |
12 ms |
17024 KB |
Output is correct |
24 |
Correct |
12 ms |
17024 KB |
Output is correct |
25 |
Correct |
12 ms |
17024 KB |
Output is correct |
26 |
Correct |
12 ms |
17024 KB |
Output is correct |
27 |
Correct |
13 ms |
16896 KB |
Output is correct |
28 |
Correct |
13 ms |
17024 KB |
Output is correct |
29 |
Correct |
12 ms |
17024 KB |
Output is correct |
30 |
Correct |
12 ms |
17008 KB |
Output is correct |
31 |
Correct |
12 ms |
17024 KB |
Output is correct |
32 |
Correct |
13 ms |
17024 KB |
Output is correct |
33 |
Correct |
13 ms |
17024 KB |
Output is correct |
34 |
Correct |
12 ms |
16896 KB |
Output is correct |
35 |
Correct |
12 ms |
16896 KB |
Output is correct |
36 |
Correct |
12 ms |
16896 KB |
Output is correct |
37 |
Correct |
12 ms |
16896 KB |
Output is correct |
38 |
Correct |
13 ms |
17024 KB |
Output is correct |
39 |
Correct |
146 ms |
30968 KB |
Output is correct |
40 |
Correct |
134 ms |
30972 KB |
Output is correct |
41 |
Correct |
139 ms |
30964 KB |
Output is correct |
42 |
Correct |
141 ms |
31224 KB |
Output is correct |
43 |
Correct |
166 ms |
40696 KB |
Output is correct |
44 |
Correct |
145 ms |
36312 KB |
Output is correct |
45 |
Correct |
97 ms |
29304 KB |
Output is correct |
46 |
Correct |
65 ms |
34808 KB |
Output is correct |
47 |
Correct |
246 ms |
39800 KB |
Output is correct |
48 |
Correct |
299 ms |
60992 KB |
Output is correct |
49 |
Correct |
301 ms |
59840 KB |
Output is correct |
50 |
Correct |
238 ms |
39800 KB |
Output is correct |
51 |
Correct |
240 ms |
39800 KB |
Output is correct |
52 |
Correct |
311 ms |
39800 KB |
Output is correct |
53 |
Correct |
227 ms |
36728 KB |
Output is correct |
54 |
Correct |
375 ms |
58048 KB |
Output is correct |
55 |
Correct |
24 ms |
18944 KB |
Output is correct |
56 |
Correct |
20 ms |
18048 KB |
Output is correct |
57 |
Correct |
92 ms |
30328 KB |
Output is correct |
58 |
Correct |
74 ms |
29292 KB |
Output is correct |
59 |
Correct |
78 ms |
38940 KB |
Output is correct |
60 |
Correct |
268 ms |
60220 KB |
Output is correct |
61 |
Correct |
260 ms |
41848 KB |
Output is correct |
62 |
Correct |
246 ms |
39672 KB |
Output is correct |
63 |
Correct |
310 ms |
39800 KB |
Output is correct |
64 |
Correct |
487 ms |
73116 KB |
Output is correct |
65 |
Correct |
512 ms |
73668 KB |
Output is correct |
66 |
Correct |
308 ms |
57024 KB |
Output is correct |
67 |
Correct |
251 ms |
41700 KB |
Output is correct |
68 |
Correct |
385 ms |
53056 KB |
Output is correct |
69 |
Correct |
391 ms |
53436 KB |
Output is correct |
70 |
Correct |
368 ms |
51644 KB |
Output is correct |