답안 #961449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961449 2024-04-12T06:20:46 Z kilkuwu 자매 도시 (APIO20_swap) C++17
100 / 100
306 ms 41688 KB
#include "swap.h"

#include <bits/stdc++.h>

template <typename T>
inline bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}

template <typename T>
inline bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}

constexpr int mxM = 200'000;
constexpr int mxN = 100'000;
constexpr int LOG = 20;
constexpr int inf = 1e9 + 7;

struct Edge {
  int u, v, w;

  bool operator<(const Edge& rhs) const {
    return w < rhs.w;
  }

  inline int other(int x) { return u ^ v ^ x; }
};

Edge edges[mxM];
std::vector<std::pair<int, int>> adj[mxN];

int up[LOG][mxN];
int vv[LOG][mxN];
int dep[mxN];

int num_nodes;

std::pair<int, int> par[mxN + mxM];

std::pair<int, int> find(int u) {
  if (par[u].first == -1) {
    return {u, par[u].second};
  }

  auto pu = find(par[u].first);
  pu.second = std::min(pu.second, par[u].second);

  return par[u] = pu;
}

void merge(int u, int v, int w) {

  auto pu = find(u), pv = find(v);

  par[pu.first].first = par[pv.first].first = num_nodes;

  if (par[pu.first].second <= 1e9) {
    ckmin(par[num_nodes].second, w);
  }

  if (par[pv.first].second <= 1e9) {
    ckmin(par[num_nodes].second, w);
  }

  if (pu.first == pv.first) {
    ckmin(par[num_nodes].second, w);
  } else {
    adj[u].emplace_back(w, v);
    adj[v].emplace_back(w, u);
  }

  if (adj[u].size() > 2 || adj[v].size() > 2) {
    ckmin(par[num_nodes].second, w);
  }

  //std::cerr << u << " " << v << " " << w << " " << par[num_nodes].second << std::endl;
  num_nodes++;
}

void dfs(int u) {
  for (auto [w, v] : adj[u]) {
    if (v == up[0][u]) continue;
    dep[v] = dep[u] + 1;
    up[0][v] = u;
    vv[0][v] = w;
    dfs(v);
  } 
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  
  for (int i = 0; i < N + M; i++) {
    par[i] = {-1, inf};
  }

  num_nodes = N;
  for (int i = 0; i < M; i++) {
    edges[i] = {U[i], V[i], W[i]};
  }

  std::sort(edges, edges + M);

  for (int i = 0; i < M; i++) {
    auto [u, v, w] = edges[i];
    merge(u, v, w);
  }

  dfs(0);

  for (int k = 0; k + 1 < LOG; k++) {
    for (int i = 0; i < N; i++) {
      up[k + 1][i] = up[k][up[k][i]];
      vv[k + 1][i] = std::max(vv[k][i], vv[k][up[k][i]]);
    }
  }
}

int max_path(int u, int v) {
  if (dep[u] > dep[v]) std::swap(u, v);
  int d = dep[v] - dep[u];
  int ans = 0;
  for (int k = 0; k < LOG; k++) {
    if (d >> k & 1) {
      ckmax(ans, vv[k][v]);
      v = up[k][v];
    }
  }

  if (u == v) return ans;

  for (int k = LOG - 1; k >= 0; k--) {
    if (up[k][u] != up[k][v]) {
      ckmax(ans, vv[k][u]);
      ckmax(ans, vv[k][v]);
      u = up[k][u];
      v = up[k][v];
    }
  }

  ckmax(ans, vv[0][u]);
  ckmax(ans, vv[0][v]);

  return ans;
}

int getMinimumFuelCapacity(int X, int Y) {
  int ans = std::min(find(X).second, find(Y).second);
  ans = std::max(ans, max_path(X, Y));
  return ans <= 1e9 ? ans : -1; 
}

