Submission #979600

# Submission time Handle Problem Language Result Execution time Memory
979600 2024-05-11T08:05:00 Z bubble_xd Swapping Cities (APIO20_swap) C++17
100 / 100
273 ms 44448 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int nax = 300005;
const int K = 23;

int fa[nax], front[nax], back[nax], val[nax];
bool chain[nax];
int N, M, id;
vector<pair<int, pair<int, int>>> edges; // (W, index)
int G[nax][2], sp[nax][K], dep[nax];

int find(int x) {
  if (x == fa[x]) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}

void dfs(int u, int f) {
  if (!u) return;
  dep[u] = dep[f] + 1;
  sp[u][0] = f;
  for (int i = 1; i < K; i++) sp[u][i] = sp[sp[u][i - 1]][i - 1];
  for (int child = 0; child < 2; child++) {
    dfs(G[u][child], u);
  } 
}

int lca(int u, int v) {
  if (dep[v] >= dep[u]) swap(u, v);
  for (int i = K - 1; i >= 0; i--) if (dep[sp[u][i]] >= dep[v]) u = sp[u][i];
  if (u == v) return u;
  for (int i = K - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
  return sp[u][0];
}
void init(int _N, int _M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  N = _N; M = _M;
  for (int i = 0; i < M; i++) 
    edges.push_back(make_pair(W[i], make_pair(U[i], V[i])));
  sort(edges.begin(), edges.end());
  for (int i = 0; i < nax; i++) fa[i] = i;
  for (int i = 1; i <= N; i++) {
    chain[i] = true;
    front[i] = back[i] = i;
  }
  id = N;
  for (int i = 0; i < M; i++) {
    int w = edges[i].first;
    int u = edges[i].second.first + 1;
    int v = edges[i].second.second + 1;

    int A = find(u), B = find(v);
    if (A == B) {
      if (chain[A]) {
        int new_id = ++id;
        fa[A] = new_id;
        val[new_id] = w;
        G[new_id][0] = A;
      }
      continue;
    }
    int new_id = ++id;
    fa[A] = new_id;
    fa[B] = new_id;
    val[new_id] = w;
    G[new_id][0] = A;
    G[new_id][1] = B;
    if (chain[A] && chain[B]) {
      if (front[A] == v || back[A] == v) swap(A, B);
      // connect u -> v 
      // x -> u -> v -> y
      if (front[A] == u) swap(front[A], back[A]);
      if (back[B] == v) swap(front[B], back[B]);
      if (back[A] == u && front[B] == v) {
        chain[new_id] = true;
        front[new_id] = front[A];
        back[new_id] = back[B];
      }
    }
  }
  dfs(id, 0);
}

int getMinimumFuelCapacity(int X, int Y) {
  X++; Y++;
  int l = lca(X, Y);
  if (!chain[l]) return val[l];
  for (int i = K - 1; i >= 0; i--) {
    if (sp[l][i] && chain[sp[l][i]]) {
      l = sp[l][i];
    }
  }
  if (l == id) return -1;
  return val[sp[l][0]];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11156 KB Output is correct
4 Correct 2 ms 11096 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 10992 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 56 ms 28376 KB Output is correct
10 Correct 79 ms 29212 KB Output is correct
11 Correct 65 ms 29036 KB Output is correct
12 Correct 69 ms 31184 KB Output is correct
13 Correct 77 ms 32468 KB Output is correct
14 Correct 64 ms 28624 KB Output is correct
15 Correct 200 ms 30800 KB Output is correct
16 Correct 184 ms 31092 KB Output is correct
17 Correct 205 ms 33100 KB Output is correct
18 Correct 247 ms 34268 KB Output is correct
19 Correct 93 ms 16632 KB Output is correct
20 Correct 189 ms 34024 KB Output is correct
21 Correct 188 ms 32128 KB Output is correct
22 Correct 205 ms 34368 KB Output is correct
23 Correct 259 ms 35556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 213 ms 36344 KB Output is correct
4 Correct 208 ms 38780 KB Output is correct
5 Correct 227 ms 36832 KB Output is correct
6 Correct 220 ms 38576 KB Output is correct
7 Correct 207 ms 38864 KB Output is correct
8 Correct 212 ms 36368 KB Output is correct
9 Correct 212 ms 38724 KB Output is correct
10 Correct 192 ms 36392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11156 KB Output is correct
4 Correct 2 ms 11096 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 10992 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 2 ms 11096 KB Output is correct
10 Correct 3 ms 11100 KB Output is correct
11 Correct 2 ms 11168 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 2 ms 11096 KB Output is correct
20 Correct 3 ms 11100 KB Output is correct
21 Correct 2 ms 11100 KB Output is correct
22 Correct 2 ms 11100 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 3 ms 11100 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 4 ms 11096 KB Output is correct
27 Correct 2 ms 11096 KB Output is correct
28 Correct 2 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11156 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11096 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 10992 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 56 ms 28376 KB Output is correct
11 Correct 79 ms 29212 KB Output is correct
12 Correct 65 ms 29036 KB Output is correct
13 Correct 69 ms 31184 KB Output is correct
14 Correct 77 ms 32468 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 2 ms 11168 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 3 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 2 ms 11100 KB Output is correct
22 Correct 2 ms 11100 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 2 ms 11096 KB Output is correct
25 Correct 3 ms 11100 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 2 ms 11100 KB Output is correct
28 Correct 2 ms 11100 KB Output is correct
29 Correct 3 ms 11100 KB Output is correct
30 Correct 2 ms 11100 KB Output is correct
31 Correct 4 ms 11096 KB Output is correct
32 Correct 2 ms 11096 KB Output is correct
33 Correct 2 ms 11100 KB Output is correct
34 Correct 9 ms 12824 KB Output is correct
35 Correct 73 ms 31232 KB Output is correct
36 Correct 69 ms 31400 KB Output is correct
37 Correct 73 ms 31200 KB Output is correct
38 Correct 78 ms 31172 KB Output is correct
39 Correct 70 ms 31184 KB Output is correct
40 Correct 61 ms 30920 KB Output is correct
41 Correct 72 ms 31256 KB Output is correct
42 Correct 69 ms 31256 KB Output is correct
43 Correct 83 ms 32948 KB Output is correct
44 Correct 71 ms 31172 KB Output is correct
45 Correct 81 ms 31484 KB Output is correct
46 Correct 67 ms 31400 KB Output is correct
47 Correct 68 ms 31432 KB Output is correct
48 Correct 74 ms 32200 KB Output is correct
49 Correct 49 ms 17856 KB Output is correct
50 Correct 41 ms 16528 KB Output is correct
51 Correct 78 ms 28616 KB Output is correct
52 Correct 93 ms 33476 KB Output is correct
53 Correct 113 ms 36488 KB Output is correct
54 Correct 103 ms 35156 KB Output is correct
55 Correct 67 ms 32708 KB Output is correct
56 Correct 116 ms 34564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11156 KB Output is correct
4 Correct 2 ms 11096 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 10992 KB Output is correct
8 Correct 3 ms 11100 KB Output is correct
9 Correct 56 ms 28376 KB Output is correct
10 Correct 79 ms 29212 KB Output is correct
11 Correct 65 ms 29036 KB Output is correct
12 Correct 69 ms 31184 KB Output is correct
13 Correct 77 ms 32468 KB Output is correct
14 Correct 64 ms 28624 KB Output is correct
15 Correct 200 ms 30800 KB Output is correct
16 Correct 184 ms 31092 KB Output is correct
17 Correct 205 ms 33100 KB Output is correct
18 Correct 247 ms 34268 KB Output is correct
19 Correct 213 ms 36344 KB Output is correct
20 Correct 208 ms 38780 KB Output is correct
21 Correct 227 ms 36832 KB Output is correct
22 Correct 220 ms 38576 KB Output is correct
23 Correct 207 ms 38864 KB Output is correct
24 Correct 212 ms 36368 KB Output is correct
25 Correct 212 ms 38724 KB Output is correct
26 Correct 192 ms 36392 KB Output is correct
27 Correct 3 ms 11100 KB Output is correct
28 Correct 2 ms 11168 KB Output is correct
29 Correct 2 ms 11100 KB Output is correct
30 Correct 2 ms 11100 KB Output is correct
31 Correct 3 ms 11100 KB Output is correct
32 Correct 2 ms 11100 KB Output is correct
33 Correct 2 ms 11100 KB Output is correct
34 Correct 2 ms 11100 KB Output is correct
35 Correct 2 ms 11100 KB Output is correct
36 Correct 9 ms 12824 KB Output is correct
37 Correct 73 ms 31232 KB Output is correct
38 Correct 69 ms 31400 KB Output is correct
39 Correct 73 ms 31200 KB Output is correct
40 Correct 78 ms 31172 KB Output is correct
41 Correct 70 ms 31184 KB Output is correct
42 Correct 61 ms 30920 KB Output is correct
43 Correct 72 ms 31256 KB Output is correct
44 Correct 69 ms 31256 KB Output is correct
45 Correct 83 ms 32948 KB Output is correct
46 Correct 71 ms 31172 KB Output is correct
47 Correct 20 ms 13796 KB Output is correct
48 Correct 194 ms 38004 KB Output is correct
49 Correct 174 ms 37936 KB Output is correct
50 Correct 195 ms 37956 KB Output is correct
51 Correct 173 ms 37944 KB Output is correct
52 Correct 167 ms 35652 KB Output is correct
53 Correct 139 ms 33212 KB Output is correct
54 Correct 182 ms 38852 KB Output is correct
55 Correct 206 ms 38240 KB Output is correct
56 Correct 273 ms 39104 KB Output is correct
57 Correct 174 ms 38956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11156 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11096 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 10992 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 56 ms 28376 KB Output is correct
11 Correct 79 ms 29212 KB Output is correct
12 Correct 65 ms 29036 KB Output is correct
13 Correct 69 ms 31184 KB Output is correct
14 Correct 77 ms 32468 KB Output is correct
15 Correct 64 ms 28624 KB Output is correct
16 Correct 200 ms 30800 KB Output is correct
17 Correct 184 ms 31092 KB Output is correct
18 Correct 205 ms 33100 KB Output is correct
19 Correct 247 ms 34268 KB Output is correct
20 Correct 213 ms 36344 KB Output is correct
21 Correct 208 ms 38780 KB Output is correct
22 Correct 227 ms 36832 KB Output is correct
23 Correct 220 ms 38576 KB Output is correct
24 Correct 207 ms 38864 KB Output is correct
25 Correct 212 ms 36368 KB Output is correct
26 Correct 212 ms 38724 KB Output is correct
27 Correct 192 ms 36392 KB Output is correct
28 Correct 3 ms 11100 KB Output is correct
29 Correct 2 ms 11168 KB Output is correct
30 Correct 2 ms 11100 KB Output is correct
31 Correct 2 ms 11100 KB Output is correct
32 Correct 3 ms 11100 KB Output is correct
33 Correct 2 ms 11100 KB Output is correct
34 Correct 2 ms 11100 KB Output is correct
35 Correct 2 ms 11100 KB Output is correct
36 Correct 2 ms 11100 KB Output is correct
37 Correct 9 ms 12824 KB Output is correct
38 Correct 73 ms 31232 KB Output is correct
39 Correct 69 ms 31400 KB Output is correct
40 Correct 73 ms 31200 KB Output is correct
41 Correct 78 ms 31172 KB Output is correct
42 Correct 70 ms 31184 KB Output is correct
43 Correct 61 ms 30920 KB Output is correct
44 Correct 72 ms 31256 KB Output is correct
45 Correct 69 ms 31256 KB Output is correct
46 Correct 83 ms 32948 KB Output is correct
47 Correct 71 ms 31172 KB Output is correct
48 Correct 20 ms 13796 KB Output is correct
49 Correct 194 ms 38004 KB Output is correct
50 Correct 174 ms 37936 KB Output is correct
51 Correct 195 ms 37956 KB Output is correct
52 Correct 173 ms 37944 KB Output is correct
53 Correct 167 ms 35652 KB Output is correct
54 Correct 139 ms 33212 KB Output is correct
55 Correct 182 ms 38852 KB Output is correct
56 Correct 206 ms 38240 KB Output is correct
57 Correct 273 ms 39104 KB Output is correct
58 Correct 174 ms 38956 KB Output is correct
59 Correct 93 ms 16632 KB Output is correct
60 Correct 189 ms 34024 KB Output is correct
61 Correct 188 ms 32128 KB Output is correct
62 Correct 205 ms 34368 KB Output is correct
63 Correct 259 ms 35556 KB Output is correct
64 Correct 2 ms 11096 KB Output is correct
65 Correct 3 ms 11100 KB Output is correct
66 Correct 2 ms 11100 KB Output is correct
67 Correct 2 ms 11100 KB Output is correct
68 Correct 2 ms 11100 KB Output is correct
69 Correct 3 ms 11100 KB Output is correct
70 Correct 2 ms 11100 KB Output is correct
71 Correct 4 ms 11096 KB Output is correct
72 Correct 2 ms 11096 KB Output is correct
73 Correct 2 ms 11100 KB Output is correct
74 Correct 81 ms 31484 KB Output is correct
75 Correct 67 ms 31400 KB Output is correct
76 Correct 68 ms 31432 KB Output is correct
77 Correct 74 ms 32200 KB Output is correct
78 Correct 49 ms 17856 KB Output is correct
79 Correct 41 ms 16528 KB Output is correct
80 Correct 78 ms 28616 KB Output is correct
81 Correct 93 ms 33476 KB Output is correct
82 Correct 113 ms 36488 KB Output is correct
83 Correct 103 ms 35156 KB Output is correct
84 Correct 67 ms 32708 KB Output is correct
85 Correct 116 ms 34564 KB Output is correct
86 Correct 52 ms 21456 KB Output is correct
87 Correct 176 ms 37932 KB Output is correct
88 Correct 173 ms 37932 KB Output is correct
89 Correct 194 ms 38832 KB Output is correct
90 Correct 137 ms 24600 KB Output is correct
91 Correct 160 ms 24840 KB Output is correct
92 Correct 181 ms 36140 KB Output is correct
93 Correct 203 ms 42440 KB Output is correct
94 Correct 241 ms 44448 KB Output is correct
95 Correct 251 ms 42740 KB Output is correct
96 Correct 237 ms 39304 KB Output is correct
97 Correct 216 ms 41092 KB Output is correct