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>
using namespace std;
const int N = 100005;
const int M = 1000005;
const int INF = 0x3f3f3f;
int K;
int res = INF;
int sz[N], h[N];
int f[M];
long long d[N];
bool del[N];
vector<int> buf;
vector< pair<int, int> > G[N];
void add(int u, int v, int w) {
G[u].push_back({v, w});
G[v].push_back({u, w});
}
void dfs(int u, int p) {
sz[u] = 1;
buf.push_back(u);
for (auto v : G[u]) {
if (p == v.first || del[v.first]) continue;
dfs(v.first, u), sz[u] += sz[v.first];
}
}
int find(int u, int p, int r) {
for (auto v : G[u]) {
if (p == v.first || del[v.first]) continue;
if (2 * sz[v.first] >= sz[r]) return find(v.first, u, r);
}
return u;
}
void query(int u, int p) {
if (d[u] <= K) res = min(res, h[u] + f[K - d[u]]);
for (auto v : G[u]) {
if (p == v.first || del[v.first]) continue;
h[v.first] = h[u] + 1, d[v.first] = d[u] + v.second;
query(v.first, u);
}
}
void add(int u, int p) {
if (d[u] <= K) f[d[u]] = min(f[d[u]], h[u]);
for (auto v : G[u]) {
if (p == v.first || del[v.first]) continue;
add(v.first, u);
}
}
void solve(int u) {
buf.clear(), dfs(u, u);
u = find(u, u, u), del[u] = 1;
f[0] = 0;
for (auto v : G[u]) {
if (del[v.first]) continue;
d[v.first] = v.second, h[v.first] = 1;
query(v.first, u), add(v.first, u);
}
for (auto i : buf) f[h[i]] = INF;
for (auto v : G[u]) {
if (del[v.first]) continue; solve(v.first);
}
}
int best_path(int n, int _K, int H[][2], int L[]) {
K = _K;
for (int i = 0; i < (n - 1); ++i) {
add(H[i][0], H[i][1], L[i]);
}
memset(f, INF, sizeof f);
solve(0);
return res == INF ? -1 : res;
}
Compilation message (stderr)
race.cpp: In function 'void solve(int)':
race.cpp:70:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if (del[v.first]) continue; solve(v.first);
^~
race.cpp:70:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if (del[v.first]) continue; solve(v.first);
^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |