# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
935225 |
2024-02-28T21:17:08 Z |
Bula |
Race (IOI11_race) |
C++17 |
|
1915 ms |
77868 KB |
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int mod = 1e9 + 7, N = 2e5 + 5;
int n, k, ans = INT_MAX;
vector<int> adj[N], subtree_size(N);
vector<bool> is_removed(N);
vector<pair<int, int>> cur;
map<pair<int, int>, int> l;
void dfs(int v, int par, int dist, int edge){
if(dist > k || edge > ans) return;
cur.push_back({dist, edge});
for(auto to : adj[v]){
if(to == par || is_removed[to]) continue;
dfs(to, v, dist + l[{v, to}], edge + 1);
}
}
int get_subtree_size(int node, int parent = -1) {
subtree_size[node] = 1;
for (int child : adj[node]) {
if (child == parent || is_removed[child]) { continue; }
subtree_size[node] += get_subtree_size(child, node);
}
return subtree_size[node];
}
int get_centroid(int node, int tree_size, int parent = -1) {
for (int child : adj[node]) {
if (child == parent || is_removed[child]) { continue; }
if (subtree_size[child] * 2 > tree_size) {
return get_centroid(child, tree_size, node);
}
}
return node;
}
void build_centroid_decomp(int node = 0) {
int centroid = get_centroid(node, get_subtree_size(node));
map<int, int> mp;
for(int x : adj[centroid]){
if(is_removed[x]) continue;
dfs(x, centroid, l[{centroid, x}], 1);
for(auto pr : cur){
if(pr.first == k) ans = min(ans, pr.second);
else if(mp.find(k - pr.first) != mp.end()){
ans = min(ans, mp[k - pr.first] + pr.second);
}
}
for(auto pr : cur){
if(mp.find(pr.first) == mp.end()){
mp[pr.first] = pr.second;
}else mp[pr.first] = min(mp[pr.first], pr.second);
}
cur.clear();
}
is_removed[centroid] = true;
for (int child : adj[centroid]) {
if (is_removed[child]) { continue; }
build_centroid_decomp(child);
}
}
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 x = H[i][0], y = H[i][1];
adj[x].push_back(y);
adj[y].push_back(x);
l[{x, y}] = l[{y, x}] = L[i];
}
build_centroid_decomp();
return (ans == INT_MAX ? -1 : ans);
}
//main(){
// ios::sync_with_stdio(0);
// cin.tie(0),cout.tie(0);
// int tt = 1; //cin >> tt;
// while(tt--){
// int N, K;
// cin >> N >> K;
// int H[N][2], L[N];
// 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) << endl;
// }
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9604 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
3 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9604 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
3 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9560 KB |
Output is correct |
22 |
Correct |
4 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
9564 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
4 ms |
9816 KB |
Output is correct |
29 |
Correct |
4 ms |
9816 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
5 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
4 ms |
9612 KB |
Output is correct |
36 |
Correct |
4 ms |
9564 KB |
Output is correct |
37 |
Correct |
4 ms |
9820 KB |
Output is correct |
38 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9604 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
3 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
294 ms |
27476 KB |
Output is correct |
20 |
Correct |
262 ms |
28472 KB |
Output is correct |
21 |
Correct |
321 ms |
28452 KB |
Output is correct |
22 |
Correct |
370 ms |
29296 KB |
Output is correct |
23 |
Correct |
257 ms |
28776 KB |
Output is correct |
24 |
Correct |
159 ms |
29012 KB |
Output is correct |
25 |
Correct |
399 ms |
32684 KB |
Output is correct |
26 |
Correct |
250 ms |
36484 KB |
Output is correct |
27 |
Correct |
412 ms |
45264 KB |
Output is correct |
28 |
Correct |
571 ms |
60536 KB |
Output is correct |
29 |
Correct |
583 ms |
59588 KB |
Output is correct |
30 |
Correct |
385 ms |
45812 KB |
Output is correct |
31 |
Correct |
422 ms |
45756 KB |
Output is correct |
32 |
Correct |
557 ms |
45632 KB |
Output is correct |
33 |
Correct |
768 ms |
45388 KB |
Output is correct |
34 |
Correct |
428 ms |
45544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9604 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
3 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9560 KB |
Output is correct |
22 |
Correct |
4 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
9564 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
4 ms |
9816 KB |
Output is correct |
29 |
Correct |
4 ms |
9816 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
5 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
4 ms |
9612 KB |
Output is correct |
36 |
Correct |
4 ms |
9564 KB |
Output is correct |
37 |
Correct |
4 ms |
9820 KB |
Output is correct |
38 |
Correct |
4 ms |
9820 KB |
Output is correct |
39 |
Correct |
294 ms |
27476 KB |
Output is correct |
40 |
Correct |
262 ms |
28472 KB |
Output is correct |
41 |
Correct |
321 ms |
28452 KB |
Output is correct |
42 |
Correct |
370 ms |
29296 KB |
Output is correct |
43 |
Correct |
257 ms |
28776 KB |
Output is correct |
44 |
Correct |
159 ms |
29012 KB |
Output is correct |
45 |
Correct |
399 ms |
32684 KB |
Output is correct |
46 |
Correct |
250 ms |
36484 KB |
Output is correct |
47 |
Correct |
412 ms |
45264 KB |
Output is correct |
48 |
Correct |
571 ms |
60536 KB |
Output is correct |
49 |
Correct |
583 ms |
59588 KB |
Output is correct |
50 |
Correct |
385 ms |
45812 KB |
Output is correct |
51 |
Correct |
422 ms |
45756 KB |
Output is correct |
52 |
Correct |
557 ms |
45632 KB |
Output is correct |
53 |
Correct |
768 ms |
45388 KB |
Output is correct |
54 |
Correct |
428 ms |
45544 KB |
Output is correct |
55 |
Correct |
24 ms |
11608 KB |
Output is correct |
56 |
Correct |
21 ms |
11100 KB |
Output is correct |
57 |
Correct |
281 ms |
28120 KB |
Output is correct |
58 |
Correct |
90 ms |
28696 KB |
Output is correct |
59 |
Correct |
399 ms |
43840 KB |
Output is correct |
60 |
Correct |
1915 ms |
77868 KB |
Output is correct |
61 |
Correct |
502 ms |
45228 KB |
Output is correct |
62 |
Correct |
552 ms |
46144 KB |
Output is correct |
63 |
Correct |
784 ms |
45828 KB |
Output is correct |
64 |
Correct |
1483 ms |
56196 KB |
Output is correct |
65 |
Correct |
532 ms |
46272 KB |
Output is correct |
66 |
Correct |
1399 ms |
56760 KB |
Output is correct |
67 |
Correct |
477 ms |
47312 KB |
Output is correct |
68 |
Correct |
510 ms |
45736 KB |
Output is correct |
69 |
Correct |
577 ms |
43860 KB |
Output is correct |
70 |
Correct |
483 ms |
44896 KB |
Output is correct |