# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
941450 | Mike_Vu | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
const int inf = 1e9;
int n, k, re = inf, sz[MAX], min_length[MAX];
bool deleted[MAX];
vector<int> undo_length;
vector<pair<int, int>> adj[MAX];
stack<pair<int, int>> updates;
int get_size(int u, int pre){
sz[u] = 1;
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
sz[u] += get_size(v, u);
}
return sz[u];
}
int find_centroid(int u, int pre, int target){
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v] and sz[v] * 2 > target){
return find_centroid(v, u, target);
}
return u;
}
void compute(int u, int pre, int len, int sum){
if(sum > k) return;
res = min(res, len + min_length[k - sum]);
updates.push(make_pair(len, sum));
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
compute(v, u, len + 1, sum + w);
}
}
void decompose(int u){
int c = find_centroid(u, -1, get_size(u, -1));
deleted[c] = true;
undo_length.clear();
for(auto [v, w] : adj[c]) if(!deleted[v]){
compute(v, c, 1, w);
while(!updates.empty()){
int len, sum;
tie(len, sum) = updates.top(); updates.pop();
min_length[sum] = min(min_length[sum], len);
undo_length.push_back(sum):
}
}
for(auto v : undo_length) min_length[v] = inf;
for(auto [v, w] : adj[c]) if(!deleted[v]){
decompose(v);
}
}
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 u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
fill(min_length + 1, min_length + 1 + k, inf);
decompose(1);
if(res >= inf) res = -1;
return res;
}
//#define Zero_OP
#ifdef Zero_OP
void init(void){
}
void process(){
int H[2][2];
H[0][0] = 0;
H[0][1] = 1;
H[1][0] = 1;
H[1][1] = 2;
int L[2];
L[0] = 1;
L[1] = 1;
cout << best_path(3, 3, H, L) << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
init();
process();
return 0;
}
#endif