Submission #429183

# Submission time Handle Problem Language Result Execution time Memory
429183 2021-06-15T18:22:08 Z Hegdahl Swapping Cities (APIO20_swap) C++17
100 / 100
505 ms 36040 KB
#pragma GCC optimize("Ofast")
#include "swap.h"
#include <bits/stdc++.h>
#define ar array

using namespace std;

const int mxN = 4*2e5;

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 7 ms 9676 KB Output is correct
2 Correct 5 ms 9616 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 6 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 85 ms 25700 KB Output is correct
10 Correct 97 ms 29388 KB Output is correct
11 Correct 93 ms 28936 KB Output is correct
12 Correct 100 ms 30024 KB Output is correct
13 Correct 101 ms 30612 KB Output is correct
14 Correct 96 ms 25732 KB Output is correct
15 Correct 306 ms 30896 KB Output is correct
16 Correct 290 ms 30348 KB Output is correct
17 Correct 325 ms 31556 KB Output is correct
18 Correct 428 ms 32136 KB Output is correct
19 Correct 136 ms 16200 KB Output is correct
20 Correct 289 ms 32048 KB Output is correct
21 Correct 322 ms 31476 KB Output is correct
22 Correct 303 ms 32784 KB Output is correct
23 Correct 505 ms 33376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 5 ms 9616 KB Output is correct
3 Correct 407 ms 33292 KB Output is correct
4 Correct 397 ms 34312 KB Output is correct
5 Correct 417 ms 33820 KB Output is correct
6 Correct 423 ms 34332 KB Output is correct
7 Correct 412 ms 34112 KB Output is correct
8 Correct 425 ms 33264 KB Output is correct
9 Correct 397 ms 33888 KB Output is correct
10 Correct 420 ms 33060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 5 ms 9616 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 6 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 6 ms 9804 KB Output is correct
11 Correct 8 ms 9920 KB Output is correct
12 Correct 6 ms 9804 KB Output is correct
13 Correct 5 ms 9804 KB Output is correct
14 Correct 6 ms 9804 KB Output is correct
15 Correct 6 ms 9804 KB Output is correct
16 Correct 6 ms 9804 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 6 ms 9804 KB Output is correct
19 Correct 6 ms 9804 KB Output is correct
20 Correct 6 ms 9804 KB Output is correct
21 Correct 6 ms 9804 KB Output is correct
22 Correct 6 ms 9804 KB Output is correct
23 Correct 5 ms 9804 KB Output is correct
24 Correct 6 ms 9932 KB Output is correct
25 Correct 9 ms 9932 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 7 ms 9804 KB Output is correct
28 Correct 6 ms 9920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 5 ms 9616 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 5 ms 9712 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 6 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 6 ms 9804 KB Output is correct
10 Correct 85 ms 25700 KB Output is correct
11 Correct 97 ms 29388 KB Output is correct
12 Correct 93 ms 28936 KB Output is correct
13 Correct 100 ms 30024 KB Output is correct
14 Correct 101 ms 30612 KB Output is correct
15 Correct 6 ms 9804 KB Output is correct
16 Correct 8 ms 9920 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9804 KB Output is correct
19 Correct 6 ms 9804 KB Output is correct
20 Correct 6 ms 9804 KB Output is correct
21 Correct 6 ms 9804 KB Output is correct
22 Correct 6 ms 9804 KB Output is correct
23 Correct 6 ms 9804 KB Output is correct
24 Correct 6 ms 9804 KB Output is correct
25 Correct 6 ms 9804 KB Output is correct
26 Correct 6 ms 9804 KB Output is correct
27 Correct 6 ms 9804 KB Output is correct
28 Correct 5 ms 9804 KB Output is correct
29 Correct 6 ms 9932 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
31 Correct 8 ms 9932 KB Output is correct
32 Correct 7 ms 9804 KB Output is correct
33 Correct 6 ms 9920 KB Output is correct
34 Correct 15 ms 12364 KB Output is correct
35 Correct 99 ms 30004 KB Output is correct
36 Correct 106 ms 30072 KB Output is correct
37 Correct 107 ms 30152 KB Output is correct
38 Correct 95 ms 29784 KB Output is correct
39 Correct 98 ms 29636 KB Output is correct
40 Correct 87 ms 27952 KB Output is correct
41 Correct 99 ms 30020 KB Output is correct
42 Correct 118 ms 30152 KB Output is correct
43 Correct 94 ms 30792 KB Output is correct
44 Correct 101 ms 30096 KB Output is correct
45 Correct 119 ms 27844 KB Output is correct
46 Correct 118 ms 30080 KB Output is correct
47 Correct 100 ms 29896 KB Output is correct
48 Correct 108 ms 30300 KB Output is correct
49 Correct 92 ms 14188 KB Output is correct
50 Correct 64 ms 14284 KB Output is correct
51 Correct 99 ms 24596 KB Output is correct
52 Correct 133 ms 31768 KB Output is correct
53 Correct 153 ms 35296 KB Output is correct
54 Correct 200 ms 32732 KB Output is correct
55 Correct 98 ms 30660 KB Output is correct
56 Correct 152 ms 32324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 5 ms 9616 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 6 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 85 ms 25700 KB Output is correct
10 Correct 97 ms 29388 KB Output is correct
11 Correct 93 ms 28936 KB Output is correct
12 Correct 100 ms 30024 KB Output is correct
13 Correct 101 ms 30612 KB Output is correct
14 Correct 96 ms 25732 KB Output is correct
15 Correct 306 ms 30896 KB Output is correct
16 Correct 290 ms 30348 KB Output is correct
17 Correct 325 ms 31556 KB Output is correct
18 Correct 428 ms 32136 KB Output is correct
19 Correct 407 ms 33292 KB Output is correct
20 Correct 397 ms 34312 KB Output is correct
21 Correct 417 ms 33820 KB Output is correct
22 Correct 423 ms 34332 KB Output is correct
23 Correct 412 ms 34112 KB Output is correct
24 Correct 425 ms 33264 KB Output is correct
25 Correct 397 ms 33888 KB Output is correct
26 Correct 420 ms 33060 KB Output is correct
27 Correct 6 ms 9804 KB Output is correct
28 Correct 8 ms 9920 KB Output is correct
29 Correct 6 ms 9804 KB Output is correct
30 Correct 5 ms 9804 KB Output is correct
31 Correct 6 ms 9804 KB Output is correct
32 Correct 6 ms 9804 KB Output is correct
33 Correct 6 ms 9804 KB Output is correct
34 Correct 6 ms 9804 KB Output is correct
35 Correct 6 ms 9804 KB Output is correct
36 Correct 15 ms 12364 KB Output is correct
37 Correct 99 ms 30004 KB Output is correct
38 Correct 106 ms 30072 KB Output is correct
39 Correct 107 ms 30152 KB Output is correct
40 Correct 95 ms 29784 KB Output is correct
41 Correct 98 ms 29636 KB Output is correct
42 Correct 87 ms 27952 KB Output is correct
43 Correct 99 ms 30020 KB Output is correct
44 Correct 118 ms 30152 KB Output is correct
45 Correct 94 ms 30792 KB Output is correct
46 Correct 101 ms 30096 KB Output is correct
47 Correct 29 ms 12440 KB Output is correct
48 Correct 305 ms 32148 KB Output is correct
49 Correct 292 ms 32196 KB Output is correct
50 Correct 306 ms 32320 KB Output is correct
51 Correct 305 ms 32056 KB Output is correct
52 Correct 268 ms 31012 KB Output is correct
53 Correct 253 ms 26424 KB Output is correct
54 Correct 296 ms 32804 KB Output is correct
55 Correct 300 ms 32416 KB Output is correct
56 Correct 479 ms 32824 KB Output is correct
57 Correct 295 ms 32804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 5 ms 9616 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 5 ms 9712 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 6 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 6 ms 9804 KB Output is correct
10 Correct 85 ms 25700 KB Output is correct
11 Correct 97 ms 29388 KB Output is correct
12 Correct 93 ms 28936 KB Output is correct
13 Correct 100 ms 30024 KB Output is correct
14 Correct 101 ms 30612 KB Output is correct
15 Correct 96 ms 25732 KB Output is correct
16 Correct 306 ms 30896 KB Output is correct
17 Correct 290 ms 30348 KB Output is correct
18 Correct 325 ms 31556 KB Output is correct
19 Correct 428 ms 32136 KB Output is correct
20 Correct 407 ms 33292 KB Output is correct
21 Correct 397 ms 34312 KB Output is correct
22 Correct 417 ms 33820 KB Output is correct
23 Correct 423 ms 34332 KB Output is correct
24 Correct 412 ms 34112 KB Output is correct
25 Correct 425 ms 33264 KB Output is correct
26 Correct 397 ms 33888 KB Output is correct
27 Correct 420 ms 33060 KB Output is correct
28 Correct 6 ms 9804 KB Output is correct
29 Correct 8 ms 9920 KB Output is correct
30 Correct 6 ms 9804 KB Output is correct
31 Correct 5 ms 9804 KB Output is correct
32 Correct 6 ms 9804 KB Output is correct
33 Correct 6 ms 9804 KB Output is correct
34 Correct 6 ms 9804 KB Output is correct
35 Correct 6 ms 9804 KB Output is correct
36 Correct 6 ms 9804 KB Output is correct
37 Correct 15 ms 12364 KB Output is correct
38 Correct 99 ms 30004 KB Output is correct
39 Correct 106 ms 30072 KB Output is correct
40 Correct 107 ms 30152 KB Output is correct
41 Correct 95 ms 29784 KB Output is correct
42 Correct 98 ms 29636 KB Output is correct
43 Correct 87 ms 27952 KB Output is correct
44 Correct 99 ms 30020 KB Output is correct
45 Correct 118 ms 30152 KB Output is correct
46 Correct 94 ms 30792 KB Output is correct
47 Correct 101 ms 30096 KB Output is correct
48 Correct 29 ms 12440 KB Output is correct
49 Correct 305 ms 32148 KB Output is correct
50 Correct 292 ms 32196 KB Output is correct
51 Correct 306 ms 32320 KB Output is correct
52 Correct 305 ms 32056 KB Output is correct
53 Correct 268 ms 31012 KB Output is correct
54 Correct 253 ms 26424 KB Output is correct
55 Correct 296 ms 32804 KB Output is correct
56 Correct 300 ms 32416 KB Output is correct
57 Correct 479 ms 32824 KB Output is correct
58 Correct 295 ms 32804 KB Output is correct
59 Correct 136 ms 16200 KB Output is correct
60 Correct 289 ms 32048 KB Output is correct
61 Correct 322 ms 31476 KB Output is correct
62 Correct 303 ms 32784 KB Output is correct
63 Correct 505 ms 33376 KB Output is correct
64 Correct 6 ms 9804 KB Output is correct
65 Correct 6 ms 9804 KB Output is correct
66 Correct 6 ms 9804 KB Output is correct
67 Correct 6 ms 9804 KB Output is correct
68 Correct 5 ms 9804 KB Output is correct
69 Correct 6 ms 9932 KB Output is correct
70 Correct 9 ms 9932 KB Output is correct
71 Correct 8 ms 9932 KB Output is correct
72 Correct 7 ms 9804 KB Output is correct
73 Correct 6 ms 9920 KB Output is correct
74 Correct 119 ms 27844 KB Output is correct
75 Correct 118 ms 30080 KB Output is correct
76 Correct 100 ms 29896 KB Output is correct
77 Correct 108 ms 30300 KB Output is correct
78 Correct 92 ms 14188 KB Output is correct
79 Correct 64 ms 14284 KB Output is correct
80 Correct 99 ms 24596 KB Output is correct
81 Correct 133 ms 31768 KB Output is correct
82 Correct 153 ms 35296 KB Output is correct
83 Correct 200 ms 32732 KB Output is correct
84 Correct 98 ms 30660 KB Output is correct
85 Correct 152 ms 32324 KB Output is correct
86 Correct 91 ms 16968 KB Output is correct
87 Correct 281 ms 32164 KB Output is correct
88 Correct 298 ms 32268 KB Output is correct
89 Correct 348 ms 31096 KB Output is correct
90 Correct 230 ms 16116 KB Output is correct
91 Correct 254 ms 17744 KB Output is correct
92 Correct 330 ms 27000 KB Output is correct
93 Correct 354 ms 33612 KB Output is correct
94 Correct 415 ms 36040 KB Output is correct
95 Correct 359 ms 34424 KB Output is correct
96 Correct 449 ms 33008 KB Output is correct
97 Correct 445 ms 33488 KB Output is correct