Submission #1084572

#TimeUsernameProblemLanguageResultExecution timeMemory
1084572avighnaFirefighting (NOI20_firefighting)C++17
22 / 100
263 ms52672 KiB
#include <bits/stdc++.h> typedef long long ll; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); ll n, k; std::cin >> n >> k; std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1); for (ll i = 0, u, v, w; i < n - 1; ++i) { std::cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } // dp[node][0] => you need to cover parent // dp[node][1] => parent covers you // dp[node][2] => you only need to cover your subtree std::vector<std::vector<ll>> dp(n + 1, std::vector<ll>(3, 1e15)); std::vector<bool> is_leaf(n + 1, true); std::function<void(ll, ll, ll)> dfs; dfs = [&](ll node, ll par, ll wt) { ll sum = 0; for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } is_leaf[node] = false; dfs(ch, node, w); sum += dp[ch][2]; } if (is_leaf[node]) { dp[node][0] = wt <= k ? 1 : 1e15; dp[node][1] = wt <= k ? 0 : 1e15; dp[node][2] = 1; } else { dp[node][0] = 1; dp[node][1] = 0; for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } if (w <= k) { dp[node][2] = std::min(dp[node][2], sum - dp[ch][2] + dp[ch][0]); dp[node][0] += dp[ch][1]; } else { dp[node][0] += dp[ch][2]; } dp[node][1] += dp[ch][2]; } dp[node][1] = std::min(dp[node][1], dp[node][0]); dp[node][2] = std::min(dp[node][2], dp[node][0]); } }; dfs(1, 0, 0); std::vector<ll> ans; std::function<void(ll, ll, ll)> build_ans; build_ans = [&](ll node, ll par, ll j) { if (is_leaf[node]) { if (j == 0 or j == 2) { ans.push_back(node); } return; } if (j == 0) { ans.push_back(node); for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } if (w <= k) { build_ans(ch, node, 1); } else { build_ans(ch, node, 2); } } } else if (j == 1) { if (dp[node][1] == dp[node][0]) { build_ans(node, par, 0); return; } for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } build_ans(ch, node, 2); } } else { if (dp[node][2] == dp[node][0]) { build_ans(node, par, 0); return; } ll sum = 0; for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } sum += dp[ch][2]; } bool found = false; for (auto &[ch, w] : adj[node]) { if (ch == par) { continue; } if (!found and dp[node][2] == sum - dp[ch][2] + dp[ch][0]) { found = true; build_ans(ch, node, 0); } else { build_ans(ch, node, 2); } } } }; build_ans(1, 0, 2); std::cout << dp[1][2] << '\n'; for (auto &i : ans) { std::cout << i << ' '; } std::cout << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...