Submission #871407

# Submission time Handle Problem Language Result Execution time Memory
871407 2023-11-10T18:39:15 Z evenvalue Race (IOI11_race) C++17
100 / 100
900 ms 50940 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;
  map<int64, int> path;

  function<void(int, int, int64, int)> upd_ans = [&](const int x, const int p, const int64 cost, const int d) {
    if (path.count(k - cost)) {
      ans = min(ans, path.at(k - cost) + d);
    }
    for (const auto [y, w] : g[x]) {
      if (decomposed[y] or y == p) continue;
      upd_ans(y, x, cost + w, d + 1);
    }
  };

  function<void(int, int, int64, int)> upd_path = [&](const int x, const int p, const int64 cost, const int d) {
    path.try_emplace(cost, d);
    path[cost] = min(path[cost], d);
    for (const auto [y, w] : g[x]) {
      if (decomposed[y] or y == p) continue;
      upd_path(y, x, cost + w, d + 1);
    }
  };

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

    path.clear();
    path[0] = 0;

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

    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 2392 KB Output is correct
2 Correct 1 ms 2488 KB Output is correct
3 Correct 1 ms 2396 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 2396 KB Output is correct
8 Correct 1 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 1 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 2396 KB Output is correct
17 Correct 1 ms 2492 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 2488 KB Output is correct
3 Correct 1 ms 2396 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 2396 KB Output is correct
8 Correct 1 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 1 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 2396 KB Output is correct
17 Correct 1 ms 2492 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2504 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 2 ms 2396 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 2 ms 2468 KB Output is correct
33 Correct 2 ms 2396 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 2 ms 2396 KB Output is correct
36 Correct 2 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 2 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 2488 KB Output is correct
3 Correct 1 ms 2396 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 2396 KB Output is correct
8 Correct 1 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 1 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 2396 KB Output is correct
17 Correct 1 ms 2492 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 213 ms 12488 KB Output is correct
20 Correct 201 ms 12436 KB Output is correct
21 Correct 183 ms 12500 KB Output is correct
22 Correct 173 ms 12756 KB Output is correct
23 Correct 263 ms 12864 KB Output is correct
24 Correct 123 ms 12720 KB Output is correct
25 Correct 187 ms 19516 KB Output is correct
26 Correct 104 ms 21316 KB Output is correct
27 Correct 242 ms 20820 KB Output is correct
28 Correct 869 ms 50940 KB Output is correct
29 Correct 782 ms 49300 KB Output is correct
30 Correct 265 ms 20912 KB Output is correct
31 Correct 244 ms 20820 KB Output is correct
32 Correct 351 ms 21000 KB Output is correct
33 Correct 419 ms 19756 KB Output is correct
34 Correct 778 ms 32896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2488 KB Output is correct
3 Correct 1 ms 2396 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 2396 KB Output is correct
8 Correct 1 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 1 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 2396 KB Output is correct
17 Correct 1 ms 2492 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2504 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 2 ms 2396 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 2 ms 2468 KB Output is correct
33 Correct 2 ms 2396 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 2 ms 2396 KB Output is correct
36 Correct 2 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 2 ms 2396 KB Output is correct
39 Correct 213 ms 12488 KB Output is correct
40 Correct 201 ms 12436 KB Output is correct
41 Correct 183 ms 12500 KB Output is correct
42 Correct 173 ms 12756 KB Output is correct
43 Correct 263 ms 12864 KB Output is correct
44 Correct 123 ms 12720 KB Output is correct
45 Correct 187 ms 19516 KB Output is correct
46 Correct 104 ms 21316 KB Output is correct
47 Correct 242 ms 20820 KB Output is correct
48 Correct 869 ms 50940 KB Output is correct
49 Correct 782 ms 49300 KB Output is correct
50 Correct 265 ms 20912 KB Output is correct
51 Correct 244 ms 20820 KB Output is correct
52 Correct 351 ms 21000 KB Output is correct
53 Correct 419 ms 19756 KB Output is correct
54 Correct 778 ms 32896 KB Output is correct
55 Correct 20 ms 3524 KB Output is correct
56 Correct 12 ms 3164 KB Output is correct
57 Correct 128 ms 12704 KB Output is correct
58 Correct 42 ms 12240 KB Output is correct
59 Correct 294 ms 26016 KB Output is correct
60 Correct 840 ms 48724 KB Output is correct
61 Correct 291 ms 22612 KB Output is correct
62 Correct 256 ms 20820 KB Output is correct
63 Correct 270 ms 20824 KB Output is correct
64 Correct 900 ms 27080 KB Output is correct
65 Correct 882 ms 33876 KB Output is correct
66 Correct 867 ms 46224 KB Output is correct
67 Correct 113 ms 21716 KB Output is correct
68 Correct 423 ms 32084 KB Output is correct
69 Correct 435 ms 32452 KB Output is correct
70 Correct 441 ms 30804 KB Output is correct