/*

8 7
2 0 9
2 1 3
1 3 1
0 5 6
5 4 5
1 6 3
3 7 4
1 
0 4 


*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 46 ms 31568 KB Output is correct
10 Correct 72 ms 35104 KB Output is correct
11 Correct 62 ms 34456 KB Output is correct
12 Correct 61 ms 35484 KB Output is correct
13 Correct 56 ms 36688 KB Output is correct
14 Correct 62 ms 31316 KB Output is correct
15 Correct 224 ms 38744 KB Output is correct
16 Correct 230 ms 37216 KB Output is correct
17 Correct 191 ms 40884 KB Output is correct
18 Correct 231 ms 39732 KB Output is correct
19 Correct 68 ms 28500 KB Output is correct
20 Correct 229 ms 39380 KB Output is correct
21 Correct 215 ms 38672 KB Output is correct
22 Correct 273 ms 39772 KB Output is correct
23 Correct 232 ms 40152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 127 ms 36276 KB Output is correct
4 Correct 178 ms 37676 KB Output is correct
5 Correct 117 ms 37728 KB Output is correct
6 Correct 117 ms 36544 KB Output is correct
7 Correct 121 ms 36008 KB Output is correct
8 Correct 140 ms 36132 KB Output is correct
9 Correct 124 ms 35904 KB Output is correct
10 Correct 118 ms 35460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 3 ms 18776 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 19036 KB Output is correct
12 Correct 3 ms 18776 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 4 ms 19036 KB Output is correct
16 Correct 3 ms 19288 KB Output is correct
17 Correct 3 ms 19036 KB Output is correct
18 Correct 3 ms 18780 KB Output is correct
19 Correct 3 ms 18780 KB Output is correct
20 Correct 3 ms 19036 KB Output is correct
21 Correct 3 ms 18780 KB Output is correct
22 Correct 3 ms 18988 KB Output is correct
23 Correct 3 ms 18776 KB Output is correct
24 Correct 3 ms 19036 KB Output is correct
25 Correct 3 ms 19036 KB Output is correct
26 Correct 4 ms 19288 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19032 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 3 ms 19036 KB Output is correct
10 Correct 46 ms 31568 KB Output is correct
11 Correct 72 ms 35104 KB Output is correct
12 Correct 62 ms 34456 KB Output is correct
13 Correct 61 ms 35484 KB Output is correct
14 Correct 56 ms 36688 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 19036 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
18 Correct 3 ms 18780 KB Output is correct
19 Correct 4 ms 18780 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 3 ms 19288 KB Output is correct
22 Correct 3 ms 19036 KB Output is correct
23 Correct 3 ms 18780 KB Output is correct
24 Correct 3 ms 18780 KB Output is correct
25 Correct 3 ms 19036 KB Output is correct
26 Correct 3 ms 18780 KB Output is correct
27 Correct 3 ms 18988 KB Output is correct
28 Correct 3 ms 18776 KB Output is correct
29 Correct 3 ms 19036 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 4 ms 19288 KB Output is correct
32 Correct 3 ms 19036 KB Output is correct
33 Correct 4 ms 19040 KB Output is correct
34 Correct 10 ms 22248 KB Output is correct
35 Correct 75 ms 32868 KB Output is correct
36 Correct 68 ms 30808 KB Output is correct
37 Correct 59 ms 29208 KB Output is correct
38 Correct 59 ms 28956 KB Output is correct
39 Correct 55 ms 28496 KB Output is correct
40 Correct 53 ms 27968 KB Output is correct
41 Correct 80 ms 31308 KB Output is correct
42 Correct 66 ms 32220 KB Output is correct
43 Correct 54 ms 33448 KB Output is correct
44 Correct 75 ms 29008 KB Output is correct
45 Correct 84 ms 28920 KB Output is correct
46 Correct 57 ms 30020 KB Output is correct
47 Correct 66 ms 28752 KB Output is correct
48 Correct 70 ms 28860 KB Output is correct
49 Correct 75 ms 24824 KB Output is correct
50 Correct 55 ms 24204 KB Output is correct
51 Correct 69 ms 27672 KB Output is correct
52 Correct 91 ms 31380 KB Output is correct
53 Correct 103 ms 31628 KB Output is correct
54 Correct 138 ms 34156 KB Output is correct
55 Correct 67 ms 36200 KB Output is correct
56 Correct 100 ms 34268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 46 ms 31568 KB Output is correct
10 Correct 72 ms 35104 KB Output is correct
11 Correct 62 ms 34456 KB Output is correct
12 Correct 61 ms 35484 KB Output is correct
13 Correct 56 ms 36688 KB Output is correct
14 Correct 62 ms 31316 KB Output is correct
15 Correct 224 ms 38744 KB Output is correct
16 Correct 230 ms 37216 KB Output is correct
17 Correct 191 ms 40884 KB Output is correct
18 Correct 231 ms 39732 KB Output is correct
19 Correct 127 ms 36276 KB Output is correct
20 Correct 178 ms 37676 KB Output is correct
21 Correct 117 ms 37728 KB Output is correct
22 Correct 117 ms 36544 KB Output is correct
23 Correct 121 ms 36008 KB Output is correct
24 Correct 140 ms 36132 KB Output is correct
25 Correct 124 ms 35904 KB Output is correct
26 Correct 118 ms 35460 KB Output is correct
27 Correct 3 ms 18780 KB Output is correct
28 Correct 3 ms 19036 KB Output is correct
29 Correct 3 ms 18776 KB Output is correct
30 Correct 3 ms 18780 KB Output is correct
31 Correct 4 ms 18780 KB Output is correct
32 Correct 4 ms 19036 KB Output is correct
33 Correct 3 ms 19288 KB Output is correct
34 Correct 3 ms 19036 KB Output is correct
35 Correct 3 ms 18780 KB Output is correct
36 Correct 10 ms 22248 KB Output is correct
37 Correct 75 ms 32868 KB Output is correct
38 Correct 68 ms 30808 KB Output is correct
39 Correct 59 ms 29208 KB Output is correct
40 Correct 59 ms 28956 KB Output is correct
41 Correct 55 ms 28496 KB Output is correct
42 Correct 53 ms 27968 KB Output is correct
43 Correct 80 ms 31308 KB Output is correct
44 Correct 66 ms 32220 KB Output is correct
45 Correct 54 ms 33448 KB Output is correct
46 Correct 75 ms 29008 KB Output is correct
47 Correct 18 ms 22364 KB Output is correct
48 Correct 273 ms 33972 KB Output is correct
49 Correct 268 ms 32948 KB Output is correct
50 Correct 251 ms 31932 KB Output is correct
51 Correct 252 ms 31552 KB Output is correct
52 Correct 197 ms 30884 KB Output is correct
53 Correct 135 ms 30020 KB Output is correct
54 Correct 306 ms 38292 KB Output is correct
55 Correct 231 ms 38520 KB Output is correct
56 Correct 215 ms 40632 KB Output is correct
57 Correct 249 ms 36612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19032 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 3 ms 19036 KB Output is correct
10 Correct 46 ms 31568 KB Output is correct
11 Correct 72 ms 35104 KB Output is correct
12 Correct 62 ms 34456 KB Output is correct
13 Correct 61 ms 35484 KB Output is correct
14 Correct 56 ms 36688 KB Output is correct
15 Correct 62 ms 31316 KB Output is correct
16 Correct 224 ms 38744 KB Output is correct
17 Correct 230 ms 37216 KB Output is correct
18 Correct 191 ms 40884 KB Output is correct
19 Correct 231 ms 39732 KB Output is correct
20 Correct 127 ms 36276 KB Output is correct
21 Correct 178 ms 37676 KB Output is correct
22 Correct 117 ms 37728 KB Output is correct
23 Correct 117 ms 36544 KB Output is correct
24 Correct 121 ms 36008 KB Output is correct
25 Correct 140 ms 36132 KB Output is correct
26 Correct 124 ms 35904 KB Output is correct
27 Correct 118 ms 35460 KB Output is correct
28 Correct 3 ms 18780 KB Output is correct
29 Correct 3 ms 19036 KB Output is correct
30 Correct 3 ms 18776 KB Output is correct
31 Correct 3 ms 18780 KB Output is correct
32 Correct 4 ms 18780 KB Output is correct
33 Correct 4 ms 19036 KB Output is correct
34 Correct 3 ms 19288 KB Output is correct
35 Correct 3 ms 19036 KB Output is correct
36 Correct 3 ms 18780 KB Output is correct
37 Correct 10 ms 22248 KB Output is correct
38 Correct 75 ms 32868 KB Output is correct
39 Correct 68 ms 30808 KB Output is correct
40 Correct 59 ms 29208 KB Output is correct
41 Correct 59 ms 28956 KB Output is correct
42 Correct 55 ms 28496 KB Output is correct
43 Correct 53 ms 27968 KB Output is correct
44 Correct 80 ms 31308 KB Output is correct
45 Correct 66 ms 32220 KB Output is correct
46 Correct 54 ms 33448 KB Output is correct
47 Correct 75 ms 29008 KB Output is correct
48 Correct 18 ms 22364 KB Output is correct
49 Correct 273 ms 33972 KB Output is correct
50 Correct 268 ms 32948 KB Output is correct
51 Correct 251 ms 31932 KB Output is correct
52 Correct 252 ms 31552 KB Output is correct
53 Correct 197 ms 30884 KB Output is correct
54 Correct 135 ms 30020 KB Output is correct
55 Correct 306 ms 38292 KB Output is correct
56 Correct 231 ms 38520 KB Output is correct
57 Correct 215 ms 40632 KB Output is correct
58 Correct 249 ms 36612 KB Output is correct
59 Correct 68 ms 28500 KB Output is correct
60 Correct 229 ms 39380 KB Output is correct
61 Correct 215 ms 38672 KB Output is correct
62 Correct 273 ms 39772 KB Output is correct
63 Correct 232 ms 40152 KB Output is correct
64 Correct 3 ms 18780 KB Output is correct
65 Correct 3 ms 19036 KB Output is correct
66 Correct 3 ms 18780 KB Output is correct
67 Correct 3 ms 18988 KB Output is correct
68 Correct 3 ms 18776 KB Output is correct
69 Correct 3 ms 19036 KB Output is correct
70 Correct 3 ms 19036 KB Output is correct
71 Correct 4 ms 19288 KB Output is correct
72 Correct 3 ms 19036 KB Output is correct
73 Correct 4 ms 19040 KB Output is correct
74 Correct 84 ms 28920 KB Output is correct
75 Correct 57 ms 30020 KB Output is correct
76 Correct 66 ms 28752 KB Output is correct
77 Correct 70 ms 28860 KB Output is correct
78 Correct 75 ms 24824 KB Output is correct
79 Correct 55 ms 24204 KB Output is correct
80 Correct 69 ms 27672 KB Output is correct
81 Correct 91 ms 31380 KB Output is correct
82 Correct 103 ms 31628 KB Output is correct
83 Correct 138 ms 34156 KB Output is correct
84 Correct 67 ms 36200 KB Output is correct
85 Correct 100 ms 34268 KB Output is correct
86 Correct 75 ms 26068 KB Output is correct
87 Correct 280 ms 36504 KB Output is correct
88 Correct 280 ms 36556 KB Output is correct
89 Correct 182 ms 35280 KB Output is correct
90 Correct 138 ms 31904 KB Output is correct
91 Correct 171 ms 32352 KB Output is correct
92 Correct 160 ms 35252 KB Output is correct
93 Correct 290 ms 39820 KB Output is correct
94 Correct 232 ms 39616 KB Output is correct
95 Correct 294 ms 41688 KB Output is correct
96 Correct 246 ms 38992 KB Output is correct
97 Correct 238 ms 37896 KB Output is correct