Submission #429184

# Submission time Handle Problem Language Result Execution time Memory
429184 2021-06-15T18:24:46 Z Hegdahl Swapping Cities (APIO20_swap) C++17
100 / 100
434 ms 33732 KB
#pragma GCC optimize("Ofast")
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

const int mxN = 3*2e5-2;

int deg[mxN], boss[mxN], lk[mxN], rk[mxN], ndw[mxN], can_pass[mxN], nxt;

int find(int i) {
  return i == boss[i] ? i : boss[i] = find(boss[i]);
}

void unite(int i, int j, int w) {
  bool tri = ++deg[i] >= 3 || ++deg[j] >= 3;

  i = find(i), j = find(j);
  if (i == j) {
    if (can_pass[i]) return;
    lk[nxt] = i;
    can_pass[nxt] = 1;
    ndw[nxt] = w;
    boss[i] = nxt++;
    return;
  }

  lk[nxt] = i;
  rk[nxt] = j;
  can_pass[nxt] = tri || can_pass[i] || can_pass[j];
  ndw[nxt] = w;
  boss[i] = boss[j] = nxt++;
}

int up[mxN][20], depth[mxN];
void dfs(int cur, int prv, int d) {
  depth[cur] = d;
  up[cur][0] = prv;
  for (int lvl = 0; lvl < 19; ++lvl)
    up[cur][lvl+1] = up[up[cur][lvl]][lvl];

  if (lk[cur] != -1) dfs(lk[cur], cur, d+1);
  if (rk[cur] != -1) dfs(rk[cur], cur, d+1);
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
  iota(boss, boss+mxN, 0);
  fill(lk, lk+mxN, -1);
  fill(rk, rk+mxN, -1);
  nxt = n;

  vector<int> s(m);
  iota(s.begin(), s.end(), 0);
  sort(s.begin(), s.end(), [&](int i, int j) { return w[i] < w[j]; });
  for (int i : s) unite(u[i], v[i], w[i]);

  for (int i = 1; i < n; ++i)
    assert(find(i) == find(0));

  dfs(find(0), find(0), 0);
}

