# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1245213 | huydayy | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define TASK "lqdrace"
#define int long long
#define double long double
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<ii>
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define sz(x) (long long)x.size()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define FORV(v, H) for (auto &v: H)
#define __builtin_popcount __builtin_popcountll
#define BIT(mask, i) ((mask >> i) & 1ll)
#define MASK(i) (1ll << (i))
#define ONBIT(mask, i) (mask (1ll << (i)))
#define OFFBIT(mask, i) (mask &~ (1ll << (i)))
#define mid(l, r) ((l + r) >> 1)
#define left(id) (id << 1)
#define right(id) ((id << 1) 1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define UNIQUE(x) sort(all(x)); x.resize(distance(x.begin(), unique(all(x))))
#define BUG1(a) cout << a << '\n'
#define BUG2(a, b) cout << a << " " << b << '\n'
#define BUG3(a, b, c) cout << a << " " << b << " " << c << '\n'
#define BUG4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << '\n'
#define BUG5(a, b, c, d, e) cout << a << " " << b << " " << c << " " << d << " " << e << '\n'
#define BUG6(a, b, c, d, e, f) cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << '\n'
using namespace std;
template <class X, class Y> bool maximize(X &A, const Y &B){
if (A < B) return A = B, true;
return false;
}
template <class X, class Y> bool minimize(X &A, const Y &B){
if (A > B) return A = B, true;
return false;
}
const int oo = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int LOG = 16;
const int base = 311;
const int BLOCK = 317;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
int N, K;
int sz[MAXN], timer[MAXN * 10], mn[MAXN * 10];
bool del[MAXN];
vii G[MAXN];
int DFS(int u, int parent){
sz[u] = 1;
FORV(V, G[u]){
int v = V.fi;
if (v == parent || del[v]) continue;
DFS(v, u);
sz[u] += sz[v];
}
return sz[u];
}
int FIND(int u, int parent, int Size){
FORV(V, G[u]){
int v = V.fi;
if (v == parent || del[v]) continue;
if (sz[v] > Size) return FIND(v, u, Size);
}
return u;
}
int cnt = 0, res = +oo;
void update(int u, int parent, int w, int d){
if (w > K) return;
if (timer[w] != cnt) mn[w] = d;
else mn[w] = min(mn[w], d);
timer[w] = cnt;
FORV(V, G[u]){
int v = V.fi, c = V.se;
if (v == parent || del[v]) continue;
update(v, u, w + c, d + 1);
}
}
void calc(int u, int parent, int w, int d){
if (w > K) return;
if (timer[K - w] == cnt) minimize(res, mn[K - w] + d);
FORV(V, G[u]){
int v = V.fi, c = V.se;
if (v == parent || del[v]) continue;
calc(v, u, w + c, d + 1);
}
}
void solve(int u, int last_centroid){
int Size = DFS(u, last_centroid);
int centroid = FIND(u, last_centroid, Size >> 1ll);
cnt++;
timer[0] = cnt;
FORV(V, G[centroid]){
int v = V.fi, c = V.se;
if (v == last_centroid || del[v]) continue;
calc(v, centroid, c, 1);
update(v, centroid, c, 1);
}
del[centroid] = 1;
FORV(V, G[centroid]){
int v = V.fi;
if (v == last_centroid || del[v]) continue;
solve(v, centroid);
}
}
void SOLVE(){
reset(mn, +oo);
mn[0] = 0;
solve(1, 0);
if (res == +oo) cout << -1 << '\n';
else cout << res << '\n';
}
main(){
if (fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> K;
FOR(i, 1, N - 1){
int u, v, c;
cin >> u >> v >> c;
u++; v++;
G[u].push_back({v, c});
G[v].push_back({u, c});
}
SOLVE();
return 0;
}
/*
___ __ ___ _______ ___ ___ ________ ___ ________ ___ ___ ___ ___ ___ ___
\ \\ \ |\ \|\ ___ \ |\ \|\ \ |\ ____\|\ \|\ __ \ |\ \|\ \|\ \|\ \ |\ \ / / |
\ \ \/ /|\ \ \ \ __/|\ \ \\\ \ \ \ \___|\ \ \ \ \|\ \ \ \ \\\ \ \ \\\ \ \ \ \/ / /
\ \ ___ \ \ \ \ \_|/_\ \ \\\ \ \ \ \ __\ \ \ \ __ \ \ \ __ \ \ \\\ \ \ \ / /
\ \ \\ \ \ \ \ \ \_|\ \ \ \\\ \ \ \ \|\ \ \ \ \ \ \ \ \ \ \ \ \ \ \\\ \ \/ / /
\ \__\\ \__\ \__\ \_______\ \_______\ \ \_______\ \__\ \__\ \__\ \ \__\ \__\ \_______\__/ / /
\|__| \|__|\|__|\|_______|\|_______| \|_______|\|__|\|__|\|__| \|__|\|__|\|_______|\___/ /
\|___|/
Author: Kieu Gia Huy a.k.a kiwi
From: C.H.V with luv <3
...
*/