Submission #978163

# Submission time Handle Problem Language Result Execution time Memory
978163 2024-05-09T01:19:37 Z kilkuwu Race (IOI11_race) C++17
100 / 100
304 ms 92240 KB
#include "race.h"

#include <bits/stdc++.h>

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

int best_path(int N, int K, int H[][2], int L[]) {

  std::vector<std::vector<std::pair<int, int>>> adj(N);

  for (int i = 0; i + 1 < N; i++) {
    int u = H[i][0], v = H[i][1], w = L[i];
    adj[u].emplace_back(v, w);
    adj[v].emplace_back(u, w);
  }

  int ans = 1e9;

  struct Answer {
    int64_t dw = 0;
    int dc = 0;
    std::map<int64_t, int> mp;

    int size() const {
      return (int) mp.size();
    }
  };

  auto merge = [&](Answer& l, Answer& r) {
    if (l.size() < r.size()) std::swap(l, r);

    for (auto [k, v] : r.mp) {

      auto required = K - k - r.dw - l.dw; // all the added weight is loss
      auto it = l.mp.find(required);
      
      if (it != l.mp.end()) {
        ans = std::min(ans, it->second + v + l.dc + r.dc);
      }
      // after that's done we merge
    }

    for (auto [k, v] : r.mp) {
      auto nk = k + r.dw - l.dw;
      auto nv = v + r.dc - l.dc;
      auto it = l.mp.find(nk);

      if (it == l.mp.end()) {
        l.mp[nk] = nv;
      } else {
        it->second = std::min(it->second, nv);
      }
    }
  };

  auto dfs = [&](auto self, int u, int p) -> Answer {
    Answer re;
    re.mp[0] = 0;

    for (auto [v, w] : adj[u]) {
      if (v == p) continue;
      auto res = self(self, v, u);
      
      res.dw += w;
      res.dc++;
      merge(re, res);
    }

    dbg(u, re.dc, re.dw, re.mp);
    return re;
  };

  dfs(dfs, 0, 0);

  return ans < 1e9 ? ans : -1;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2400 KB Output is correct
4 Correct 1 ms 2420 KB Output is correct
5 Correct 1 ms 2400 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2492 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 1 ms 2400 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2400 KB Output is correct
4 Correct 1 ms 2420 KB Output is correct
5 Correct 1 ms 2400 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2492 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 1 ms 2400 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2408 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2404 KB Output is correct
22 Correct 1 ms 2400 KB Output is correct
23 Correct 2 ms 2400 KB Output is correct
24 Correct 1 ms 2500 KB Output is correct
25 Correct 1 ms 2404 KB Output is correct
26 Correct 1 ms 2400 KB Output is correct
27 Correct 1 ms 2408 KB Output is correct
28 Correct 1 ms 2392 KB Output is correct
29 Correct 1 ms 2408 KB Output is correct
30 Correct 1 ms 2404 KB Output is correct
31 Correct 1 ms 2408 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2664 KB Output is correct
34 Correct 1 ms 2664 KB Output is correct
35 Correct 1 ms 2664 KB Output is correct
36 Correct 1 ms 2664 KB Output is correct
37 Correct 1 ms 2916 KB Output is correct
38 Correct 1 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2400 KB Output is correct
4 Correct 1 ms 2420 KB Output is correct
5 Correct 1 ms 2400 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2492 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 1 ms 2400 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 72 ms 10736 KB Output is correct
20 Correct 58 ms 11868 KB Output is correct
21 Correct 54 ms 11344 KB Output is correct
22 Correct 56 ms 12192 KB Output is correct
23 Correct 78 ms 12628 KB Output is correct
24 Correct 60 ms 12116 KB Output is correct
25 Correct 62 ms 31456 KB Output is correct
26 Correct 62 ms 48468 KB Output is correct
27 Correct 103 ms 18772 KB Output is correct
28 Correct 210 ms 92240 KB Output is correct
29 Correct 210 ms 88912 KB Output is correct
30 Correct 105 ms 19256 KB Output is correct
31 Correct 97 ms 19288 KB Output is correct
32 Correct 106 ms 19532 KB Output is correct
33 Correct 121 ms 19280 KB Output is correct
34 Correct 223 ms 32076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2400 KB Output is correct
4 Correct 1 ms 2420 KB Output is correct
5 Correct 1 ms 2400 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2492 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2400 KB Output is correct
17 Correct 1 ms 2400 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2408 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2404 KB Output is correct
22 Correct 1 ms 2400 KB Output is correct
23 Correct 2 ms 2400 KB Output is correct
24 Correct 1 ms 2500 KB Output is correct
25 Correct 1 ms 2404 KB Output is correct
26 Correct 1 ms 2400 KB Output is correct
27 Correct 1 ms 2408 KB Output is correct
28 Correct 1 ms 2392 KB Output is correct
29 Correct 1 ms 2408 KB Output is correct
30 Correct 1 ms 2404 KB Output is correct
31 Correct 1 ms 2408 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2664 KB Output is correct
34 Correct 1 ms 2664 KB Output is correct
35 Correct 1 ms 2664 KB Output is correct
36 Correct 1 ms 2664 KB Output is correct
37 Correct 1 ms 2916 KB Output is correct
38 Correct 1 ms 2660 KB Output is correct
39 Correct 72 ms 10736 KB Output is correct
40 Correct 58 ms 11868 KB Output is correct
41 Correct 54 ms 11344 KB Output is correct
42 Correct 56 ms 12192 KB Output is correct
43 Correct 78 ms 12628 KB Output is correct
44 Correct 60 ms 12116 KB Output is correct
45 Correct 62 ms 31456 KB Output is correct
46 Correct 62 ms 48468 KB Output is correct
47 Correct 103 ms 18772 KB Output is correct
48 Correct 210 ms 92240 KB Output is correct
49 Correct 210 ms 88912 KB Output is correct
50 Correct 105 ms 19256 KB Output is correct
51 Correct 97 ms 19288 KB Output is correct
52 Correct 106 ms 19532 KB Output is correct
53 Correct 121 ms 19280 KB Output is correct
54 Correct 223 ms 32076 KB Output is correct
55 Correct 9 ms 3672 KB Output is correct
56 Correct 5 ms 3164 KB Output is correct
57 Correct 38 ms 11020 KB Output is correct
58 Correct 38 ms 11804 KB Output is correct
59 Correct 81 ms 48560 KB Output is correct
60 Correct 207 ms 89940 KB Output is correct
61 Correct 120 ms 20260 KB Output is correct
62 Correct 93 ms 19280 KB Output is correct
63 Correct 144 ms 19508 KB Output is correct
64 Correct 304 ms 26348 KB Output is correct
65 Correct 290 ms 32720 KB Output is correct
66 Correct 217 ms 78420 KB Output is correct
67 Correct 89 ms 20664 KB Output is correct
68 Correct 201 ms 30144 KB Output is correct
69 Correct 216 ms 32596 KB Output is correct
70 Correct 181 ms 29632 KB Output is correct