# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
456204 | sean617 | Race (IOI11_race) | C++98 | 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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define N 200005
#define A 1000005
#include "race.h"
using namespace std;
int n, k, mn, M = 1e9, s[A], sz[N];
bool v2[N];
vector<int> a[N], b[N];
queue<int> qu;
int gs(int p, int q) {
int i, t;
sz[p] = 1;
for (i = 0; i < a[p].size(); i++) {
t = a[p][i];
if (t == q || v2[t]) continue;
sz[p] += gs(t, p);
}
return sz[p];
}
int gc(int p, int q, int nn) {
int i, t;
for (i = 0; i < a[p].size(); i++) {
t = a[p][i];
if (t != q && !v2[t] && sz[t] *2 > nn) return gc(t, p, nn);
}
return p;
}
void g(int p, int q, int w, int cnt) {
int i, t;
if (w > k) return;
mn = min(mn, cnt + s[k - w]);
for (i = 0; i < a[p].size(); i++) {
t = a[p][i];
if (t == q || v2[t]) continue;
g(t, p, w + b[p][i], cnt + 1);
}
}
void up(int p, int q, int w, int cnt) {
int i, t;
if (w > k) return;
if (s[w] > cnt) {
s[w] = cnt;
qu.push(w);
}
for (i = 0; i < a[p].size(); i++) {
t = a[p][i];
if (t == q || v2[t]) continue;
up(t, p, w + b[p][i], cnt + 1);
}
}
void f(int p) {
int i, t, cen;
gs(p, -1);
cen = gc(p, -1, sz[p]);
v2[cen] = 1;
while (!qu.empty()) {
t = qu.front(); qu.pop();
s[t] = M;
}
s[0] = 0;
for (i = 0; i < a[cen].size(); i++) {
t = a[cen][i];
if (v2[t]) continue;
g(t, -1, b[cen][i], 1);
up(t, -1, b[cen][i], 1);
}
for (i = 0; i < a[cen].size(); i++) {
t = a[cen][i];
if (v2[t]) continue;
f(t);
}
}
int best_path(int NN, int KK, int H[][2], int L[])
{
int i, t1, t2, t3;
n = NN;
k = KK;
for (i = 0; i < n - 1; i++) {
t1 = H[i][0];
t2 = H[i][1];
t3 = L[i];
a[t1].push_back(t2);
b[t1].push_back(t3);
a[t2].push_back(t1);
b[t2].push_back(t3);
}
mn = M;
for (i = 0; i <= k; i++) s[i] = M;
f(0);
if (mn == M) mn = -1;
return mn;
}