답안 #1108190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108190 2024-11-03T08:55:51 Z keunbum 경주 (Race) (IOI11_race) C++17
100 / 100
238 ms 32584 KB
#include <bits/stdc++.h>

using namespace std;

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

int best_path(int N, int K, int H[][2], int L[]) {
  int n = N;
  int k = K;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; ++i) {
    int x = H[i][0];
    int y = H[i][1];
    int z = L[i];
    g[x].emplace_back(y, z);
    g[y].emplace_back(x, z);
  }
  vector<bool> removed(n, false);
  vector<int> sz(n);
  vector<int> pv(n);
  auto DFS = [&](auto&& self, int v) -> void {
    sz[v] = 1;
    for (auto [u, _] : g[v]) {
      if (u != pv[v] && !removed[u]) {
        pv[u] = v;
        self(self, u);
        sz[v] += sz[u];
      }
    }
  };
  auto GetCenter = [&](int r) {
    pv[r] = -1;
    DFS(DFS, r);
    int tot = sz[r];
    while (true) {
      int nr = -1;
      for (auto [u, _] : g[r]) {
        if (u != pv[r] && !removed[u] && sz[u] * 2 > tot) {
          nr = u;
          break;
        }
      }
      if (nr == -1) {
        return r;
      }
      r = nr;
    }
  };
  int ans = n;
  int iter = 0;
  vector<int> aux(k + 1, 0);
  vector<int> dp(k + 1, 0);
  auto Find = [&](auto&& self, int v, int pv, int dist, int depth) -> void {
    if (dist > k) {
      return;
    }
    if (aux[k - dist] == iter) {
      ans = min(ans, dp[k - dist] + depth);
    }
    if (dist == k) {
      ans = min(ans, depth);
    }
    for (auto [u, w] : g[v]) {
      if (!removed[u] && u != pv) {
        self(self, u, v, dist + w, depth + 1);
      }
    }
  };
  auto Calc = [&](auto&& self, int v, int pv, int dist, int depth) -> void {
    if (dist > k) {
      return;
    }
    if (aux[dist] < iter) {
      aux[dist] = iter;
      dp[dist] = depth;
    } else
    if (dp[dist] > depth) {
      aux[dist] = iter;
      dp[dist] = depth;
    }
    for (auto [u, w] : g[v]) {
      if (!removed[u] && u != pv) {
        self(self, u, v, dist + w, depth + 1);
      }
    }
  };  
  auto Solve = [&](auto&& self, int v) -> void {
    v = GetCenter(v);
    iter += 1;
    for (auto [u, w] : g[v]) {
      if (!removed[u]) {
        Find(Find, u, v, w, 1);
        Calc(Calc, u, v, w, 1);
      }
    }
    removed[v] = true;
    for (auto [u, _] : g[v]) {
      if (!removed[u]) {
        self(self, u);
      }
    }
  };
  Solve(Solve, 0);
  return (ans == n ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 3 ms 9552 KB Output is correct
23 Correct 3 ms 8272 KB Output is correct
24 Correct 3 ms 9296 KB Output is correct
25 Correct 3 ms 9268 KB Output is correct
26 Correct 3 ms 5052 KB Output is correct
27 Correct 3 ms 8784 KB Output is correct
28 Correct 2 ms 3920 KB Output is correct
29 Correct 3 ms 4944 KB Output is correct
30 Correct 3 ms 5200 KB Output is correct
31 Correct 2 ms 7504 KB Output is correct
32 Correct 2 ms 8016 KB Output is correct
33 Correct 3 ms 8528 KB Output is correct
34 Correct 2 ms 6992 KB Output is correct
35 Correct 4 ms 8784 KB Output is correct
36 Correct 4 ms 9832 KB Output is correct
37 Correct 3 ms 8784 KB Output is correct
38 Correct 4 ms 6480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
19 Correct 78 ms 11344 KB Output is correct
20 Correct 76 ms 11344 KB Output is correct
21 Correct 82 ms 11500 KB Output is correct
22 Correct 78 ms 11600 KB Output is correct
23 Correct 47 ms 11612 KB Output is correct
24 Correct 39 ms 11600 KB Output is correct
25 Correct 78 ms 13080 KB Output is correct
26 Correct 64 ms 14960 KB Output is correct
27 Correct 93 ms 18472 KB Output is correct
28 Correct 142 ms 25192 KB Output is correct
29 Correct 142 ms 24392 KB Output is correct
30 Correct 98 ms 18504 KB Output is correct
31 Correct 111 ms 18504 KB Output is correct
32 Correct 102 ms 18248 KB Output is correct
33 Correct 121 ms 17244 KB Output is correct
34 Correct 113 ms 17224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2384 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 3 ms 9552 KB Output is correct
23 Correct 3 ms 8272 KB Output is correct
24 Correct 3 ms 9296 KB Output is correct
25 Correct 3 ms 9268 KB Output is correct
26 Correct 3 ms 5052 KB Output is correct
27 Correct 3 ms 8784 KB Output is correct
28 Correct 2 ms 3920 KB Output is correct
29 Correct 3 ms 4944 KB Output is correct
30 Correct 3 ms 5200 KB Output is correct
31 Correct 2 ms 7504 KB Output is correct
32 Correct 2 ms 8016 KB Output is correct
33 Correct 3 ms 8528 KB Output is correct
34 Correct 2 ms 6992 KB Output is correct
35 Correct 4 ms 8784 KB Output is correct
36 Correct 4 ms 9832 KB Output is correct
37 Correct 3 ms 8784 KB Output is correct
38 Correct 4 ms 6480 KB Output is correct
39 Correct 78 ms 11344 KB Output is correct
40 Correct 76 ms 11344 KB Output is correct
41 Correct 82 ms 11500 KB Output is correct
42 Correct 78 ms 11600 KB Output is correct
43 Correct 47 ms 11612 KB Output is correct
44 Correct 39 ms 11600 KB Output is correct
45 Correct 78 ms 13080 KB Output is correct
46 Correct 64 ms 14960 KB Output is correct
47 Correct 93 ms 18472 KB Output is correct
48 Correct 142 ms 25192 KB Output is correct
49 Correct 142 ms 24392 KB Output is correct
50 Correct 98 ms 18504 KB Output is correct
51 Correct 111 ms 18504 KB Output is correct
52 Correct 102 ms 18248 KB Output is correct
53 Correct 121 ms 17244 KB Output is correct
54 Correct 113 ms 17224 KB Output is correct
55 Correct 7 ms 3152 KB Output is correct
56 Correct 8 ms 3152 KB Output is correct
57 Correct 67 ms 11592 KB Output is correct
58 Correct 26 ms 11716 KB Output is correct
59 Correct 67 ms 16208 KB Output is correct
60 Correct 237 ms 32584 KB Output is correct
61 Correct 102 ms 18532 KB Output is correct
62 Correct 122 ms 26184 KB Output is correct
63 Correct 140 ms 26192 KB Output is correct
64 Correct 233 ms 22116 KB Output is correct
65 Correct 116 ms 18248 KB Output is correct
66 Correct 238 ms 31048 KB Output is correct
67 Correct 71 ms 27064 KB Output is correct
68 Correct 150 ms 26184 KB Output is correct
69 Correct 124 ms 26464 KB Output is correct
70 Correct 128 ms 25672 KB Output is correct