Submission #1084603

# Submission time Handle Problem Language Result Execution time Memory
1084603 2024-09-06T13:18:14 Z avighna Firefighting (NOI20_firefighting) C++17
62 / 100
3000 ms 64704 KB
#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);
  ll min_w = 1e15;
  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});
    min_w = std::min(min_w, w);
  }

  if (k < min_w) {
    std::cout << n << '\n';
    for (ll i = 1; i <= n; ++i) {
      std::cout << i << ' ';
    }
    std::cout << '\n';
    return 0;
  }

  if (n <= 17) {
    std::pair<ll, ll> ans = {1e15, -1};
    for (ll mask = 1; mask < (1 << n); ++mask) {
      std::priority_queue<std::pair<ll, ll>> pq;
      std::vector<bool> vis(n + 1);
      std::vector<ll> dist(n + 1, 1e15);
      dist[0] = 0;
      for (ll i = 0; i < n; ++i) {
        if (mask & (1 << i)) {
          dist[i + 1] = 0;
          pq.push({0, i + 1});
        }
      }
      while (!pq.empty()) {
        auto [d, node] = pq.top();
        pq.pop();
        if (vis[node]) {
          continue;
        }
        vis[node] = true;
        d *= -1;
        for (auto &[ch, w] : adj[node]) {
          if (d + w < dist[ch]) {
            dist[ch] = d + w;
            pq.push({-dist[ch], ch});
          }
        }
      }
      if (*std::max_element(dist.begin(), dist.end()) <= k) {
        ans = std::min(ans, {__builtin_popcount(mask), mask});
      }
    }
    std::cout << ans.first << '\n';
    for (ll i = 0; i < n; ++i) {
      if (ans.second & (1 << i)) {
        std::cout << i + 1 << ' ';
      }
    }
    std::cout << '\n';
    return 0;
  }

  if (2 * min_w > k) {
    // 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';
    return 0;
  }

  std::set<ll> active;
  for (ll i = 1; i <= n; ++i) {
    active.insert(i);
  }
  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 time Memory Grader output
1 Correct 121 ms 25168 KB Output is correct
2 Correct 116 ms 25168 KB Output is correct
3 Correct 38 ms 9532 KB Output is correct
4 Correct 126 ms 24460 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 436 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 32 ms 452 KB Output is correct
6 Correct 35 ms 348 KB Output is correct
7 Correct 77 ms 432 KB Output is correct
8 Correct 74 ms 344 KB Output is correct
9 Correct 74 ms 348 KB Output is correct
10 Correct 76 ms 348 KB Output is correct
11 Correct 75 ms 432 KB Output is correct
12 Correct 76 ms 348 KB Output is correct
13 Correct 74 ms 344 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 77 ms 348 KB Output is correct
4 Correct 41 ms 428 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 75 ms 432 KB Output is correct
10 Correct 81 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 86 ms 348 KB Output is correct
13 Correct 38 ms 348 KB Output is correct
14 Correct 42 ms 348 KB Output is correct
15 Correct 94 ms 348 KB Output is correct
16 Correct 45 ms 344 KB Output is correct
17 Correct 92 ms 348 KB Output is correct
18 Correct 43 ms 348 KB Output is correct
19 Correct 85 ms 436 KB Output is correct
20 Correct 92 ms 348 KB Output is correct
21 Correct 89 ms 348 KB Output is correct
22 Correct 84 ms 348 KB Output is correct
23 Correct 83 ms 436 KB Output is correct
24 Correct 87 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 85 ms 432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 25168 KB Output is correct
2 Correct 101 ms 24012 KB Output is correct
3 Correct 136 ms 24520 KB Output is correct
4 Correct 81 ms 21448 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 198 ms 42072 KB Output is correct
8 Correct 202 ms 46780 KB Output is correct
9 Correct 191 ms 47036 KB Output is correct
10 Correct 199 ms 47040 KB Output is correct
11 Correct 125 ms 32164 KB Output is correct
12 Correct 141 ms 32552 KB Output is correct
13 Correct 71 ms 20436 KB Output is correct
14 Correct 132 ms 30660 KB Output is correct
15 Correct 159 ms 37068 KB Output is correct
16 Correct 187 ms 39728 KB Output is correct
17 Correct 125 ms 33740 KB Output is correct
18 Correct 145 ms 32964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 2 ms 860 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 91 ms 1184 KB Output is correct
6 Correct 74 ms 1112 KB Output is correct
7 Correct 47 ms 1360 KB Output is correct
8 Correct 51 ms 1112 KB Output is correct
9 Correct 45 ms 1128 KB Output is correct
10 Correct 43 ms 1116 KB Output is correct
11 Correct 83 ms 1116 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 51 ms 1116 KB Output is correct
14 Correct 50 ms 1112 KB Output is correct
15 Correct 3 ms 860 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 3 ms 856 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 704 ms 51308 KB Output is correct
2 Correct 468 ms 49344 KB Output is correct
3 Correct 545 ms 52672 KB Output is correct
4 Correct 193 ms 25020 KB Output is correct
5 Execution timed out 3064 ms 64704 KB Time limit exceeded
6 Halted 0 ms 0 KB -