Submission #871452

# Submission time Handle Problem Language Result Execution time Memory
871452 2023-11-10T20:40:20 Z evenvalue Race (IOI11_race) C++17
100 / 100
301 ms 43976 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

#ifdef evenvalue
  #include "debug.h"
#else
  #define debug(...)
#endif

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

using int64 = long long;
using ld = long double;

constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;

int best_path(const int n, const int k, int H[][2], int L[]) {
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    const int x = H[i][0];
    const int y = H[i][1];
    const int w = L[i];
    g[x].emplace_back(y, w);
    g[y].emplace_back(x, w);
  }

  vector<int> sz(n);
  vector<bool> decomposed(n);

  function<int(int, int)> subtree_size = [&](const int x, const int p) {
    sz[x] = 1;
    for (const auto [y, w] : g[x]) {
      if (decomposed[y] or y == p) continue;
      sz[x] += subtree_size(y, x);
    }
    return sz[x];
  };

  function<int(int, int, int)> centroid = [&](const int x, const int p, const int size) {
    int c = x;
    for (const auto [y, w] : g[x]) {
      if (decomposed[y] or y == p) continue;
      if (2 * sz[y] < size) continue;
      c = centroid(y, x, size);
      break;
    }
    return c;
  };

  int ans = kInf;

  struct Dist {
    int weight_dist;
    int edge_dist;
  };

  auto calc_dist = [&](const int root, const int parent, Dist d) -> vector<Dist> {
    vector<Dist> distances;
    function<void(int, int, Dist)> rec = [&](int x, int p, Dist d) {
      if (d.weight_dist > k) return;
      distances.push_back(d);
      for (const auto [y, w] : g[x]) {
        if (decomposed[y] or y == p) continue;
        rec(y, x, Dist{ d.weight_dist + w, d.edge_dist + 1});
      }
    };
    rec(root, parent, d);
    return distances;
  };

  vector<pair<int, int>> path(k + 1, {kInf, -1});

  function<void(int)> decompose = [&](int x) {
    x = centroid(x, -1, subtree_size(x, -1));
    decomposed[x] = true;
    path[0] = {0, x};

    for (const auto [y, w] : g[x]) {
      if (decomposed[y]) continue;
      vector<Dist> distances = calc_dist(y, x, Dist{w, 1});
      for (const auto [weight_dist, edge_dist] : distances) {
        if (0 <= weight_dist and weight_dist <= k and path[k - weight_dist].second == x) {
          ans = min(ans, path[k - weight_dist].first + edge_dist);
        }
      }
      for (const auto [weight_dist, edge_dist] : distances) {
        if (0 <= weight_dist and weight_dist <= k) {
          if (path[weight_dist].second != x or path[weight_dist].first >= edge_dist) {
            path[weight_dist].first = edge_dist;
            path[weight_dist].second = x;
          }
        }
      }
    }

    for (const auto [y, w] : g[x]) {
      if (decomposed[y]) continue;
      decompose(y);
    }
  };

  decompose(0);
  return (ans == kInf ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2488 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2648 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2488 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2648 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2648 KB Output is correct
22 Correct 3 ms 9564 KB Output is correct
23 Correct 3 ms 8284 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
25 Correct 3 ms 9052 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 3932 KB Output is correct
29 Correct 2 ms 4956 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
31 Correct 2 ms 7516 KB Output is correct
32 Correct 2 ms 8028 KB Output is correct
33 Correct 3 ms 8540 KB Output is correct
34 Correct 2 ms 7004 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 9820 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2488 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2648 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 88 ms 11236 KB Output is correct
20 Correct 93 ms 11436 KB Output is correct
21 Correct 91 ms 11348 KB Output is correct
22 Correct 92 ms 11580 KB Output is correct
23 Correct 59 ms 11356 KB Output is correct
24 Correct 44 ms 11100 KB Output is correct
25 Correct 102 ms 15808 KB Output is correct
26 Correct 86 ms 20800 KB Output is correct
27 Correct 120 ms 17748 KB Output is correct
28 Correct 205 ms 35152 KB Output is correct
29 Correct 190 ms 33620 KB Output is correct
30 Correct 116 ms 17760 KB Output is correct
31 Correct 115 ms 17760 KB Output is correct
32 Correct 129 ms 17784 KB Output is correct
33 Correct 152 ms 16468 KB Output is correct
34 Correct 131 ms 16656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2488 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2648 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2648 KB Output is correct
22 Correct 3 ms 9564 KB Output is correct
23 Correct 3 ms 8284 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
25 Correct 3 ms 9052 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 3932 KB Output is correct
29 Correct 2 ms 4956 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
31 Correct 2 ms 7516 KB Output is correct
32 Correct 2 ms 8028 KB Output is correct
33 Correct 3 ms 8540 KB Output is correct
34 Correct 2 ms 7004 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 9820 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 2 ms 6492 KB Output is correct
39 Correct 88 ms 11236 KB Output is correct
40 Correct 93 ms 11436 KB Output is correct
41 Correct 91 ms 11348 KB Output is correct
42 Correct 92 ms 11580 KB Output is correct
43 Correct 59 ms 11356 KB Output is correct
44 Correct 44 ms 11100 KB Output is correct
45 Correct 102 ms 15808 KB Output is correct
46 Correct 86 ms 20800 KB Output is correct
47 Correct 120 ms 17748 KB Output is correct
48 Correct 205 ms 35152 KB Output is correct
49 Correct 190 ms 33620 KB Output is correct
50 Correct 116 ms 17760 KB Output is correct
51 Correct 115 ms 17760 KB Output is correct
52 Correct 129 ms 17784 KB Output is correct
53 Correct 152 ms 16468 KB Output is correct
54 Correct 131 ms 16656 KB Output is correct
55 Correct 7 ms 3164 KB Output is correct
56 Correct 8 ms 3164 KB Output is correct
57 Correct 68 ms 11996 KB Output is correct
58 Correct 32 ms 11204 KB Output is correct
59 Correct 91 ms 22356 KB Output is correct
60 Correct 301 ms 43976 KB Output is correct
61 Correct 123 ms 17744 KB Output is correct
62 Correct 161 ms 25684 KB Output is correct
63 Correct 163 ms 25436 KB Output is correct
64 Correct 241 ms 22684 KB Output is correct
65 Correct 141 ms 17784 KB Output is correct
66 Correct 281 ms 38544 KB Output is correct
67 Correct 81 ms 25796 KB Output is correct
68 Correct 164 ms 25684 KB Output is correct
69 Correct 172 ms 25700 KB Output is correct
70 Correct 162 ms 25168 KB Output is correct