답안 #483979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483979 2021-11-01T17:29:55 Z arujbansal 경주 (Race) (IOI11_race) C++17
100 / 100
541 ms 46252 KB
#include "race.h"

#include <iostream>
#include <vector>

using namespace std;

const int MXN = 2e5 + 5, INF = 1e9 + 5;
vector<pair<int, int>> g[MXN];
int subtree_sz[MXN];
bool vis[MXN];
int N, K;
int ans = INF;

struct two_set {
  pair<int, int> x, y;

  two_set() { x = y = make_pair(INF, INF); }

  void insert(int val, int node) {
    if (val < y.first) y = make_pair(val, node);
    if (x.first > y.first) swap(x, y);
  }

  int query(int exclude_node) {
    if (x.second == exclude_node) return y.first;
    return x.first;
  }

  void work(int u) {
    if (x.second == u) x = make_pair(INF, INF);
    else if (y.second == u) y = make_pair(INF, INF);
    if (x.first > y.first) swap(x, y);
  }

} mn_val[int(1e6) + 5];

int find_centroid(int u, int p, int nodes) {
  if (p != -1) {
    subtree_sz[p] -= subtree_sz[u];
    subtree_sz[u] += subtree_sz[p];
  }

  for (const auto &[v, wt] : g[u])
    if (v != p && !vis[v] && subtree_sz[v] * 2 > nodes)
      return find_centroid(v, u, nodes);

  return u;
}

vector<int> to_reset;

void calc_dist(int u, int p, int len, int depth, bool remove) {
  if (len > K) return;
  
  if (!remove) {
    if (len <= K) mn_val[len].insert(depth, u);
    to_reset.push_back(len);
  } else {
    if (len <= K) {
      mn_val[len].work(u);
    }
  }

  for (const auto &[v, wt] : g[u])
    if (v != p && !vis[v]) calc_dist(v, u, len + wt, depth + 1, remove);
}

void calc_ans(int u, int p, int len, int depth) {
  if (len > K) return;

  if (len < K) ans = min(ans, depth + mn_val[K - len].query(u));
  if (len == K) ans = min(ans, depth);

  for (const auto &[v, wt] : g[u])
    if (v != p && !vis[v]) calc_ans(v, u, len + wt, depth + 1);
}

void calc_sz(int u, int p) {
  subtree_sz[u] = 1;

  for (const auto &[v, wt] : g[u]) {
    if (v == p) continue;
    calc_sz(v, u);
    subtree_sz[u] += subtree_sz[v];
  }
}

void cd(int u) {
  int centroid = find_centroid(u, -1, subtree_sz[u]);
  vis[centroid] = true;

  for (const auto &[v, wt] : g[centroid])
    if (!vis[v]) calc_dist(v, centroid, wt, 1, false); 

  for (const auto &[v, wt] : g[centroid]) {
    if (!vis[v]) {
      calc_dist(v, centroid, wt, 1, true); 
      calc_ans(v, centroid, wt, 1);
      calc_dist(v, centroid, wt, 1, false); 
    }
  }

  for (const auto &x : to_reset)
    mn_val[x] = two_set();

  to_reset.clear();

  for (const auto &[v, wt] : g[centroid])
    if (!vis[v]) cd(v); 
}

