#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+4;
const int inf = 1e9;
struct Cd{
vector<pair<int,int>> adj[N];
int vis[N],sz[N],up[N],mn[5*N];
int ans,k;
void init(int _k){
for (int i=0; i<5*N; i++) mn[i] = inf;
ans = inf; k = _k;
}
int find(int u,int p,int Sz){
sz[u] = 1;
int val = 0, x = 0;
for (auto [v, w] : adj[u]) if (v != p && !vis[v]){
x ^= find(v, u, Sz); sz[u] += sz[v];
val = max(val, sz[v]);
}
if (max(val, Sz-sz[u]) <= Sz/2) up[x = u] = p;
return x;
}
void dfs(int u,int p,int dis,int h,int f){
if (dis > k) return;
if (f == 0) ans = min(ans, mn[k-dis]+h);
if (f == 1) mn[dis] = min(mn[dis], h);
if (f == 2) mn[dis] = inf;
for (auto [v, w] : adj[u]) if (v != p && !vis[v])
dfs(v, u, dis+w, h+1, f);
}
void calc(int u){
mn[0] = 0;
for (auto [v, w] : adj[u]) if (!vis[v]){
dfs(v, u, w, 1, 0);
dfs(v, u, w, 1, 1);
}
dfs(u, -1, 0, 0, 2);
}
void build(int u,int Sz){
u = find(u, -1, Sz);
if (up[u] != -1) sz[up[u]] = Sz-sz[u];
vis[u] = 1; calc(u);
for (auto [v, w] : adj[u]) if (!vis[v])
build(v, sz[v]);
}
} d;
int best_path(int n, int k, int H[][2], int L[]){
for (int i=0; i<n-1; i++){
d.adj[H[i][0]+1].push_back({H[i][1]+1, L[i]});
d.adj[H[i][1]+1].push_back({H[i][0]+1, L[i]});
}
d.init(k);
d.build(1, n);
return (d.ans == inf ? -1 : d.ans);
}
/*
int main(){
int n,k,H[N][2],L[N]; cin >> n >> k;
for (int i=0; i<n-1; i++) cin >> H[i][0] >> H[i][1] >> L[i];
cout << best_path(n, k, H, L) << "\n";
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8900 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8900 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8844 KB |
Output is correct |
12 |
Correct |
5 ms |
8840 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8840 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8900 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8900 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8844 KB |
Output is correct |
12 |
Correct |
5 ms |
8840 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8840 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8852 KB |
Output is correct |
19 |
Correct |
5 ms |
8848 KB |
Output is correct |
20 |
Correct |
5 ms |
8848 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
6 ms |
8908 KB |
Output is correct |
23 |
Correct |
8 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
7 ms |
8992 KB |
Output is correct |
26 |
Correct |
7 ms |
8972 KB |
Output is correct |
27 |
Correct |
5 ms |
8988 KB |
Output is correct |
28 |
Correct |
6 ms |
8988 KB |
Output is correct |
29 |
Correct |
9 ms |
8992 KB |
Output is correct |
30 |
Correct |
5 ms |
8908 KB |
Output is correct |
31 |
Correct |
10 ms |
8984 KB |
Output is correct |
32 |
Correct |
11 ms |
8912 KB |
Output is correct |
33 |
Correct |
9 ms |
8984 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8984 KB |
Output is correct |
36 |
Correct |
5 ms |
8992 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
7 ms |
8984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8900 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8900 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8844 KB |
Output is correct |
12 |
Correct |
5 ms |
8840 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8840 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8852 KB |
Output is correct |
19 |
Correct |
135 ms |
16272 KB |
Output is correct |
20 |
Correct |
127 ms |
16268 KB |
Output is correct |
21 |
Correct |
179 ms |
16228 KB |
Output is correct |
22 |
Correct |
170 ms |
16388 KB |
Output is correct |
23 |
Correct |
115 ms |
16536 KB |
Output is correct |
24 |
Correct |
53 ms |
16420 KB |
Output is correct |
25 |
Correct |
124 ms |
18072 KB |
Output is correct |
26 |
Correct |
92 ms |
20020 KB |
Output is correct |
27 |
Correct |
148 ms |
24008 KB |
Output is correct |
28 |
Correct |
244 ms |
31592 KB |
Output is correct |
29 |
Correct |
236 ms |
30904 KB |
Output is correct |
30 |
Correct |
169 ms |
24116 KB |
Output is correct |
31 |
Correct |
165 ms |
24008 KB |
Output is correct |
32 |
Correct |
189 ms |
24132 KB |
Output is correct |
33 |
Correct |
231 ms |
22732 KB |
Output is correct |
34 |
Correct |
186 ms |
23740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8900 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8900 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8844 KB |
Output is correct |
12 |
Correct |
5 ms |
8840 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8840 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8852 KB |
Output is correct |
19 |
Correct |
5 ms |
8848 KB |
Output is correct |
20 |
Correct |
5 ms |
8848 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
6 ms |
8908 KB |
Output is correct |
23 |
Correct |
8 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
7 ms |
8992 KB |
Output is correct |
26 |
Correct |
7 ms |
8972 KB |
Output is correct |
27 |
Correct |
5 ms |
8988 KB |
Output is correct |
28 |
Correct |
6 ms |
8988 KB |
Output is correct |
29 |
Correct |
9 ms |
8992 KB |
Output is correct |
30 |
Correct |
5 ms |
8908 KB |
Output is correct |
31 |
Correct |
10 ms |
8984 KB |
Output is correct |
32 |
Correct |
11 ms |
8912 KB |
Output is correct |
33 |
Correct |
9 ms |
8984 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8984 KB |
Output is correct |
36 |
Correct |
5 ms |
8992 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
7 ms |
8984 KB |
Output is correct |
39 |
Correct |
135 ms |
16272 KB |
Output is correct |
40 |
Correct |
127 ms |
16268 KB |
Output is correct |
41 |
Correct |
179 ms |
16228 KB |
Output is correct |
42 |
Correct |
170 ms |
16388 KB |
Output is correct |
43 |
Correct |
115 ms |
16536 KB |
Output is correct |
44 |
Correct |
53 ms |
16420 KB |
Output is correct |
45 |
Correct |
124 ms |
18072 KB |
Output is correct |
46 |
Correct |
92 ms |
20020 KB |
Output is correct |
47 |
Correct |
148 ms |
24008 KB |
Output is correct |
48 |
Correct |
244 ms |
31592 KB |
Output is correct |
49 |
Correct |
236 ms |
30904 KB |
Output is correct |
50 |
Correct |
169 ms |
24116 KB |
Output is correct |
51 |
Correct |
165 ms |
24008 KB |
Output is correct |
52 |
Correct |
189 ms |
24132 KB |
Output is correct |
53 |
Correct |
231 ms |
22732 KB |
Output is correct |
54 |
Correct |
186 ms |
23740 KB |
Output is correct |
55 |
Correct |
14 ms |
9660 KB |
Output is correct |
56 |
Correct |
15 ms |
9628 KB |
Output is correct |
57 |
Correct |
93 ms |
16404 KB |
Output is correct |
58 |
Correct |
34 ms |
16252 KB |
Output is correct |
59 |
Correct |
93 ms |
19976 KB |
Output is correct |
60 |
Correct |
454 ms |
31028 KB |
Output is correct |
61 |
Correct |
185 ms |
24100 KB |
Output is correct |
62 |
Correct |
217 ms |
24112 KB |
Output is correct |
63 |
Correct |
266 ms |
24076 KB |
Output is correct |
64 |
Correct |
511 ms |
24340 KB |
Output is correct |
65 |
Correct |
205 ms |
24900 KB |
Output is correct |
66 |
Correct |
388 ms |
29696 KB |
Output is correct |
67 |
Correct |
102 ms |
24776 KB |
Output is correct |
68 |
Correct |
286 ms |
24852 KB |
Output is correct |
69 |
Correct |
249 ms |
24980 KB |
Output is correct |
70 |
Correct |
293 ms |
24212 KB |
Output is correct |