#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
#define f(i,a,b) for(int i = a; i < b; i++)
#define ff first
#define ss second
const ll N = 1e6 + 5;
ll n, k, sz[N], ans = N, dist[N], dreal[N], aux[N];
bool on[N];
vector <pair<int,ll>> adj[N];
vector <int> ra;
int size(int u, int f){
sz[u] = 1;
for(auto v: adj[u]) if(v.first != f and !on[v.first]) sz[u] += size(v.first, u);
return sz[u];
}
int get_centroid(int u, int f, int curr_size){
for(auto v: adj[u]) if(v.first != f and !on[v.first] and sz[v.first]*2 > curr_size) return get_centroid(v.first, u, curr_size);
return u;
}
void get_tree(int u, int f){
ra.push_back(u);
for(auto v: adj[u]) if(v.first != f and !on[v.first]) get_tree(v.first, u);
}
void dfs(int u, int f){
for(auto p: adj[u]){
ll v = p.first, w = p.second;
if(v == f or on[v]) continue;
dist[v] = dist[u] + 1;
dreal[v] = dreal[u] + w;
dfs(v, u);
}
}
void solve(int u, int f){
int cen = get_centroid(u, f, size(u, f));
on[cen] = 1, dist[cen] = dreal[cen] = 0;
dfs(cen, f);
vector <ll> AUX;
for(auto p: adj[cen]){
int v = p.ff;
if(v == f or on[v]) continue;
ra.clear(); get_tree(v, cen);
for(int x: ra){
if(dreal[x] == k){
ans = min(ans, dist[x]);
}
else if(dreal[x] > k) continue;
ans = min(ans, aux[k-dreal[x]] + dist[x]);
}
for(int x: ra){
if(dreal[x] >= k) continue;
aux[dreal[x]] = min(aux[dreal[x]], dist[x]);
AUX.push_back(dreal[x]);
}
}
for(int x: AUX) aux[x] = N;
for(auto v: adj[cen]) if(v.first != f and !on[v.first]) solve(v.first, cen);
}
int best_path(int m, int K, int H[][2], int L[]){
n = m, k = K;
f(i,0,n-1){
ll u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
f(i,0,N) aux[i] = N;
solve(0, -1);
if(ans == N) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31600 KB |
Output is correct |
5 |
Correct |
15 ms |
31584 KB |
Output is correct |
6 |
Correct |
18 ms |
31576 KB |
Output is correct |
7 |
Correct |
16 ms |
31596 KB |
Output is correct |
8 |
Correct |
15 ms |
31660 KB |
Output is correct |
9 |
Correct |
15 ms |
31576 KB |
Output is correct |
10 |
Correct |
16 ms |
31600 KB |
Output is correct |
11 |
Correct |
14 ms |
31652 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
15 ms |
31564 KB |
Output is correct |
14 |
Correct |
14 ms |
31616 KB |
Output is correct |
15 |
Correct |
15 ms |
31600 KB |
Output is correct |
16 |
Correct |
14 ms |
31576 KB |
Output is correct |
17 |
Correct |
14 ms |
31564 KB |
Output is correct |
18 |
Correct |
14 ms |
31576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31600 KB |
Output is correct |
5 |
Correct |
15 ms |
31584 KB |
Output is correct |
6 |
Correct |
18 ms |
31576 KB |
Output is correct |
7 |
Correct |
16 ms |
31596 KB |
Output is correct |
8 |
Correct |
15 ms |
31660 KB |
Output is correct |
9 |
Correct |
15 ms |
31576 KB |
Output is correct |
10 |
Correct |
16 ms |
31600 KB |
Output is correct |
11 |
Correct |
14 ms |
31652 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
15 ms |
31564 KB |
Output is correct |
14 |
Correct |
14 ms |
31616 KB |
Output is correct |
15 |
Correct |
15 ms |
31600 KB |
Output is correct |
16 |
Correct |
14 ms |
31576 KB |
Output is correct |
17 |
Correct |
14 ms |
31564 KB |
Output is correct |
18 |
Correct |
14 ms |
31576 KB |
Output is correct |
19 |
Correct |
14 ms |
31564 KB |
Output is correct |
20 |
Correct |
14 ms |
31564 KB |
Output is correct |
21 |
Correct |
15 ms |
31756 KB |
Output is correct |
22 |
Correct |
20 ms |
31712 KB |
Output is correct |
23 |
Correct |
15 ms |
31692 KB |
Output is correct |
24 |
Correct |
15 ms |
31692 KB |
Output is correct |
25 |
Correct |
16 ms |
31624 KB |
Output is correct |
26 |
Correct |
15 ms |
31660 KB |
Output is correct |
27 |
Correct |
14 ms |
31732 KB |
Output is correct |
28 |
Correct |
15 ms |
31668 KB |
Output is correct |
29 |
Correct |
17 ms |
31668 KB |
Output is correct |
30 |
Correct |
16 ms |
31692 KB |
Output is correct |
31 |
Correct |
20 ms |
31744 KB |
Output is correct |
32 |
Correct |
15 ms |
31620 KB |
Output is correct |
33 |
Correct |
16 ms |
31656 KB |
Output is correct |
34 |
Correct |
14 ms |
31692 KB |
Output is correct |
35 |
Correct |
19 ms |
31652 KB |
Output is correct |
36 |
Correct |
15 ms |
31724 KB |
Output is correct |
37 |
Correct |
15 ms |
31652 KB |
Output is correct |
38 |
Correct |
21 ms |
31672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31600 KB |
Output is correct |
5 |
Correct |
15 ms |
31584 KB |
Output is correct |
6 |
Correct |
18 ms |
31576 KB |
Output is correct |
7 |
Correct |
16 ms |
31596 KB |
Output is correct |
8 |
Correct |
15 ms |
31660 KB |
Output is correct |
9 |
Correct |
15 ms |
31576 KB |
Output is correct |
10 |
Correct |
16 ms |
31600 KB |
Output is correct |
11 |
Correct |
14 ms |
31652 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
15 ms |
31564 KB |
Output is correct |
14 |
Correct |
14 ms |
31616 KB |
Output is correct |
15 |
Correct |
15 ms |
31600 KB |
Output is correct |
16 |
Correct |
14 ms |
31576 KB |
Output is correct |
17 |
Correct |
14 ms |
31564 KB |
Output is correct |
18 |
Correct |
14 ms |
31576 KB |
Output is correct |
19 |
Correct |
212 ms |
40944 KB |
Output is correct |
20 |
Correct |
171 ms |
40800 KB |
Output is correct |
21 |
Correct |
236 ms |
41264 KB |
Output is correct |
22 |
Correct |
162 ms |
41916 KB |
Output is correct |
23 |
Correct |
121 ms |
40976 KB |
Output is correct |
24 |
Correct |
75 ms |
40052 KB |
Output is correct |
25 |
Correct |
185 ms |
43476 KB |
Output is correct |
26 |
Correct |
112 ms |
48096 KB |
Output is correct |
27 |
Correct |
214 ms |
49588 KB |
Output is correct |
28 |
Correct |
476 ms |
61340 KB |
Output is correct |
29 |
Correct |
524 ms |
60368 KB |
Output is correct |
30 |
Correct |
199 ms |
49644 KB |
Output is correct |
31 |
Correct |
220 ms |
49688 KB |
Output is correct |
32 |
Correct |
287 ms |
49564 KB |
Output is correct |
33 |
Correct |
393 ms |
48816 KB |
Output is correct |
34 |
Correct |
340 ms |
48864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31600 KB |
Output is correct |
5 |
Correct |
15 ms |
31584 KB |
Output is correct |
6 |
Correct |
18 ms |
31576 KB |
Output is correct |
7 |
Correct |
16 ms |
31596 KB |
Output is correct |
8 |
Correct |
15 ms |
31660 KB |
Output is correct |
9 |
Correct |
15 ms |
31576 KB |
Output is correct |
10 |
Correct |
16 ms |
31600 KB |
Output is correct |
11 |
Correct |
14 ms |
31652 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
15 ms |
31564 KB |
Output is correct |
14 |
Correct |
14 ms |
31616 KB |
Output is correct |
15 |
Correct |
15 ms |
31600 KB |
Output is correct |
16 |
Correct |
14 ms |
31576 KB |
Output is correct |
17 |
Correct |
14 ms |
31564 KB |
Output is correct |
18 |
Correct |
14 ms |
31576 KB |
Output is correct |
19 |
Correct |
14 ms |
31564 KB |
Output is correct |
20 |
Correct |
14 ms |
31564 KB |
Output is correct |
21 |
Correct |
15 ms |
31756 KB |
Output is correct |
22 |
Correct |
20 ms |
31712 KB |
Output is correct |
23 |
Correct |
15 ms |
31692 KB |
Output is correct |
24 |
Correct |
15 ms |
31692 KB |
Output is correct |
25 |
Correct |
16 ms |
31624 KB |
Output is correct |
26 |
Correct |
15 ms |
31660 KB |
Output is correct |
27 |
Correct |
14 ms |
31732 KB |
Output is correct |
28 |
Correct |
15 ms |
31668 KB |
Output is correct |
29 |
Correct |
17 ms |
31668 KB |
Output is correct |
30 |
Correct |
16 ms |
31692 KB |
Output is correct |
31 |
Correct |
20 ms |
31744 KB |
Output is correct |
32 |
Correct |
15 ms |
31620 KB |
Output is correct |
33 |
Correct |
16 ms |
31656 KB |
Output is correct |
34 |
Correct |
14 ms |
31692 KB |
Output is correct |
35 |
Correct |
19 ms |
31652 KB |
Output is correct |
36 |
Correct |
15 ms |
31724 KB |
Output is correct |
37 |
Correct |
15 ms |
31652 KB |
Output is correct |
38 |
Correct |
21 ms |
31672 KB |
Output is correct |
39 |
Correct |
212 ms |
40944 KB |
Output is correct |
40 |
Correct |
171 ms |
40800 KB |
Output is correct |
41 |
Correct |
236 ms |
41264 KB |
Output is correct |
42 |
Correct |
162 ms |
41916 KB |
Output is correct |
43 |
Correct |
121 ms |
40976 KB |
Output is correct |
44 |
Correct |
75 ms |
40052 KB |
Output is correct |
45 |
Correct |
185 ms |
43476 KB |
Output is correct |
46 |
Correct |
112 ms |
48096 KB |
Output is correct |
47 |
Correct |
214 ms |
49588 KB |
Output is correct |
48 |
Correct |
476 ms |
61340 KB |
Output is correct |
49 |
Correct |
524 ms |
60368 KB |
Output is correct |
50 |
Correct |
199 ms |
49644 KB |
Output is correct |
51 |
Correct |
220 ms |
49688 KB |
Output is correct |
52 |
Correct |
287 ms |
49564 KB |
Output is correct |
53 |
Correct |
393 ms |
48816 KB |
Output is correct |
54 |
Correct |
340 ms |
48864 KB |
Output is correct |
55 |
Correct |
25 ms |
32648 KB |
Output is correct |
56 |
Correct |
25 ms |
32716 KB |
Output is correct |
57 |
Correct |
102 ms |
42356 KB |
Output is correct |
58 |
Correct |
49 ms |
41696 KB |
Output is correct |
59 |
Correct |
113 ms |
48080 KB |
Output is correct |
60 |
Correct |
546 ms |
63864 KB |
Output is correct |
61 |
Correct |
208 ms |
49984 KB |
Output is correct |
62 |
Correct |
205 ms |
51452 KB |
Output is correct |
63 |
Correct |
321 ms |
51528 KB |
Output is correct |
64 |
Correct |
559 ms |
52916 KB |
Output is correct |
65 |
Correct |
467 ms |
49604 KB |
Output is correct |
66 |
Correct |
540 ms |
58296 KB |
Output is correct |
67 |
Correct |
173 ms |
51780 KB |
Output is correct |
68 |
Correct |
284 ms |
51588 KB |
Output is correct |
69 |
Correct |
281 ms |
51416 KB |
Output is correct |
70 |
Correct |
310 ms |
50872 KB |
Output is correct |