#ifndef TPV
#include "race.h"
#endif
#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int maxn = 2e5 + 5;
const int lim = 1e6 + 5;
int n, k, res, sz[maxn];
vector<pair<int, int>> adj[maxn];
bool del[maxn];
struct IT {
int st[4 * lim];
bool lz[4 * lim];
void clearAll() {
lz[1] = true;
}
void push(int id) {
bool &t = lz[id];
if (!t) return;
st[id << 1] = st[id << 1 | 1] = INF;
lz[id << 1] = lz[id << 1 | 1] = true;
t = false;
}
void update(int id, int l, int r, int u, int v) {
if (l == r) {
minimize(st[id], v);
return;
}
int mid = (l + r) >> 1; push(id);
if (u <= mid) update(id << 1, l, mid, u, v);
else update(id << 1 | 1, mid + 1, r, u, v);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u) {
if (l == r) return st[id];
int mid = (l + r) >> 1; push(id);
if (u <= mid) return get(id << 1, l, mid, u);
return get(id << 1 | 1, mid + 1, r, u);
}
} IT;
void countChild(int u, int p) {
sz[u] = 1;
for (auto g: adj[u]) {
int v = g.F;
if (v == p || del[v]) continue;
countChild(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int n) {
for (auto g: adj[u]) {
int v = g.F;
if (v == p || del[v] || sz[v] <= n / 2) continue;
return centroid(v, u, n);
}
return u;
}
void dfs_calc(int u, int par, int &res, int wei, int depth) {
if (wei > k) return;
int get = IT.get(1, 0, k, k - wei);
if (get < n) minimize(res, depth + get);
for (auto g: adj[u]) {
int v = g.F, w = g.S;
if (del[v] || v == par) continue;
dfs_calc(v, u, res, wei + w, depth + 1);
}
}
void dfs_update(int u, int par, int wei, int depth) {
if (wei > k) return;
IT.update(1, 0, k, wei, depth);
for (auto g: adj[u]) {
int v = g.F, w = g.S;
if (del[v] || v == par) continue;
dfs_update(v, u, wei + w, depth + 1);
}
}
void updateRes(int &res, int root, int n) {
for (auto g: adj[root]) {
int v = g.F, w = g.S;
if (del[v]) continue;
dfs_calc(v, root, res, w, 1);
dfs_update(v, root, w, 1);
}
}
int calc(int u) {
countChild(u, 0); int res = INF;
int n = sz[u];
int root = centroid(u, 0, n);
IT.clearAll();
IT.update(1, 0, k, 0, 0);
updateRes(res, root, n);
del[root] = true;
for (auto g: adj[root]) {
int v = g.F;
if (del[v]) continue;
minimize(res, calc(v));
}
return res;
}
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]].pb(make_pair(H[i][1], L[i]));
adj[H[i][1]].pb(make_pair(H[i][0], L[i]));
}
int getRes = calc(0);
return (getRes == INF ? -1 : getRes);
}
#ifdef TPV
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
int numNodes, desired;
int adjMatrix[15][2], len[15];
cin >> numNodes >> desired;
for (int i = 0; i < numNodes - 1; ++i)
cin >> adjMatrix[i][0] >> adjMatrix[i][1] >> len[i];
cout << best_path(numNodes, desired, adjMatrix, len);
return 0;
}
#endif
# | 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... |