# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
600198 | Meloric | 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 "race.h"
#include <bits/stdc++.h>
#define pb push_back
#define int int64_t
#define pii pair<int, int>
#define X first
#define Y second
#define all(x) (x).begin(),(x).end()
#define lb lower_bound
#define ub upper_bound
using namespace std;
const int inf = 1e9;
void p(auto A){
for(auto e : A)cout << e << ' ';
cout << '\n';
}
struct special_set{
set<pii> S;
pii delta = {0, 0};
void insert(pii e){
S.insert({e.X-delta.X, e.Y-delta.Y});
}
void update(pii dx){
delta.X+=dx.X;
delta.Y+=dx.Y;
}
int find(int x){
x-=delta.X;
if(S.lb({x, -inf}) == S.end())return inf;
if(S.lb({x, -inf})->X != x)return inf;
return S.lb({x, -inf})->Y+delta.Y;
}
pii pop(){
pii ret = *S.begin();
S.erase(S.begin());
ret.X+=delta.X;
ret.Y+=delta.Y;
return ret;
}
int size(){
return S.size();
}
void p(){
//for(auto [e, f] : S)cout << e+delta.X << ' '<< f+delta.Y << endl;
}
};
int best_path(int N, int K, int H[][2], int L[])
{
int n = N;
int k = K;
vector<vector<pii>> g(n, vector<pii>());
for(int i = 0; i< n-1; i++){
int c = H[i][0];
int d = H[i][1];
int w = L[i];
g[c].pb({d, w});
g[d].pb({c, w});
}
int ret = inf;
auto dfs = [&](auto&& self, int u, int p)->special_set{
//cout << u << endl;
special_set S;
S.insert({0, 0});
for(auto [v, w] : g[u])if(v!=p){
special_set O = self(self, v, u);
O.update(make_pair(w, 1));
if(S.size() < O.size())swap(S, O);
if(u == 0)S.p();
while(O.size()){
pii e = O.pop();
//cout << u << ' '<< e.X << ' '<< e.Y << ' ' << endl;
ret = min(ret, e.Y+S.find(k-e.X));
S.insert(e);
}
}
return S;
};
auto S = dfs(dfs, 0, 0);
while(S.size()){
auto e = S.pop();
//cout << e.X << ' '<< e.Y << ' ';
}
if(ret == inf)return -1;
return ret;
}
/*
void solve(){
int N, K, H[100][2], L[100];
cin >> N >> K;
for(int i = 0; i< N-1; i++){
cin >> H[i][0] >> H[i][1] >> L[i];
}
cout << best_path(N, K, H, L);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--)solve();
}
*/