# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
790477 | kilikuma | 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 pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
using namespace std;
#define MAXN 200010
#define MAXK 1000010
vector<pii> adj[MAXN];
int N, K, siz[MAXN], res, b[MAXK];
bool vis[MAXN];
int dfsSize(int node, int prev) {
siz[node] = 1;
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi]) siz[node] += dfsSize(x.fi, node);
}
return siz[node];
}
int dfsCentroid(int node, int prev, int S) {
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi] && siz[x.fi] > S/2) return dfsCentroid(x.fi, node, S);
}
return node;
}
vector<int> v;
vector<pii> vt;
void dfsSolve(int node, int prev, int len, int dub) {
vt.pb({dub, len});
v.pb(len);
res = min(res, b[K-len]+dub);
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi] && len+x.se <= K) dfsSolve(x.fi, node, len+x.se, dub+1);
}
}
void centroidDecomp(int node) {
int S = dfsSize(node, node);
node = dfsCentroid(node, node, S);
vis[node] = 1;
for(auto x : adj[node]) {
if(!vis[x.fi] && x.se <= K) dfsSolve(x.fi, node, x.se, 1);
for(auto y : vt) {
b[y.se] = min(b[y.se], y.fi);
}
res = min(res, b[K]);
vt.clear();
}
for(auto x : v) b[x] = N;
v.clear();
for(auto x : adj[node]) if(!vis[x.fi]) centroidDecomp(x.fi);
}
int best_path(int n, int k, int H[][2], int L[]) {
N = n; K = k;
for(int i = 0; i < N-1; i++) {
adj[H[i][0]+1].pb({H[i][1]+1, L[i]});
adj[H[i][1]+1].pb({H[i][0]+1, L[i]});
}
res = N;
for(int i = 0; i <= K; i++) b[i] = N;
centroidDecomp(1);
return (res == N ? -1:res);
}