#include "race.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
using namespace std;
int INF = numeric_limits<int>::max()/4, best[1000001];
void dfs(int u, int p, vector<int>& st, vector<int>& active, vector< vector<pair<int, int> > >& g){
st[u] = 1;
for(auto &t : g[u]){
if(!active[t.first] || t.first == p) continue;
dfs(t.first, u, st, active, g);
st[u] += st[t.first];
}
}
int fnd(int u, int p, int n, vector<int>& st, vector<int>& active, vector< vector<pair<int, int> > >& g){
for(auto &t : g[u]){
if(!active[t.first] || t.first == p) continue;
if(st[t.first] > n/2) return fnd(t.first, u, n, st, active, g);
}
return u;
}
void sum(int u, int p, int d, int num, vector< pair<int, int> >& temp, vector<int>& active, vector< vector< pair<int, int> > >& g){
temp.push_back({num++, d});
for(auto& t : g[u]){
if(t.first == p || !active[t.first]) continue;
sum(t.first, u, d+t.second, num, temp, active, g);
}
}
void solve(int u, int k, vector<int>& st, vector<int>& active, vector< vector< pair<int, int> > >& g, int& b){
dfs(u, u, st, active, g);
u = fnd(u, u, st[u], st, active, g);
vector<int> ers;
vector< pair<int, int> > temp;
for(auto &t : g[u]){
if(!active[t.first]) continue;
temp.clear();
sum(t.first, u, t.second, 1, temp, active, g);
for(auto &q : temp){
if(q.second > k) continue;
else b = min(b, q.first + best[k-q.second]);
}
for(auto &q : temp) if(q.second <= k) best[q.second] = min(best[q.second], q.first), ers.push_back(q.second);
}
for(int &x : ers) best[x] = INF;
active[u] = 0;
for(int i = 0; i < (int)g[u].size(); i++)
if(active[g[u][i].first] && st[g[u][i].first] > 1)
solve(g[u][i].first, k, st, active, g, b);
}
signed best_path(signed N, signed K, signed H[][2], signed L[]){
int n = N, k = K, b = INF, u, v, w;
vector< vector< pair<int, int> > > g(n);
for(int i = 1; i < n; i++){
u = H[i-1][0], v = H[i-1][1], w = L[i-1];
g[u].push_back({v, w}), g[v].push_back({u, w});
}
for(int i = 1; i <= k; i++) best[i] = INF;
vector<int> active(n, 1), st(n);
solve(0, k, st, active, g, b);
return (b==INF?-1:b);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
7680 KB |
Output is correct |
23 |
Correct |
5 ms |
6400 KB |
Output is correct |
24 |
Correct |
5 ms |
7296 KB |
Output is correct |
25 |
Correct |
6 ms |
7040 KB |
Output is correct |
26 |
Correct |
3 ms |
3072 KB |
Output is correct |
27 |
Correct |
5 ms |
6784 KB |
Output is correct |
28 |
Correct |
3 ms |
2048 KB |
Output is correct |
29 |
Correct |
3 ms |
3072 KB |
Output is correct |
30 |
Correct |
4 ms |
3328 KB |
Output is correct |
31 |
Correct |
5 ms |
5632 KB |
Output is correct |
32 |
Correct |
5 ms |
6016 KB |
Output is correct |
33 |
Correct |
5 ms |
6656 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
6784 KB |
Output is correct |
36 |
Correct |
6 ms |
7808 KB |
Output is correct |
37 |
Correct |
5 ms |
6656 KB |
Output is correct |
38 |
Correct |
4 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
249 ms |
13588 KB |
Output is correct |
20 |
Correct |
253 ms |
13680 KB |
Output is correct |
21 |
Correct |
231 ms |
14320 KB |
Output is correct |
22 |
Correct |
211 ms |
14696 KB |
Output is correct |
23 |
Correct |
168 ms |
13680 KB |
Output is correct |
24 |
Correct |
101 ms |
12156 KB |
Output is correct |
25 |
Correct |
227 ms |
20464 KB |
Output is correct |
26 |
Correct |
174 ms |
22704 KB |
Output is correct |
27 |
Correct |
274 ms |
24316 KB |
Output is correct |
28 |
Correct |
661 ms |
41704 KB |
Output is correct |
29 |
Correct |
680 ms |
40556 KB |
Output is correct |
30 |
Correct |
266 ms |
23672 KB |
Output is correct |
31 |
Correct |
275 ms |
23800 KB |
Output is correct |
32 |
Correct |
363 ms |
23800 KB |
Output is correct |
33 |
Correct |
490 ms |
26260 KB |
Output is correct |
34 |
Correct |
454 ms |
25580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
7680 KB |
Output is correct |
23 |
Correct |
5 ms |
6400 KB |
Output is correct |
24 |
Correct |
5 ms |
7296 KB |
Output is correct |
25 |
Correct |
6 ms |
7040 KB |
Output is correct |
26 |
Correct |
3 ms |
3072 KB |
Output is correct |
27 |
Correct |
5 ms |
6784 KB |
Output is correct |
28 |
Correct |
3 ms |
2048 KB |
Output is correct |
29 |
Correct |
3 ms |
3072 KB |
Output is correct |
30 |
Correct |
4 ms |
3328 KB |
Output is correct |
31 |
Correct |
5 ms |
5632 KB |
Output is correct |
32 |
Correct |
5 ms |
6016 KB |
Output is correct |
33 |
Correct |
5 ms |
6656 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
6784 KB |
Output is correct |
36 |
Correct |
6 ms |
7808 KB |
Output is correct |
37 |
Correct |
5 ms |
6656 KB |
Output is correct |
38 |
Correct |
4 ms |
4480 KB |
Output is correct |
39 |
Correct |
249 ms |
13588 KB |
Output is correct |
40 |
Correct |
253 ms |
13680 KB |
Output is correct |
41 |
Correct |
231 ms |
14320 KB |
Output is correct |
42 |
Correct |
211 ms |
14696 KB |
Output is correct |
43 |
Correct |
168 ms |
13680 KB |
Output is correct |
44 |
Correct |
101 ms |
12156 KB |
Output is correct |
45 |
Correct |
227 ms |
20464 KB |
Output is correct |
46 |
Correct |
174 ms |
22704 KB |
Output is correct |
47 |
Correct |
274 ms |
24316 KB |
Output is correct |
48 |
Correct |
661 ms |
41704 KB |
Output is correct |
49 |
Correct |
680 ms |
40556 KB |
Output is correct |
50 |
Correct |
266 ms |
23672 KB |
Output is correct |
51 |
Correct |
275 ms |
23800 KB |
Output is correct |
52 |
Correct |
363 ms |
23800 KB |
Output is correct |
53 |
Correct |
490 ms |
26260 KB |
Output is correct |
54 |
Correct |
454 ms |
25580 KB |
Output is correct |
55 |
Correct |
12 ms |
1792 KB |
Output is correct |
56 |
Correct |
13 ms |
1792 KB |
Output is correct |
57 |
Correct |
121 ms |
14692 KB |
Output is correct |
58 |
Correct |
45 ms |
13032 KB |
Output is correct |
59 |
Correct |
154 ms |
24112 KB |
Output is correct |
60 |
Correct |
696 ms |
52444 KB |
Output is correct |
61 |
Correct |
290 ms |
25096 KB |
Output is correct |
62 |
Correct |
288 ms |
34432 KB |
Output is correct |
63 |
Correct |
379 ms |
34304 KB |
Output is correct |
64 |
Correct |
729 ms |
34456 KB |
Output is correct |
65 |
Correct |
518 ms |
27756 KB |
Output is correct |
66 |
Correct |
731 ms |
49036 KB |
Output is correct |
67 |
Correct |
171 ms |
34784 KB |
Output is correct |
68 |
Correct |
389 ms |
35948 KB |
Output is correct |
69 |
Correct |
370 ms |
36584 KB |
Output is correct |
70 |
Correct |
351 ms |
35304 KB |
Output is correct |