#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 69;
int n, k;
vector <pair<int, int>> adj[maxn];
bool del[maxn];
int sz[maxn];
map <int, int> mp;
vector <int> comp;
int ans = 1e9;
vector <pair<int, int>> vec;
void dfs(int u, int par, int dist, int dep = 1){
if (dist > k) return;
vec.push_back(make_pair(dist, dep));
if (mp.find(k - dist) != mp.end()){
ans = min(ans, mp[k - dist] + dep);
}
for (auto [v, w] : adj[u]){
if (!del[v] && v != par){
dfs(v, u, dist + w, dep + 1);
}
}
}
void dfs2(int u, int par){
sz[u] = 1;
comp.push_back(u);
for (auto [v, w] : adj[u]){
if (!del[v] && v != par){
dfs2(v, u);
sz[u] += sz[v];
}
}
}
int find(int x){
comp.clear();
dfs2(x, -1);
for (auto u : comp){
int mx = 0;
for (auto [v, w] : adj[u]){
if (!del[v] && sz[v] < sz[u])
mx = max(mx, sz[v]);
}
mx = max(mx, (int)comp.size() - 1 - sz[u]);
// cout << u << " " << mx << "\n";
if (2 * mx <= (int)comp.size()) return u;
}
// for (auto x : comp){
// cout << x << " " << sz[x] << "\n";
// }
// exit(0);
assert(false);
}
void cd(int x){
x = find(x);
mp.clear();
mp[0] = 0;
int u = x;
for (auto [v, w] : adj[u]){
if (!del[v]){
dfs(v, u, w);
for (auto [x, y] : vec){
if (mp.find(x) == mp.end()) mp[x] = y;
else mp[x] = min(mp[x], y);
}
vec.clear();
}
}
del[x] = true;
for (auto [v, w] : adj[x]){
if (!del[v]){
cd(v);
}
}
}
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];
int v = H[i][1];
int w = L[i];
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
cd(0);
if (ans == 1e9) return -1;
else return ans;
}
// int main(){
// int n, k; cin >> n >> k;
// int H[n - 1][2], L[n - 1];
// for (int i = 0; i < n - 1; i++){
// cin >> H[i][0] >> H[i][1];
// }
// for (int i = 0; i < n - 1; i++) cin >> L[i];
// cout << best_path(n, k, H, L);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5016 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5016 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5032 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5024 KB |
Output is correct |
31 |
Correct |
4 ms |
5152 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5016 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
169 ms |
12172 KB |
Output is correct |
20 |
Correct |
130 ms |
12292 KB |
Output is correct |
21 |
Correct |
141 ms |
12536 KB |
Output is correct |
22 |
Correct |
173 ms |
12612 KB |
Output is correct |
23 |
Correct |
61 ms |
12492 KB |
Output is correct |
24 |
Correct |
62 ms |
12448 KB |
Output is correct |
25 |
Correct |
102 ms |
15712 KB |
Output is correct |
26 |
Correct |
90 ms |
19952 KB |
Output is correct |
27 |
Correct |
145 ms |
19732 KB |
Output is correct |
28 |
Correct |
215 ms |
34112 KB |
Output is correct |
29 |
Correct |
210 ms |
33008 KB |
Output is correct |
30 |
Correct |
145 ms |
19700 KB |
Output is correct |
31 |
Correct |
170 ms |
19660 KB |
Output is correct |
32 |
Correct |
161 ms |
19792 KB |
Output is correct |
33 |
Correct |
218 ms |
18596 KB |
Output is correct |
34 |
Correct |
139 ms |
19484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5016 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5032 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5024 KB |
Output is correct |
31 |
Correct |
4 ms |
5152 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
169 ms |
12172 KB |
Output is correct |
40 |
Correct |
130 ms |
12292 KB |
Output is correct |
41 |
Correct |
141 ms |
12536 KB |
Output is correct |
42 |
Correct |
173 ms |
12612 KB |
Output is correct |
43 |
Correct |
61 ms |
12492 KB |
Output is correct |
44 |
Correct |
62 ms |
12448 KB |
Output is correct |
45 |
Correct |
102 ms |
15712 KB |
Output is correct |
46 |
Correct |
90 ms |
19952 KB |
Output is correct |
47 |
Correct |
145 ms |
19732 KB |
Output is correct |
48 |
Correct |
215 ms |
34112 KB |
Output is correct |
49 |
Correct |
210 ms |
33008 KB |
Output is correct |
50 |
Correct |
145 ms |
19700 KB |
Output is correct |
51 |
Correct |
170 ms |
19660 KB |
Output is correct |
52 |
Correct |
161 ms |
19792 KB |
Output is correct |
53 |
Correct |
218 ms |
18596 KB |
Output is correct |
54 |
Correct |
139 ms |
19484 KB |
Output is correct |
55 |
Correct |
15 ms |
5844 KB |
Output is correct |
56 |
Correct |
12 ms |
5856 KB |
Output is correct |
57 |
Correct |
111 ms |
12884 KB |
Output is correct |
58 |
Correct |
40 ms |
12460 KB |
Output is correct |
59 |
Correct |
224 ms |
23248 KB |
Output is correct |
60 |
Correct |
695 ms |
42572 KB |
Output is correct |
61 |
Correct |
193 ms |
19968 KB |
Output is correct |
62 |
Correct |
210 ms |
19808 KB |
Output is correct |
63 |
Correct |
297 ms |
19784 KB |
Output is correct |
64 |
Correct |
673 ms |
25204 KB |
Output is correct |
65 |
Correct |
138 ms |
20568 KB |
Output is correct |
66 |
Correct |
465 ms |
30696 KB |
Output is correct |
67 |
Correct |
94 ms |
21300 KB |
Output is correct |
68 |
Correct |
292 ms |
23928 KB |
Output is correct |
69 |
Correct |
301 ms |
24004 KB |
Output is correct |
70 |
Correct |
256 ms |
23128 KB |
Output is correct |