#include "race.h"
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int kmax = 1000001;
const int nmax = 1000001;
vector <vector <pii> > g(nmax);
int d[nmax];
vector <bool> marked(nmax);
int k;
int n;
int ans = 1e9;
void DFS(int v, int e, int depth, int D){
ans = min(ans, d[k - depth] + D);
for(auto x: g[v]){
int to = x.f;
int len = x.s;
if(to == e || marked[to]) continue;
if(depth + len > k) continue;
DFS(to, v, depth + len, D + 1);
}
}
void upd(int v, int e, int depth, int D){
d[depth] = min(d[depth], D);
for(auto x : g[v]){
int to = x.f;
int len = x.s;
if(to == e || marked[to]) continue;
if(depth + len > k) continue;
upd(to, v, depth + len, D + 1);
}
}
void clear_dfs(int v, int e, int depth){
d[depth] = 1e9;
for(auto x : g[v]){
int to = x.f;
int len = x.s;
if(to == e || marked[to]) continue;
if(depth + len > k) continue;
clear_dfs(to, v, depth + len);
}
}
int sz[nmax];
void dfs(int v, int e){
sz[v] = 1;
for(auto x : g[v]){
int to = x.f;
if(marked[to] || to == e) continue;
dfs(to, v);
sz[v] += sz[to];
}
}
int find_cent(int v, int e, int n){
for(auto xx : g[v]){
int to = xx.f;
if(marked[to] || to == e) continue;
if(sz[to] * 2 >= n) return find_cent(to, v, n);
}
return v;
}
void decompose(int v){
dfs(v, v);
int x = find_cent(v, v, sz[v]);
//cout << 1;
d[0] = 0;
for(auto xx : g[x]){
int to = xx.f, len = xx.s;
if(marked[to]) continue;
if(len <= k){
DFS(to, x, len, 1);
upd(to, x, len, 1);
}
}
clear_dfs(x, x, 0);
marked[x] = true;
for(auto xx : g[x]){
int to = xx.f;
if(marked[to]) continue;
decompose(to);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
int n = N;
for(int i = 0; i < N - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
for(int i = 0; i <= k; i++){
d[i] = 1e9;
}
decompose(0);
if(ans == 1e9) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:104:9: warning: unused variable 'n' [-Wunused-variable]
104 | int n = N;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23924 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23936 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23892 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23856 KB |
Output is correct |
10 |
Correct |
13 ms |
23868 KB |
Output is correct |
11 |
Correct |
13 ms |
23912 KB |
Output is correct |
12 |
Correct |
13 ms |
24020 KB |
Output is correct |
13 |
Correct |
13 ms |
23936 KB |
Output is correct |
14 |
Correct |
13 ms |
23936 KB |
Output is correct |
15 |
Correct |
13 ms |
23940 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
23832 KB |
Output is correct |
18 |
Correct |
13 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23924 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23936 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23892 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23856 KB |
Output is correct |
10 |
Correct |
13 ms |
23868 KB |
Output is correct |
11 |
Correct |
13 ms |
23912 KB |
Output is correct |
12 |
Correct |
13 ms |
24020 KB |
Output is correct |
13 |
Correct |
13 ms |
23936 KB |
Output is correct |
14 |
Correct |
13 ms |
23936 KB |
Output is correct |
15 |
Correct |
13 ms |
23940 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
23832 KB |
Output is correct |
18 |
Correct |
13 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23828 KB |
Output is correct |
20 |
Correct |
12 ms |
23920 KB |
Output is correct |
21 |
Correct |
13 ms |
23960 KB |
Output is correct |
22 |
Correct |
15 ms |
27536 KB |
Output is correct |
23 |
Correct |
15 ms |
26836 KB |
Output is correct |
24 |
Correct |
15 ms |
27376 KB |
Output is correct |
25 |
Correct |
15 ms |
27276 KB |
Output is correct |
26 |
Correct |
14 ms |
25312 KB |
Output is correct |
27 |
Correct |
14 ms |
27148 KB |
Output is correct |
28 |
Correct |
14 ms |
24696 KB |
Output is correct |
29 |
Correct |
14 ms |
25172 KB |
Output is correct |
30 |
Correct |
14 ms |
25444 KB |
Output is correct |
31 |
Correct |
15 ms |
26552 KB |
Output is correct |
32 |
Correct |
15 ms |
26708 KB |
Output is correct |
33 |
Correct |
17 ms |
26964 KB |
Output is correct |
34 |
Correct |
14 ms |
26236 KB |
Output is correct |
35 |
Correct |
16 ms |
27148 KB |
Output is correct |
36 |
Correct |
15 ms |
27604 KB |
Output is correct |
37 |
Correct |
15 ms |
27100 KB |
Output is correct |
38 |
Correct |
14 ms |
25920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23924 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23936 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23892 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23856 KB |
Output is correct |
10 |
Correct |
13 ms |
23868 KB |
Output is correct |
11 |
Correct |
13 ms |
23912 KB |
Output is correct |
12 |
Correct |
13 ms |
24020 KB |
Output is correct |
13 |
Correct |
13 ms |
23936 KB |
Output is correct |
14 |
Correct |
13 ms |
23936 KB |
Output is correct |
15 |
Correct |
13 ms |
23940 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
23832 KB |
Output is correct |
18 |
Correct |
13 ms |
23892 KB |
Output is correct |
19 |
Correct |
116 ms |
30504 KB |
Output is correct |
20 |
Correct |
110 ms |
30504 KB |
Output is correct |
21 |
Correct |
111 ms |
30532 KB |
Output is correct |
22 |
Correct |
98 ms |
30616 KB |
Output is correct |
23 |
Correct |
61 ms |
30808 KB |
Output is correct |
24 |
Correct |
50 ms |
30724 KB |
Output is correct |
25 |
Correct |
110 ms |
34096 KB |
Output is correct |
26 |
Correct |
88 ms |
37800 KB |
Output is correct |
27 |
Correct |
131 ms |
37480 KB |
Output is correct |
28 |
Correct |
180 ms |
51996 KB |
Output is correct |
29 |
Correct |
188 ms |
50868 KB |
Output is correct |
30 |
Correct |
127 ms |
37448 KB |
Output is correct |
31 |
Correct |
123 ms |
37424 KB |
Output is correct |
32 |
Correct |
142 ms |
37644 KB |
Output is correct |
33 |
Correct |
203 ms |
36300 KB |
Output is correct |
34 |
Correct |
121 ms |
37316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23924 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
13 ms |
23936 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23892 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23856 KB |
Output is correct |
10 |
Correct |
13 ms |
23868 KB |
Output is correct |
11 |
Correct |
13 ms |
23912 KB |
Output is correct |
12 |
Correct |
13 ms |
24020 KB |
Output is correct |
13 |
Correct |
13 ms |
23936 KB |
Output is correct |
14 |
Correct |
13 ms |
23936 KB |
Output is correct |
15 |
Correct |
13 ms |
23940 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
23832 KB |
Output is correct |
18 |
Correct |
13 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23828 KB |
Output is correct |
20 |
Correct |
12 ms |
23920 KB |
Output is correct |
21 |
Correct |
13 ms |
23960 KB |
Output is correct |
22 |
Correct |
15 ms |
27536 KB |
Output is correct |
23 |
Correct |
15 ms |
26836 KB |
Output is correct |
24 |
Correct |
15 ms |
27376 KB |
Output is correct |
25 |
Correct |
15 ms |
27276 KB |
Output is correct |
26 |
Correct |
14 ms |
25312 KB |
Output is correct |
27 |
Correct |
14 ms |
27148 KB |
Output is correct |
28 |
Correct |
14 ms |
24696 KB |
Output is correct |
29 |
Correct |
14 ms |
25172 KB |
Output is correct |
30 |
Correct |
14 ms |
25444 KB |
Output is correct |
31 |
Correct |
15 ms |
26552 KB |
Output is correct |
32 |
Correct |
15 ms |
26708 KB |
Output is correct |
33 |
Correct |
17 ms |
26964 KB |
Output is correct |
34 |
Correct |
14 ms |
26236 KB |
Output is correct |
35 |
Correct |
16 ms |
27148 KB |
Output is correct |
36 |
Correct |
15 ms |
27604 KB |
Output is correct |
37 |
Correct |
15 ms |
27100 KB |
Output is correct |
38 |
Correct |
14 ms |
25920 KB |
Output is correct |
39 |
Correct |
116 ms |
30504 KB |
Output is correct |
40 |
Correct |
110 ms |
30504 KB |
Output is correct |
41 |
Correct |
111 ms |
30532 KB |
Output is correct |
42 |
Correct |
98 ms |
30616 KB |
Output is correct |
43 |
Correct |
61 ms |
30808 KB |
Output is correct |
44 |
Correct |
50 ms |
30724 KB |
Output is correct |
45 |
Correct |
110 ms |
34096 KB |
Output is correct |
46 |
Correct |
88 ms |
37800 KB |
Output is correct |
47 |
Correct |
131 ms |
37480 KB |
Output is correct |
48 |
Correct |
180 ms |
51996 KB |
Output is correct |
49 |
Correct |
188 ms |
50868 KB |
Output is correct |
50 |
Correct |
127 ms |
37448 KB |
Output is correct |
51 |
Correct |
123 ms |
37424 KB |
Output is correct |
52 |
Correct |
142 ms |
37644 KB |
Output is correct |
53 |
Correct |
203 ms |
36300 KB |
Output is correct |
54 |
Correct |
121 ms |
37316 KB |
Output is correct |
55 |
Correct |
19 ms |
24596 KB |
Output is correct |
56 |
Correct |
19 ms |
24588 KB |
Output is correct |
57 |
Correct |
68 ms |
30652 KB |
Output is correct |
58 |
Correct |
38 ms |
30448 KB |
Output is correct |
59 |
Correct |
90 ms |
38532 KB |
Output is correct |
60 |
Correct |
318 ms |
54940 KB |
Output is correct |
61 |
Correct |
146 ms |
37672 KB |
Output is correct |
62 |
Correct |
156 ms |
41476 KB |
Output is correct |
63 |
Correct |
204 ms |
41548 KB |
Output is correct |
64 |
Correct |
364 ms |
39812 KB |
Output is correct |
65 |
Correct |
183 ms |
38388 KB |
Output is correct |
66 |
Correct |
270 ms |
52344 KB |
Output is correct |
67 |
Correct |
83 ms |
42100 KB |
Output is correct |
68 |
Correct |
185 ms |
42252 KB |
Output is correct |
69 |
Correct |
212 ms |
42368 KB |
Output is correct |
70 |
Correct |
168 ms |
41692 KB |
Output is correct |