제출 #1084601

#제출 시각아이디문제언어결과실행 시간메모리
1084601avighnaFirefighting (NOI20_firefighting)C++17
43 / 100
3080 ms70848 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); std::set<ll> active; for (ll i = 1; i <= n; ++i) { active.insert(i); } 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}); } std::function<void(ll, ll, ll)> dfs; std::vector<std::pair<ll, ll>> up(n + 1); std::vector<ll> depths(n + 1); dfs = [&](ll node, ll par, ll depth) { depths[node] = depth; for (auto &[i, wt] : adj[node]) { if (i == par or !active.count(i)) { continue; } up[i] = {node, wt}; dfs(i, node, depth + wt); } }; dfs(1, 0, 0); std::priority_queue<std::pair<ll, ll>> pq; for (ll i = 1; i <= n; ++i) { pq.push({depths[i], i}); } std::vector<ll> ans; while (!pq.empty()) { ll deepest = pq.top().second; pq.pop(); if (!active.count(deepest)) { continue; } ll dist = 0; while (deepest != 1 and dist + up[deepest].second <= k) { dist += up[deepest].second; deepest = up[deepest].first; } ans.push_back(deepest); std::function<void(ll, ll, ll)> dfs2; dfs2 = [&](ll node, ll par, ll depth) { if (depth > k) { return; } active.erase(node); for (auto &[i, wt] : adj[node]) { if (i == par) { continue; } dfs2(i, node, depth + wt); } }; dfs2(deepest, 0, 0); } std::cout << ans.size() << '\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...