#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int oo = 1e9;
typedef pair < int, int > II;
#define z id[j]
int sz[MAXN], depth[MAXN];
long long sum[MAXN];
vector < II > adj[MAXN];
vector < int > vec[MAXN];
map < long long, int > path;
int h[MAXN][2], l[MAXN], big[MAXN];
int n, k;
int ans = oo, cnt = 0;
int in[MAXN], out[MAXN], id[MAXN];
void dfs(int u, int p) {
sz[u] = 1;
in[u] = ++cnt;
id[cnt] = u;
for(auto cur : adj[u]) {
int v = cur.first, w = cur.second;
if (v == p)
continue;
depth[v] = depth[u] + 1;
sum[v] = sum[u] + w;
dfs(v, u);
sz[u] += sz[v];
}
out[u] = cnt;
}
void modify(int u, int k) {
long long sumNow = sum[u];
if (k == -1) {
path[sumNow] = 0;
} else {
int tmp = path[sumNow];
if (tmp == 0)
tmp = oo;
path[sumNow] = min(tmp, depth[u]);
}
}
void sack(int u, int p, int keep) {
int szMax = -1, bigChild = -1;
for(auto cur : adj[u]) {
int v = cur.first;
if (v == p)
continue;
if (sz[v] > szMax) {
sz[v] = szMax;
bigChild = v;
}
}
for(auto cur : adj[u]) {
int v = cur.first;
if (v != bigChild && v != p)
sack(v, u, 0);
}
if (bigChild != -1) {
sack(bigChild, u, 1);
big[bigChild] = 1;
}
for(auto cur : adj[u]) {
int v = cur.first;
if (v == p || v == bigChild)
continue;
for(int j = in[v]; j <= out[v]; j++) {
long long need = k + sum[u] * 2 - sum[z];
if (path[need])
ans = min(ans, path[need] + depth[z] - 2 * depth[u]);
}
for(int j = in[v]; j <= out[v]; j++) {
modify(z, 1);
}
}
modify(u, 1);
if (path[k + sum[u]])
ans = min(ans, path[k + sum[u]] - depth[u]);
if (bigChild != -1)
big[bigChild] = 0;
if (!keep) {
for(int j = in[u]; j <= out[u]; j++) {
modify(z, -1);
}
}
}
int solve() {
dfs(0, -1);
sack(0, -1, 1);
return (ans == oo ? -1 : ans);
}
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], w = L[i];
adj[u].push_back(II(v, w));
adj[v].push_back(II(u, w));
}
return solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
9 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9856 KB |
Output is correct |
8 |
Correct |
9 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9776 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
9 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
9 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
9 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
9 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9856 KB |
Output is correct |
8 |
Correct |
9 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9776 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
9 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
9 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
9 ms |
9856 KB |
Output is correct |
19 |
Correct |
9 ms |
9728 KB |
Output is correct |
20 |
Correct |
9 ms |
9728 KB |
Output is correct |
21 |
Correct |
13 ms |
9984 KB |
Output is correct |
22 |
Correct |
14 ms |
10496 KB |
Output is correct |
23 |
Correct |
20 ms |
11136 KB |
Output is correct |
24 |
Correct |
15 ms |
10496 KB |
Output is correct |
25 |
Correct |
14 ms |
10496 KB |
Output is correct |
26 |
Correct |
14 ms |
10368 KB |
Output is correct |
27 |
Correct |
10 ms |
9856 KB |
Output is correct |
28 |
Correct |
19 ms |
10624 KB |
Output is correct |
29 |
Correct |
15 ms |
10368 KB |
Output is correct |
30 |
Correct |
14 ms |
10368 KB |
Output is correct |
31 |
Correct |
15 ms |
10368 KB |
Output is correct |
32 |
Correct |
15 ms |
10536 KB |
Output is correct |
33 |
Correct |
16 ms |
10496 KB |
Output is correct |
34 |
Correct |
13 ms |
10240 KB |
Output is correct |
35 |
Correct |
12 ms |
10240 KB |
Output is correct |
36 |
Correct |
11 ms |
10112 KB |
Output is correct |
37 |
Correct |
11 ms |
9984 KB |
Output is correct |
38 |
Correct |
12 ms |
10112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
9 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9856 KB |
Output is correct |
8 |
Correct |
9 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9776 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
9 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
9 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
9 ms |
9856 KB |
Output is correct |
19 |
Correct |
384 ms |
17912 KB |
Output is correct |
20 |
Correct |
322 ms |
17784 KB |
Output is correct |
21 |
Correct |
297 ms |
17784 KB |
Output is correct |
22 |
Correct |
371 ms |
17880 KB |
Output is correct |
23 |
Correct |
627 ms |
18168 KB |
Output is correct |
24 |
Correct |
314 ms |
17912 KB |
Output is correct |
25 |
Correct |
103 ms |
24440 KB |
Output is correct |
26 |
Correct |
72 ms |
29688 KB |
Output is correct |
27 |
Correct |
251 ms |
26360 KB |
Output is correct |
28 |
Correct |
367 ms |
87032 KB |
Output is correct |
29 |
Correct |
296 ms |
73596 KB |
Output is correct |
30 |
Correct |
253 ms |
26232 KB |
Output is correct |
31 |
Correct |
258 ms |
26360 KB |
Output is correct |
32 |
Correct |
366 ms |
26872 KB |
Output is correct |
33 |
Correct |
1042 ms |
25424 KB |
Output is correct |
34 |
Execution timed out |
3091 ms |
142664 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
9 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9856 KB |
Output is correct |
8 |
Correct |
9 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9776 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
9 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
9 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
9 ms |
9856 KB |
Output is correct |
19 |
Correct |
9 ms |
9728 KB |
Output is correct |
20 |
Correct |
9 ms |
9728 KB |
Output is correct |
21 |
Correct |
13 ms |
9984 KB |
Output is correct |
22 |
Correct |
14 ms |
10496 KB |
Output is correct |
23 |
Correct |
20 ms |
11136 KB |
Output is correct |
24 |
Correct |
15 ms |
10496 KB |
Output is correct |
25 |
Correct |
14 ms |
10496 KB |
Output is correct |
26 |
Correct |
14 ms |
10368 KB |
Output is correct |
27 |
Correct |
10 ms |
9856 KB |
Output is correct |
28 |
Correct |
19 ms |
10624 KB |
Output is correct |
29 |
Correct |
15 ms |
10368 KB |
Output is correct |
30 |
Correct |
14 ms |
10368 KB |
Output is correct |
31 |
Correct |
15 ms |
10368 KB |
Output is correct |
32 |
Correct |
15 ms |
10536 KB |
Output is correct |
33 |
Correct |
16 ms |
10496 KB |
Output is correct |
34 |
Correct |
13 ms |
10240 KB |
Output is correct |
35 |
Correct |
12 ms |
10240 KB |
Output is correct |
36 |
Correct |
11 ms |
10112 KB |
Output is correct |
37 |
Correct |
11 ms |
9984 KB |
Output is correct |
38 |
Correct |
12 ms |
10112 KB |
Output is correct |
39 |
Correct |
384 ms |
17912 KB |
Output is correct |
40 |
Correct |
322 ms |
17784 KB |
Output is correct |
41 |
Correct |
297 ms |
17784 KB |
Output is correct |
42 |
Correct |
371 ms |
17880 KB |
Output is correct |
43 |
Correct |
627 ms |
18168 KB |
Output is correct |
44 |
Correct |
314 ms |
17912 KB |
Output is correct |
45 |
Correct |
103 ms |
24440 KB |
Output is correct |
46 |
Correct |
72 ms |
29688 KB |
Output is correct |
47 |
Correct |
251 ms |
26360 KB |
Output is correct |
48 |
Correct |
367 ms |
87032 KB |
Output is correct |
49 |
Correct |
296 ms |
73596 KB |
Output is correct |
50 |
Correct |
253 ms |
26232 KB |
Output is correct |
51 |
Correct |
258 ms |
26360 KB |
Output is correct |
52 |
Correct |
366 ms |
26872 KB |
Output is correct |
53 |
Correct |
1042 ms |
25424 KB |
Output is correct |
54 |
Execution timed out |
3091 ms |
142664 KB |
Time limit exceeded |