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>
#include "race.h"
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int MXN = 1e6;
const int mxsz = 2e5 + 3;
int n, k;
vector<pii> adj[mxsz];
int sz[mxsz], dep[mxsz], bc[mxsz], in[mxsz], out[mxsz], flat[mxsz];
long long dist[mxsz];
int sack[MXN + 10];
void getsz(int u, int prv = -1){
static int tme = 0;
in[u] = ++tme; flat[tme] = u;
sz[u] = 1;
for (int i = 0, nx, w; i < adj[u].size(); i++){
nx = adj[u][i].fi; w = adj[u][i].se;
if (nx == prv) continue;
dep[nx] = dep[u] + 1;
dist[nx] = dist[u] + w;
getsz(nx, u);
sz[u] += sz[nx];
if (bc[u] == -1 || sz[nx] > sz[bc[u]]) bc[u] = nx;
}
out[u] = tme;
}
int ans = 0x3f3f3f3f;
void dfs(int u, int prv, bool keep){
for (int i = 0, nx, w; i < adj[u].size(); i++){
nx = adj[u][i].fi;
if (nx == prv || nx == bc[u]) continue;
w = dist[nx] - dist[u];
dfs(nx, u, 0);
}
if (bc[u] != -1) dfs(bc[u], u, 1);
for (int i = 0, nx, w; i < adj[u].size(); i++){
nx = adj[u][i].fi;
if (nx == prv || nx == bc[u]) continue;
for (int j = in[nx]; j <= out[nx]; j++){
int x = flat[j]; long long w = dist[x] - dist[u];
if (w > k) continue;
// cout << " insert " << x << ' ' << w << '\n';
if (dep[x] < dep[sack[w]]) sack[w] = x;
}
}
sack[0] = u;
// cout << " begin calc " << u << '\n';
// for (int i = 0; i <= k; i++) cout << sack[i] << " \n"[i==k];
for (int i = in[u]; i <= out[u]; i++){
int x = flat[i]; long long w = dist[x] - dist[u];
// cout << "from " << x << " dist " << w << '\n';
if (w <= k && sack[k-w] != -1){
ans = min(ans, dep[x] + dep[sack[k-w]] - 2 * dep[u]);
}
}
if (!keep){
for (int i = in[u]; i <= out[u]; i++){
sack[dist[flat[i]] - dist[u]] = -1;
}
// cout << "clear\n";
}
// cout << "end calc " << u << '\n' << '\n';
}
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]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
memset(bc, -1, sizeof(bc));
memset(sack, -1, sizeof(sack));
// for (int i = 0; i < n; i++){
// cout << i << " : ";
// for (pii p : adj[i]) cout << '{' << p.fi << ", " << p.se << "} ";
// cout << '\n';
// }
getsz(0);
// for (int i = 0; i < n; i++){
// cout << dep[i] << ' ' << dist[i] << ",\n"[i==n-1];
// }
dfs(0, -1, 1);
if (ans == 0x3f3f3f3f) return -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void getsz(int, int)':
race.cpp:22:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0, nx, w; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, bool)':
race.cpp:37:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0, nx, w; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp:37:22: warning: variable 'w' set but not used [-Wunused-but-set-variable]
for (int i = 0, nx, w; i < adj[u].size(); i++){
^
race.cpp:44:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0, nx, w; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp:44:22: warning: unused variable 'w' [-Wunused-variable]
for (int i = 0, nx, w; i < adj[u].size(); i++){
^
# | 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... |