// https://oj.uz/problem/view/IOI11_race
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<vector<pair<int, int>>> adj;
int n, k;
vector<bool> is_rem;
vector<int> sz;
vector<ll> distL; //ll
vector<int> dist;
unordered_map<int, int> buf_k_dist;
unordered_map<int, int> k_dist;
int sol;
void get_sz(int v, int e){
sz[v]=1;
for(auto edge: adj[v]){
int u = edge.first;
if(u==e||is_rem[u]) continue;
get_sz(u, v); sz[v] += sz[u];
}
return;
}
void get_dist(int v, int e){
if(distL[v]<=k){
if(buf_k_dist.count(distL[v])==0) buf_k_dist[distL[v]] = dist[v];
else if(buf_k_dist[distL[v]] > dist[v]) buf_k_dist[distL[v]] = dist[v];
}
for(auto edge: adj[v]){
int u = edge.first;
if(u==e||is_rem[u]) continue;
dist[u]=dist[v]+1;
distL[u]=distL[v]+edge.second;
get_dist(u, v);
}
return;
}
int find_centr(int v, int e, int siz){
for(auto edge: adj[v]){
int u = edge.first;
if(u==e||is_rem[u]) continue;
if(sz[u] > siz/2) return find_centr(u, v, siz);
}
return v;
}
void decompose(int v, int siz){
get_sz(v, v);
int c = find_centr(v, v, siz);
dist[c]=0; distL[c]=0;
k_dist[0]=0;
for(auto edge: adj[c]){
int u = edge.first;
if(is_rem[u]) continue;
dist[u]=1;
distL[u] = edge.second;
get_dist(u, c);
for(auto [key, val] : buf_k_dist){
if(k_dist.count(k-key)!=0){
sol = min(sol, val+ k_dist[k-key]);
}
}
for(auto [key, val] : buf_k_dist){
if(k_dist.count(key)!=0){
k_dist[key] = min(k_dist[key], val);
}
else k_dist[key] = val;
}
buf_k_dist.clear();
}
k_dist.clear();
get_sz(c, c);
is_rem[c]=1;
for(auto edge: adj[c]){
int u = edge.first;
if(is_rem[u]) continue;
decompose(u, sz[u]);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
adj.resize(N);
sz.resize(n);
is_rem.resize(n, 0);
distL.resize(n);
dist.resize(n);
sol = INT_MAX;
for(int i = 0; i<n-1; i++){
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
decompose(0, n);
return (sol==INT_MAX ? -1 : sol);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2492 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2500 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
136 ms |
12380 KB |
Output is correct |
20 |
Correct |
129 ms |
13136 KB |
Output is correct |
21 |
Correct |
128 ms |
12880 KB |
Output is correct |
22 |
Correct |
106 ms |
13732 KB |
Output is correct |
23 |
Correct |
90 ms |
13744 KB |
Output is correct |
24 |
Correct |
53 ms |
13656 KB |
Output is correct |
25 |
Correct |
130 ms |
17660 KB |
Output is correct |
26 |
Correct |
73 ms |
18772 KB |
Output is correct |
27 |
Correct |
137 ms |
21588 KB |
Output is correct |
28 |
Correct |
253 ms |
32852 KB |
Output is correct |
29 |
Correct |
263 ms |
32336 KB |
Output is correct |
30 |
Correct |
143 ms |
22100 KB |
Output is correct |
31 |
Correct |
138 ms |
22260 KB |
Output is correct |
32 |
Correct |
176 ms |
22096 KB |
Output is correct |
33 |
Correct |
244 ms |
21144 KB |
Output is correct |
34 |
Correct |
179 ms |
21328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2492 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2500 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
136 ms |
12380 KB |
Output is correct |
40 |
Correct |
129 ms |
13136 KB |
Output is correct |
41 |
Correct |
128 ms |
12880 KB |
Output is correct |
42 |
Correct |
106 ms |
13732 KB |
Output is correct |
43 |
Correct |
90 ms |
13744 KB |
Output is correct |
44 |
Correct |
53 ms |
13656 KB |
Output is correct |
45 |
Correct |
130 ms |
17660 KB |
Output is correct |
46 |
Correct |
73 ms |
18772 KB |
Output is correct |
47 |
Correct |
137 ms |
21588 KB |
Output is correct |
48 |
Correct |
253 ms |
32852 KB |
Output is correct |
49 |
Correct |
263 ms |
32336 KB |
Output is correct |
50 |
Correct |
143 ms |
22100 KB |
Output is correct |
51 |
Correct |
138 ms |
22260 KB |
Output is correct |
52 |
Correct |
176 ms |
22096 KB |
Output is correct |
53 |
Correct |
244 ms |
21144 KB |
Output is correct |
54 |
Correct |
179 ms |
21328 KB |
Output is correct |
55 |
Correct |
18 ms |
3416 KB |
Output is correct |
56 |
Correct |
9 ms |
3160 KB |
Output is correct |
57 |
Correct |
75 ms |
12764 KB |
Output is correct |
58 |
Correct |
29 ms |
13516 KB |
Output is correct |
59 |
Correct |
2756 ms |
24140 KB |
Output is correct |
60 |
Execution timed out |
3035 ms |
46408 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |