Submission #939805

#TimeUsernameProblemLanguageResultExecution timeMemory
939805duckindogRace (IOI11_race)C++17
0 / 100
4 ms13660 KiB
#include<bits/stdc++.h> using namespace std; #ifndef LOCAL #include "race.h" #endif const int N = 200'000 + 10; int n, k; vector<pair<int, int>> ad[N]; int answer = 1e7; int tsz; int sz[N]; bool mk[N]; void dfs1(int u, int pre = -1) { sz[u] = 1; for (const auto& [v, w] : ad[u]) { if (v == pre || mk[v]) continue; dfs1(v, u); sz[u] += sz[v]; } tsz = sz[u]; } int dfs2(int u, int pre = -1) { for (const auto& [v, w] : ad[u]) { if (v == pre || mk[v]) continue; if (sz[v] > tsz / 2) return dfs2(v, u); } return u; } int f[1'000'010]; vector<pair<int, int>> lst; vector<int> undo; void clr() { for (const auto& i : undo) if (i <= k) f[i] = 1e7; f[0] = 0; undo.clear(); } void dfs3(int u, int pre, int d, int sw) { lst.push_back({d, sw}); undo.push_back(sw); for (const auto& [v, w] : ad[u]) { if (v == pre) continue; dfs3(v, u, d + 1, sw + w); } } void dfs4(int u) { dfs1(u, -1); mk[u = dfs2(u, -1)] = true; clr(); for (const auto& [v, w] : ad[u]) { if (mk[v]) continue; lst.clear(); dfs3(v, u, 1, w); for (const auto& [d, sw] : lst) if (sw <= k) answer = min(answer, d + f[k - sw]); for (const auto& [d, sw] : lst) if (sw <= k) f[sw] = min(f[sw], d); } for (const auto& [v, w] : ad[u]) if (!mk[v]) dfs4(v); } int best_path(int a, int b, int h[][2], int l[]) { n = a; k = b; for (int i = 0; i < n; ++i) { int u = h[i][0], v = h[i][1], w = l[i]; ad[u].push_back({v, w}); ad[v].push_back({u, w}); } memset(f, 40, sizeof f); dfs4(0); return answer > n ? -1 : answer; } #ifdef LOCAL int h[N][2], l[N]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for (int i = 0; i < n - 1; ++i) cin >> h[i][0] >> h[i][1] >> l[i]; cout << best_path(n, k, h, l) << "\n"; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...