Submission #1072167

#TimeUsernameProblemLanguageResultExecution timeMemory
1072167avighnaFirefighting (NOI20_firefighting)C++17
0 / 100
3064 ms48704 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::vector<ll> ans;
  while (!active.empty()) {
    ll root = *active.begin();
    ll deepest = root, deepest_depth = 0;
    std::function<void(ll, ll, ll)> dfs;
    std::vector<std::pair<ll, ll>> up(n + 1);
    dfs = [&](ll node, ll par, ll depth) {
      if (depth > deepest_depth) {
        deepest = node;
        deepest_depth = 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(root, 0, 0);
    ll dist = 0;
    while (deepest != root 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 or !active.count(i)) {
          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...