int getMinimumFuelCapacity(int i, int j) {
  if (depth[i] > depth[j]) swap(i, j);

  for (int lvl = 19; lvl >= 0; --lvl)
    if (depth[up[j][lvl]] >= depth[i])
      j = up[j][lvl];

  for (int lvl = 19; lvl >= 0; --lvl)
    if (up[i][lvl] != up[j][lvl])
      i = up[i][lvl], j = up[j][lvl];

  if (i != j) i = up[i][0], j = up[j][0];

  assert(i == j);

  for (int lvl = 19; lvl >= 0; --lvl)
    if (!can_pass[up[i][lvl]])
      i = up[i][lvl];

  if (!can_pass[i]) i = up[i][0];

  if (!can_pass[i]) return -1;

  return ndw[i];
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7500 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7500 KB Output is correct
8 Correct 5 ms 7480 KB Output is correct
9 Correct 73 ms 23320 KB Output is correct
10 Correct 98 ms 26920 KB Output is correct
11 Correct 95 ms 26504 KB Output is correct
12 Correct 100 ms 27712 KB Output is correct
13 Correct 94 ms 28228 KB Output is correct
14 Correct 91 ms 23360 KB Output is correct
15 Correct 293 ms 28568 KB Output is correct
16 Correct 283 ms 28032 KB Output is correct
17 Correct 290 ms 29376 KB Output is correct
18 Correct 411 ms 29888 KB Output is correct
19 Correct 130 ms 13896 KB Output is correct
20 Correct 278 ms 29632 KB Output is correct
21 Correct 283 ms 29188 KB Output is correct
22 Correct 293 ms 30484 KB Output is correct
23 Correct 421 ms 31024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 384 ms 31012 KB Output is correct
4 Correct 386 ms 31904 KB Output is correct
5 Correct 384 ms 31588 KB Output is correct
6 Correct 382 ms 31908 KB Output is correct
7 Correct 411 ms 32028 KB Output is correct
8 Correct 379 ms 30884 KB Output is correct
9 Correct 387 ms 31648 KB Output is correct
10 Correct 387 ms 30736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7500 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7500 KB Output is correct
8 Correct 5 ms 7480 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7500 KB Output is correct
11 Correct 4 ms 7500 KB Output is correct
12 Correct 6 ms 7500 KB Output is correct
13 Correct 5 ms 7424 KB Output is correct
14 Correct 5 ms 7500 KB Output is correct
15 Correct 5 ms 7500 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 4 ms 7500 KB Output is correct
18 Correct 4 ms 7500 KB Output is correct
19 Correct 5 ms 7500 KB Output is correct
20 Correct 4 ms 7500 KB Output is correct
21 Correct 5 ms 7500 KB Output is correct
22 Correct 5 ms 7372 KB Output is correct
23 Correct 4 ms 7500 KB Output is correct
24 Correct 5 ms 7500 KB Output is correct
25 Correct 5 ms 7628 KB Output is correct
26 Correct 5 ms 7500 KB Output is correct
27 Correct 4 ms 7500 KB Output is correct
28 Correct 5 ms 7500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7500 KB Output is correct
8 Correct 4 ms 7500 KB Output is correct
9 Correct 5 ms 7480 KB Output is correct
10 Correct 73 ms 23320 KB Output is correct
11 Correct 98 ms 26920 KB Output is correct
12 Correct 95 ms 26504 KB Output is correct
13 Correct 100 ms 27712 KB Output is correct
14 Correct 94 ms 28228 KB Output is correct
15 Correct 4 ms 7500 KB Output is correct
16 Correct 4 ms 7500 KB Output is correct
17 Correct 6 ms 7500 KB Output is correct
18 Correct 5 ms 7424 KB Output is correct
19 Correct 5 ms 7500 KB Output is correct
20 Correct 5 ms 7500 KB Output is correct
21 Correct 5 ms 7500 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 7500 KB Output is correct
24 Correct 5 ms 7500 KB Output is correct
25 Correct 4 ms 7500 KB Output is correct
26 Correct 5 ms 7500 KB Output is correct
27 Correct 5 ms 7372 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 5 ms 7628 KB Output is correct
31 Correct 5 ms 7500 KB Output is correct
32 Correct 4 ms 7500 KB Output is correct
33 Correct 5 ms 7500 KB Output is correct
34 Correct 14 ms 10028 KB Output is correct
35 Correct 97 ms 27720 KB Output is correct
36 Correct 99 ms 27628 KB Output is correct
37 Correct 97 ms 27700 KB Output is correct
38 Correct 100 ms 27380 KB Output is correct
39 Correct 97 ms 27312 KB Output is correct
40 Correct 84 ms 25536 KB Output is correct
41 Correct 100 ms 27712 KB Output is correct
42 Correct 100 ms 27716 KB Output is correct
43 Correct 96 ms 28472 KB Output is correct
44 Correct 100 ms 27716 KB Output is correct
45 Correct 116 ms 25472 KB Output is correct
46 Correct 95 ms 27844 KB Output is correct
47 Correct 99 ms 27712 KB Output is correct
48 Correct 109 ms 27716 KB Output is correct
49 Correct 74 ms 11844 KB Output is correct
50 Correct 75 ms 11972 KB Output is correct
51 Correct 99 ms 22356 KB Output is correct
52 Correct 131 ms 29336 KB Output is correct
53 Correct 152 ms 32916 KB Output is correct
54 Correct 155 ms 30440 KB Output is correct
55 Correct 95 ms 28356 KB Output is correct
56 Correct 146 ms 30096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7500 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7500 KB Output is correct
8 Correct 5 ms 7480 KB Output is correct
9 Correct 73 ms 23320 KB Output is correct
10 Correct 98 ms 26920 KB Output is correct
11 Correct 95 ms 26504 KB Output is correct
12 Correct 100 ms 27712 KB Output is correct
13 Correct 94 ms 28228 KB Output is correct
14 Correct 91 ms 23360 KB Output is correct
15 Correct 293 ms 28568 KB Output is correct
16 Correct 283 ms 28032 KB Output is correct
17 Correct 290 ms 29376 KB Output is correct
18 Correct 411 ms 29888 KB Output is correct
19 Correct 384 ms 31012 KB Output is correct
20 Correct 386 ms 31904 KB Output is correct
21 Correct 384 ms 31588 KB Output is correct
22 Correct 382 ms 31908 KB Output is correct
23 Correct 411 ms 32028 KB Output is correct
24 Correct 379 ms 30884 KB Output is correct
25 Correct 387 ms 31648 KB Output is correct
26 Correct 387 ms 30736 KB Output is correct
27 Correct 4 ms 7500 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 6 ms 7500 KB Output is correct
30 Correct 5 ms 7424 KB Output is correct
31 Correct 5 ms 7500 KB Output is correct
32 Correct 5 ms 7500 KB Output is correct
33 Correct 5 ms 7500 KB Output is correct
34 Correct 4 ms 7500 KB Output is correct
35 Correct 4 ms 7500 KB Output is correct
36 Correct 14 ms 10028 KB Output is correct
37 Correct 97 ms 27720 KB Output is correct
38 Correct 99 ms 27628 KB Output is correct
39 Correct 97 ms 27700 KB Output is correct
40 Correct 100 ms 27380 KB Output is correct
41 Correct 97 ms 27312 KB Output is correct
42 Correct 84 ms 25536 KB Output is correct
43 Correct 100 ms 27712 KB Output is correct
44 Correct 100 ms 27716 KB Output is correct
45 Correct 96 ms 28472 KB Output is correct
46 Correct 100 ms 27716 KB Output is correct
47 Correct 28 ms 10124 KB Output is correct
48 Correct 291 ms 29860 KB Output is correct
49 Correct 286 ms 29892 KB Output is correct
50 Correct 280 ms 29748 KB Output is correct
51 Correct 281 ms 29756 KB Output is correct
52 Correct 256 ms 28624 KB Output is correct
53 Correct 231 ms 24056 KB Output is correct
54 Correct 285 ms 30504 KB Output is correct
55 Correct 301 ms 29984 KB Output is correct
56 Correct 434 ms 30448 KB Output is correct
57 Correct 289 ms 30492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7500 KB Output is correct
8 Correct 4 ms 7500 KB Output is correct
9 Correct 5 ms 7480 KB Output is correct
10 Correct 73 ms 23320 KB Output is correct
11 Correct 98 ms 26920 KB Output is correct
12 Correct 95 ms 26504 KB Output is correct
13 Correct 100 ms 27712 KB Output is correct
14 Correct 94 ms 28228 KB Output is correct
15 Correct 91 ms 23360 KB Output is correct
16 Correct 293 ms 28568 KB Output is correct
17 Correct 283 ms 28032 KB Output is correct
18 Correct 290 ms 29376 KB Output is correct
19 Correct 411 ms 29888 KB Output is correct
20 Correct 384 ms 31012 KB Output is correct
21 Correct 386 ms 31904 KB Output is correct
22 Correct 384 ms 31588 KB Output is correct
23 Correct 382 ms 31908 KB Output is correct
24 Correct 411 ms 32028 KB Output is correct
25 Correct 379 ms 30884 KB Output is correct
26 Correct 387 ms 31648 KB Output is correct
27 Correct 387 ms 30736 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 4 ms 7500 KB Output is correct
30 Correct 6 ms 7500 KB Output is correct
31 Correct 5 ms 7424 KB Output is correct
32 Correct 5 ms 7500 KB Output is correct
33 Correct 5 ms 7500 KB Output is correct
34 Correct 5 ms 7500 KB Output is correct
35 Correct 4 ms 7500 KB Output is correct
36 Correct 4 ms 7500 KB Output is correct
37 Correct 14 ms 10028 KB Output is correct
38 Correct 97 ms 27720 KB Output is correct
39 Correct 99 ms 27628 KB Output is correct
40 Correct 97 ms 27700 KB Output is correct
41 Correct 100 ms 27380 KB Output is correct
42 Correct 97 ms 27312 KB Output is correct
43 Correct 84 ms 25536 KB Output is correct
44 Correct 100 ms 27712 KB Output is correct
45 Correct 100 ms 27716 KB Output is correct
46 Correct 96 ms 28472 KB Output is correct
47 Correct 100 ms 27716 KB Output is correct
48 Correct 28 ms 10124 KB Output is correct
49 Correct 291 ms 29860 KB Output is correct
50 Correct 286 ms 29892 KB Output is correct
51 Correct 280 ms 29748 KB Output is correct
52 Correct 281 ms 29756 KB Output is correct
53 Correct 256 ms 28624 KB Output is correct
54 Correct 231 ms 24056 KB Output is correct
55 Correct 285 ms 30504 KB Output is correct
56 Correct 301 ms 29984 KB Output is correct
57 Correct 434 ms 30448 KB Output is correct
58 Correct 289 ms 30492 KB Output is correct
59 Correct 130 ms 13896 KB Output is correct
60 Correct 278 ms 29632 KB Output is correct
61 Correct 283 ms 29188 KB Output is correct
62 Correct 293 ms 30484 KB Output is correct
63 Correct 421 ms 31024 KB Output is correct
64 Correct 5 ms 7500 KB Output is correct
65 Correct 4 ms 7500 KB Output is correct
66 Correct 5 ms 7500 KB Output is correct
67 Correct 5 ms 7372 KB Output is correct
68 Correct 4 ms 7500 KB Output is correct
69 Correct 5 ms 7500 KB Output is correct
70 Correct 5 ms 7628 KB Output is correct
71 Correct 5 ms 7500 KB Output is correct
72 Correct 4 ms 7500 KB Output is correct
73 Correct 5 ms 7500 KB Output is correct
74 Correct 116 ms 25472 KB Output is correct
75 Correct 95 ms 27844 KB Output is correct
76 Correct 99 ms 27712 KB Output is correct
77 Correct 109 ms 27716 KB Output is correct
78 Correct 74 ms 11844 KB Output is correct
79 Correct 75 ms 11972 KB Output is correct
80 Correct 99 ms 22356 KB Output is correct
81 Correct 131 ms 29336 KB Output is correct
82 Correct 152 ms 32916 KB Output is correct
83 Correct 155 ms 30440 KB Output is correct
84 Correct 95 ms 28356 KB Output is correct
85 Correct 146 ms 30096 KB Output is correct
86 Correct 82 ms 14636 KB Output is correct
87 Correct 286 ms 29796 KB Output is correct
88 Correct 288 ms 29868 KB Output is correct
89 Correct 340 ms 28692 KB Output is correct
90 Correct 211 ms 13764 KB Output is correct
91 Correct 233 ms 15332 KB Output is correct
92 Correct 306 ms 24624 KB Output is correct
93 Correct 321 ms 31156 KB Output is correct
94 Correct 385 ms 33732 KB Output is correct
95 Correct 346 ms 31944 KB Output is correct
96 Correct 430 ms 30684 KB Output is correct
97 Correct 371 ms 31216 KB Output is correct