int best_path(int _N, int _K, int _H[][2], int _L[]) {
  N = _N, K = _K;

  for (int i = 0; i < N - 1; i++) {
    _H[i][0]++;
    _H[i][1]++;
    g[_H[i][0]].emplace_back(_H[i][1], _L[i]);
    g[_H[i][1]].emplace_back(_H[i][0], _L[i]);
  }

  calc_sz(1, -1);
  cd(1);

  return (ans < INF ? ans : -1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 20744 KB Output is correct
2 Correct 11 ms 20556 KB Output is correct
3 Correct 9 ms 20644 KB Output is correct
4 Correct 9 ms 20556 KB Output is correct
5 Correct 9 ms 20556 KB Output is correct
6 Correct 9 ms 20560 KB Output is correct
7 Correct 9 ms 20556 KB Output is correct
8 Correct 9 ms 20556 KB Output is correct
9 Correct 9 ms 20556 KB Output is correct
10 Correct 10 ms 20580 KB Output is correct
11 Correct 9 ms 20680 KB Output is correct
12 Correct 9 ms 20556 KB Output is correct
13 Correct 9 ms 20556 KB Output is correct
14 Correct 9 ms 20556 KB Output is correct
15 Correct 9 ms 20556 KB Output is correct
16 Correct 9 ms 20556 KB Output is correct
17 Correct 9 ms 20576 KB Output is correct
18 Correct 10 ms 20652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 20744 KB Output is correct
2 Correct 11 ms 20556 KB Output is correct
3 Correct 9 ms 20644 KB Output is correct
4 Correct 9 ms 20556 KB Output is correct
5 Correct 9 ms 20556 KB Output is correct
6 Correct 9 ms 20560 KB Output is correct
7 Correct 9 ms 20556 KB Output is correct
8 Correct 9 ms 20556 KB Output is correct
9 Correct 9 ms 20556 KB Output is correct
10 Correct 10 ms 20580 KB Output is correct
11 Correct 9 ms 20680 KB Output is correct
12 Correct 9 ms 20556 KB Output is correct
13 Correct 9 ms 20556 KB Output is correct
14 Correct 9 ms 20556 KB Output is correct
15 Correct 9 ms 20556 KB Output is correct
16 Correct 9 ms 20556 KB Output is correct
17 Correct 9 ms 20576 KB Output is correct
18 Correct 10 ms 20652 KB Output is correct
19 Correct 9 ms 20556 KB Output is correct
20 Correct 9 ms 20564 KB Output is correct
21 Correct 10 ms 20684 KB Output is correct
22 Correct 10 ms 20684 KB Output is correct
23 Correct 10 ms 20724 KB Output is correct
24 Correct 10 ms 20672 KB Output is correct
25 Correct 10 ms 20672 KB Output is correct
26 Correct 10 ms 20684 KB Output is correct
27 Correct 10 ms 20620 KB Output is correct
28 Correct 10 ms 20684 KB Output is correct
29 Correct 10 ms 20704 KB Output is correct
30 Correct 10 ms 20684 KB Output is correct
31 Correct 10 ms 20684 KB Output is correct
32 Correct 10 ms 20724 KB Output is correct
33 Correct 11 ms 20684 KB Output is correct
34 Correct 10 ms 20684 KB Output is correct
35 Correct 10 ms 20684 KB Output is correct
36 Correct 9 ms 20684 KB Output is correct
37 Correct 10 ms 20684 KB Output is correct
38 Correct 10 ms 20684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 20744 KB Output is correct
2 Correct 11 ms 20556 KB Output is correct
3 Correct 9 ms 20644 KB Output is correct
4 Correct 9 ms 20556 KB Output is correct
5 Correct 9 ms 20556 KB Output is correct
6 Correct 9 ms 20560 KB Output is correct
7 Correct 9 ms 20556 KB Output is correct
8 Correct 9 ms 20556 KB Output is correct
9 Correct 9 ms 20556 KB Output is correct
10 Correct 10 ms 20580 KB Output is correct
11 Correct 9 ms 20680 KB Output is correct
12 Correct 9 ms 20556 KB Output is correct
13 Correct 9 ms 20556 KB Output is correct
14 Correct 9 ms 20556 KB Output is correct
15 Correct 9 ms 20556 KB Output is correct
16 Correct 9 ms 20556 KB Output is correct
17 Correct 9 ms 20576 KB Output is correct
18 Correct 10 ms 20652 KB Output is correct
19 Correct 119 ms 26272 KB Output is correct
20 Correct 117 ms 25968 KB Output is correct
21 Correct 134 ms 26524 KB Output is correct
22 Correct 130 ms 27104 KB Output is correct
23 Correct 55 ms 26120 KB Output is correct
24 Correct 56 ms 26052 KB Output is correct
25 Correct 107 ms 28744 KB Output is correct
26 Correct 101 ms 32684 KB Output is correct
27 Correct 150 ms 31616 KB Output is correct
28 Correct 147 ms 42692 KB Output is correct
29 Correct 156 ms 41684 KB Output is correct
30 Correct 147 ms 31604 KB Output is correct
31 Correct 146 ms 31628 KB Output is correct
32 Correct 182 ms 31688 KB Output is correct
33 Correct 170 ms 30400 KB Output is correct
34 Correct 122 ms 30224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 20744 KB Output is correct
2 Correct 11 ms 20556 KB Output is correct
3 Correct 9 ms 20644 KB Output is correct
4 Correct 9 ms 20556 KB Output is correct
5 Correct 9 ms 20556 KB Output is correct
6 Correct 9 ms 20560 KB Output is correct
7 Correct 9 ms 20556 KB Output is correct
8 Correct 9 ms 20556 KB Output is correct
9 Correct 9 ms 20556 KB Output is correct
10 Correct 10 ms 20580 KB Output is correct
11 Correct 9 ms 20680 KB Output is correct
12 Correct 9 ms 20556 KB Output is correct
13 Correct 9 ms 20556 KB Output is correct
14 Correct 9 ms 20556 KB Output is correct
15 Correct 9 ms 20556 KB Output is correct
16 Correct 9 ms 20556 KB Output is correct
17 Correct 9 ms 20576 KB Output is correct
18 Correct 10 ms 20652 KB Output is correct
19 Correct 9 ms 20556 KB Output is correct
20 Correct 9 ms 20564 KB Output is correct
21 Correct 10 ms 20684 KB Output is correct
22 Correct 10 ms 20684 KB Output is correct
23 Correct 10 ms 20724 KB Output is correct
24 Correct 10 ms 20672 KB Output is correct
25 Correct 10 ms 20672 KB Output is correct
26 Correct 10 ms 20684 KB Output is correct
27 Correct 10 ms 20620 KB Output is correct
28 Correct 10 ms 20684 KB Output is correct
29 Correct 10 ms 20704 KB Output is correct
30 Correct 10 ms 20684 KB Output is correct
31 Correct 10 ms 20684 KB Output is correct
32 Correct 10 ms 20724 KB Output is correct
33 Correct 11 ms 20684 KB Output is correct
34 Correct 10 ms 20684 KB Output is correct
35 Correct 10 ms 20684 KB Output is correct
36 Correct 9 ms 20684 KB Output is correct
37 Correct 10 ms 20684 KB Output is correct
38 Correct 10 ms 20684 KB Output is correct
39 Correct 119 ms 26272 KB Output is correct
40 Correct 117 ms 25968 KB Output is correct
41 Correct 134 ms 26524 KB Output is correct
42 Correct 130 ms 27104 KB Output is correct
43 Correct 55 ms 26120 KB Output is correct
44 Correct 56 ms 26052 KB Output is correct
45 Correct 107 ms 28744 KB Output is correct
46 Correct 101 ms 32684 KB Output is correct
47 Correct 150 ms 31616 KB Output is correct
48 Correct 147 ms 42692 KB Output is correct
49 Correct 156 ms 41684 KB Output is correct
50 Correct 147 ms 31604 KB Output is correct
51 Correct 146 ms 31628 KB Output is correct
52 Correct 182 ms 31688 KB Output is correct
53 Correct 170 ms 30400 KB Output is correct
54 Correct 122 ms 30224 KB Output is correct
55 Correct 18 ms 21264 KB Output is correct
56 Correct 19 ms 21380 KB Output is correct
57 Correct 101 ms 27180 KB Output is correct
58 Correct 38 ms 27560 KB Output is correct
59 Correct 124 ms 34020 KB Output is correct
60 Correct 493 ms 46252 KB Output is correct
61 Correct 173 ms 34060 KB Output is correct
62 Correct 206 ms 35632 KB Output is correct
63 Correct 283 ms 35644 KB Output is correct
64 Correct 541 ms 35344 KB Output is correct
65 Correct 143 ms 33604 KB Output is correct
66 Correct 345 ms 42028 KB Output is correct
67 Correct 100 ms 36788 KB Output is correct
68 Correct 252 ms 34720 KB Output is correct
69 Correct 266 ms 34872 KB Output is correct
70 Correct 262 ms 34488 KB Output is correct