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"
using namespace std;
using pi = pair<int, int>;
using vpi = vector<pi>;
#define pb push_back
#define f first
#define s second
const int INF = 1000000010;
int N, K, ans = INF; vpi adj[200010]; int sub[200010]; bool vis[200010]; unordered_map<int, int> cur;
void DFS1(int X, int P) {
sub[X] = 1; for (pi Y : adj[X]) if (Y.f != P && !vis[Y.f]) { DFS1(Y.f, X); sub[X] += sub[Y.f]; }
}
int getCent(int X, int P, int S) {
for (pi Y : adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return getCent(Y.f, X, S); return X;
}
void DFS2(int X, int P, bool upd, int dist, int depth) {
if (dist > K) return ;
if (upd) { if (cur.count(dist)) cur[dist] = min(cur[dist], depth); else cur[dist] = depth; }
else { if (cur.count(K - dist)) ans = min(ans, cur[K - dist] + depth); }
for (pi Y : adj[X]) if (Y.f != P && !vis[Y.f]) DFS2(Y.f, X, upd, dist + Y.s, depth + 1);
}
void decomp(int X) {
DFS1(X, 0); int C = getCent(X, 0, sub[X]); vis[C] = true; cur[0] = 0;
for (pi Y : adj[X]) if (!vis[Y.f]) { DFS2(Y.f, C, false, Y.s, 1); DFS2(Y.f, C, true, Y.s, 1); }
cur.clear(); for (pi Y : adj[X]) if (!vis[Y.f]) decomp(Y.f);
}
int best_path(int _N, int _K, int H[][2], int L[]) {
N = _N; K = _K; for (int i = 1; i < N; i++) {
int A = H[i][0] + 1; int B = H[i][1] + 1;
adj[A].pb({B, L[i]}), adj[B].pb({A, L[i]});
}
decomp(1); return (ans == INF ? -1 : ans);
}
Compilation message (stderr)
race.cpp: In function 'int getCent(int, int, int)':
race.cpp:22:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
22 | for (pi Y : adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return getCent(Y.f, X, S); return X;
| ^~~
race.cpp:22:99: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
22 | for (pi Y : adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return getCent(Y.f, X, S); return X;
| ^~~~~~
# | 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... |