#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, k, ans = -1;
int sz[maxn], mark[maxn], cnt[maxn];
vector<pair<int,int>> adj[maxn];
void get_size(int x, int p)
{
sz[x] = 1;
for (auto [i, j] : adj[x]) if (i != p && !mark[i])
get_size(i, x),
sz[x] += sz[i];
}
int find(int x, int p, int half)
{
for (auto [i, j] : adj[x]) if (i != p && !mark[i] && sz[i] > half)
return find(i, x, half);
return x;
}
void dfs(int x, int p, int h, int d, int t)
{
if (h > k) return;
if (t == 0)
{
if (cnt[k - h] != -1)
{
if (ans == -1)
ans = d + cnt[k - h];
else ans = min(ans, d + cnt[k - h]);
}
} else if (cnt[h] == -1) cnt[h] = d; else cnt[h] = min(cnt[h], d);
for (auto [i, j] : adj[x]) if (i != p && !mark[i])
dfs(i, x, h + j, d + 1, t);
}
void reset(int x, int p, int h)
{
if (h > k) return;
cnt[h] = -1;
for (auto [i, j] : adj[x])if (i != p && !mark[i])
reset(i, x, h + j);
}
void solve(int x = 1)
{
get_size(x, x);
x = find(x, x, sz[x] / 2);
mark[x] = 1;
cnt[0] = 0;
for (auto [i, j] : adj[x]) if (!mark[i])
dfs(i, x, j, 1, 0),
dfs(i, x, j, 1, 1);
reset(x, x, 0);
for (auto [i, j] : adj[x]) if (!mark[i])
solve(i);
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N, k = K;
memset(cnt, -1, sizeof cnt);
for(int i = 0; i < n - 1; i++){
int u = H[i][0] + 1, v = H[i][1] + 1, w = L[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
solve();
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35152 KB |
Output is correct |
2 |
Correct |
6 ms |
35284 KB |
Output is correct |
3 |
Correct |
7 ms |
35152 KB |
Output is correct |
4 |
Correct |
6 ms |
35152 KB |
Output is correct |
5 |
Correct |
7 ms |
35152 KB |
Output is correct |
6 |
Correct |
7 ms |
35320 KB |
Output is correct |
7 |
Correct |
6 ms |
35152 KB |
Output is correct |
8 |
Correct |
6 ms |
35292 KB |
Output is correct |
9 |
Correct |
6 ms |
35152 KB |
Output is correct |
10 |
Correct |
6 ms |
35320 KB |
Output is correct |
11 |
Correct |
6 ms |
35152 KB |
Output is correct |
12 |
Correct |
6 ms |
35152 KB |
Output is correct |
13 |
Correct |
7 ms |
35152 KB |
Output is correct |
14 |
Correct |
6 ms |
35152 KB |
Output is correct |
15 |
Correct |
7 ms |
35152 KB |
Output is correct |
16 |
Correct |
7 ms |
35152 KB |
Output is correct |
17 |
Correct |
8 ms |
35408 KB |
Output is correct |
18 |
Correct |
6 ms |
35152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35152 KB |
Output is correct |
2 |
Correct |
6 ms |
35284 KB |
Output is correct |
3 |
Correct |
7 ms |
35152 KB |
Output is correct |
4 |
Correct |
6 ms |
35152 KB |
Output is correct |
5 |
Correct |
7 ms |
35152 KB |
Output is correct |
6 |
Correct |
7 ms |
35320 KB |
Output is correct |
7 |
Correct |
6 ms |
35152 KB |
Output is correct |
8 |
Correct |
6 ms |
35292 KB |
Output is correct |
9 |
Correct |
6 ms |
35152 KB |
Output is correct |
10 |
Correct |
6 ms |
35320 KB |
Output is correct |
11 |
Correct |
6 ms |
35152 KB |
Output is correct |
12 |
Correct |
6 ms |
35152 KB |
Output is correct |
13 |
Correct |
7 ms |
35152 KB |
Output is correct |
14 |
Correct |
6 ms |
35152 KB |
Output is correct |
15 |
Correct |
7 ms |
35152 KB |
Output is correct |
16 |
Correct |
7 ms |
35152 KB |
Output is correct |
17 |
Correct |
8 ms |
35408 KB |
Output is correct |
18 |
Correct |
6 ms |
35152 KB |
Output is correct |
19 |
Correct |
7 ms |
35152 KB |
Output is correct |
20 |
Correct |
5 ms |
35152 KB |
Output is correct |
21 |
Correct |
6 ms |
35380 KB |
Output is correct |
22 |
Correct |
6 ms |
35152 KB |
Output is correct |
23 |
Correct |
6 ms |
35152 KB |
Output is correct |
24 |
Correct |
6 ms |
35152 KB |
Output is correct |
25 |
Correct |
6 ms |
35152 KB |
Output is correct |
26 |
Correct |
6 ms |
35152 KB |
Output is correct |
27 |
Correct |
6 ms |
35152 KB |
Output is correct |
28 |
Correct |
6 ms |
35324 KB |
Output is correct |
29 |
Correct |
7 ms |
35152 KB |
Output is correct |
30 |
Correct |
6 ms |
35152 KB |
Output is correct |
31 |
Correct |
7 ms |
35324 KB |
Output is correct |
32 |
Correct |
7 ms |
35152 KB |
Output is correct |
33 |
Correct |
6 ms |
35152 KB |
Output is correct |
34 |
Correct |
7 ms |
35328 KB |
Output is correct |
35 |
Correct |
7 ms |
35152 KB |
Output is correct |
36 |
Correct |
7 ms |
35152 KB |
Output is correct |
37 |
Correct |
6 ms |
35152 KB |
Output is correct |
38 |
Correct |
7 ms |
35152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35152 KB |
Output is correct |
2 |
Correct |
6 ms |
35284 KB |
Output is correct |
3 |
Correct |
7 ms |
35152 KB |
Output is correct |
4 |
Correct |
6 ms |
35152 KB |
Output is correct |
5 |
Correct |
7 ms |
35152 KB |
Output is correct |
6 |
Correct |
7 ms |
35320 KB |
Output is correct |
7 |
Correct |
6 ms |
35152 KB |
Output is correct |
8 |
Correct |
6 ms |
35292 KB |
Output is correct |
9 |
Correct |
6 ms |
35152 KB |
Output is correct |
10 |
Correct |
6 ms |
35320 KB |
Output is correct |
11 |
Correct |
6 ms |
35152 KB |
Output is correct |
12 |
Correct |
6 ms |
35152 KB |
Output is correct |
13 |
Correct |
7 ms |
35152 KB |
Output is correct |
14 |
Correct |
6 ms |
35152 KB |
Output is correct |
15 |
Correct |
7 ms |
35152 KB |
Output is correct |
16 |
Correct |
7 ms |
35152 KB |
Output is correct |
17 |
Correct |
8 ms |
35408 KB |
Output is correct |
18 |
Correct |
6 ms |
35152 KB |
Output is correct |
19 |
Correct |
104 ms |
42568 KB |
Output is correct |
20 |
Correct |
89 ms |
42320 KB |
Output is correct |
21 |
Correct |
94 ms |
42448 KB |
Output is correct |
22 |
Correct |
92 ms |
42568 KB |
Output is correct |
23 |
Correct |
55 ms |
42832 KB |
Output is correct |
24 |
Correct |
45 ms |
42576 KB |
Output is correct |
25 |
Correct |
103 ms |
46144 KB |
Output is correct |
26 |
Correct |
87 ms |
49736 KB |
Output is correct |
27 |
Correct |
105 ms |
47944 KB |
Output is correct |
28 |
Correct |
172 ms |
62288 KB |
Output is correct |
29 |
Correct |
172 ms |
61208 KB |
Output is correct |
30 |
Correct |
109 ms |
47888 KB |
Output is correct |
31 |
Correct |
109 ms |
47956 KB |
Output is correct |
32 |
Correct |
115 ms |
47944 KB |
Output is correct |
33 |
Correct |
146 ms |
46664 KB |
Output is correct |
34 |
Correct |
119 ms |
47632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35152 KB |
Output is correct |
2 |
Correct |
6 ms |
35284 KB |
Output is correct |
3 |
Correct |
7 ms |
35152 KB |
Output is correct |
4 |
Correct |
6 ms |
35152 KB |
Output is correct |
5 |
Correct |
7 ms |
35152 KB |
Output is correct |
6 |
Correct |
7 ms |
35320 KB |
Output is correct |
7 |
Correct |
6 ms |
35152 KB |
Output is correct |
8 |
Correct |
6 ms |
35292 KB |
Output is correct |
9 |
Correct |
6 ms |
35152 KB |
Output is correct |
10 |
Correct |
6 ms |
35320 KB |
Output is correct |
11 |
Correct |
6 ms |
35152 KB |
Output is correct |
12 |
Correct |
6 ms |
35152 KB |
Output is correct |
13 |
Correct |
7 ms |
35152 KB |
Output is correct |
14 |
Correct |
6 ms |
35152 KB |
Output is correct |
15 |
Correct |
7 ms |
35152 KB |
Output is correct |
16 |
Correct |
7 ms |
35152 KB |
Output is correct |
17 |
Correct |
8 ms |
35408 KB |
Output is correct |
18 |
Correct |
6 ms |
35152 KB |
Output is correct |
19 |
Correct |
7 ms |
35152 KB |
Output is correct |
20 |
Correct |
5 ms |
35152 KB |
Output is correct |
21 |
Correct |
6 ms |
35380 KB |
Output is correct |
22 |
Correct |
6 ms |
35152 KB |
Output is correct |
23 |
Correct |
6 ms |
35152 KB |
Output is correct |
24 |
Correct |
6 ms |
35152 KB |
Output is correct |
25 |
Correct |
6 ms |
35152 KB |
Output is correct |
26 |
Correct |
6 ms |
35152 KB |
Output is correct |
27 |
Correct |
6 ms |
35152 KB |
Output is correct |
28 |
Correct |
6 ms |
35324 KB |
Output is correct |
29 |
Correct |
7 ms |
35152 KB |
Output is correct |
30 |
Correct |
6 ms |
35152 KB |
Output is correct |
31 |
Correct |
7 ms |
35324 KB |
Output is correct |
32 |
Correct |
7 ms |
35152 KB |
Output is correct |
33 |
Correct |
6 ms |
35152 KB |
Output is correct |
34 |
Correct |
7 ms |
35328 KB |
Output is correct |
35 |
Correct |
7 ms |
35152 KB |
Output is correct |
36 |
Correct |
7 ms |
35152 KB |
Output is correct |
37 |
Correct |
6 ms |
35152 KB |
Output is correct |
38 |
Correct |
7 ms |
35152 KB |
Output is correct |
39 |
Correct |
104 ms |
42568 KB |
Output is correct |
40 |
Correct |
89 ms |
42320 KB |
Output is correct |
41 |
Correct |
94 ms |
42448 KB |
Output is correct |
42 |
Correct |
92 ms |
42568 KB |
Output is correct |
43 |
Correct |
55 ms |
42832 KB |
Output is correct |
44 |
Correct |
45 ms |
42576 KB |
Output is correct |
45 |
Correct |
103 ms |
46144 KB |
Output is correct |
46 |
Correct |
87 ms |
49736 KB |
Output is correct |
47 |
Correct |
105 ms |
47944 KB |
Output is correct |
48 |
Correct |
172 ms |
62288 KB |
Output is correct |
49 |
Correct |
172 ms |
61208 KB |
Output is correct |
50 |
Correct |
109 ms |
47888 KB |
Output is correct |
51 |
Correct |
109 ms |
47956 KB |
Output is correct |
52 |
Correct |
115 ms |
47944 KB |
Output is correct |
53 |
Correct |
146 ms |
46664 KB |
Output is correct |
54 |
Correct |
119 ms |
47632 KB |
Output is correct |
55 |
Correct |
12 ms |
35664 KB |
Output is correct |
56 |
Correct |
13 ms |
35840 KB |
Output is correct |
57 |
Correct |
59 ms |
42576 KB |
Output is correct |
58 |
Correct |
28 ms |
42436 KB |
Output is correct |
59 |
Correct |
80 ms |
49740 KB |
Output is correct |
60 |
Correct |
318 ms |
61512 KB |
Output is correct |
61 |
Correct |
127 ms |
47944 KB |
Output is correct |
62 |
Correct |
135 ms |
47944 KB |
Output is correct |
63 |
Correct |
138 ms |
48132 KB |
Output is correct |
64 |
Correct |
256 ms |
48200 KB |
Output is correct |
65 |
Correct |
99 ms |
48784 KB |
Output is correct |
66 |
Correct |
221 ms |
58764 KB |
Output is correct |
67 |
Correct |
67 ms |
49228 KB |
Output is correct |
68 |
Correct |
133 ms |
48712 KB |
Output is correct |
69 |
Correct |
144 ms |
48968 KB |
Output is correct |
70 |
Correct |
127 ms |
48456 KB |
Output is correct |