Submission #428803

# Submission time Handle Problem Language Result Execution time Memory
428803 2021-06-15T14:35:17 Z Hegdahl Swapping Cities (APIO20_swap) C++17
6 / 100
545 ms 35924 KB
#include "swap.h"
#include <bits/stdc++.h>
#define ar array

using namespace std;

const int mxN = 4*2e5;

int boss[mxN], lk[mxN], rk[mxN], ndw[mxN], has_cycle[mxN], nxt;

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

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

  lk[nxt] = i;
  rk[nxt] = j;
  has_cycle[nxt] = has_cycle[i] || has_cycle[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]);

  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])
      up[i][lvl] = 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 (!has_cycle[up[i][lvl]])
      i = up[i][lvl];

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

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

  return ndw[i];
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9628 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9932 KB Output is correct
7 Correct 7 ms 9824 KB Output is correct
8 Correct 9 ms 9928 KB Output is correct
9 Correct 76 ms 25292 KB Output is correct
10 Correct 115 ms 28816 KB Output is correct
11 Correct 98 ms 28560 KB Output is correct
12 Correct 118 ms 29632 KB Output is correct
13 Correct 111 ms 31384 KB Output is correct
14 Correct 111 ms 25432 KB Output is correct
15 Correct 301 ms 30488 KB Output is correct
16 Correct 311 ms 29956 KB Output is correct
17 Correct 371 ms 31156 KB Output is correct
18 Correct 446 ms 32796 KB Output is correct
19 Correct 134 ms 16164 KB Output is correct
20 Correct 322 ms 31604 KB Output is correct
21 Correct 333 ms 31116 KB Output is correct
22 Correct 358 ms 32412 KB Output is correct
23 Correct 545 ms 33960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Incorrect 453 ms 35924 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9628 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9932 KB Output is correct
7 Correct 7 ms 9824 KB Output is correct
8 Correct 9 ms 9928 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Incorrect 8 ms 9804 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 8 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9628 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 7 ms 9804 KB Output is correct
7 Correct 7 ms 9932 KB Output is correct
8 Correct 7 ms 9824 KB Output is correct
9 Correct 9 ms 9928 KB Output is correct
10 Correct 76 ms 25292 KB Output is correct
11 Correct 115 ms 28816 KB Output is correct
12 Correct 98 ms 28560 KB Output is correct
13 Correct 118 ms 29632 KB Output is correct
14 Correct 111 ms 31384 KB Output is correct
15 Incorrect 8 ms 9804 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9628 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9932 KB Output is correct
7 Correct 7 ms 9824 KB Output is correct
8 Correct 9 ms 9928 KB Output is correct
9 Correct 76 ms 25292 KB Output is correct
10 Correct 115 ms 28816 KB Output is correct
11 Correct 98 ms 28560 KB Output is correct
12 Correct 118 ms 29632 KB Output is correct
13 Correct 111 ms 31384 KB Output is correct
14 Correct 111 ms 25432 KB Output is correct
15 Correct 301 ms 30488 KB Output is correct
16 Correct 311 ms 29956 KB Output is correct
17 Correct 371 ms 31156 KB Output is correct
18 Correct 446 ms 32796 KB Output is correct
19 Incorrect 453 ms 35924 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 8 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9628 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 7 ms 9804 KB Output is correct
7 Correct 7 ms 9932 KB Output is correct
8 Correct 7 ms 9824 KB Output is correct
9 Correct 9 ms 9928 KB Output is correct
10 Correct 76 ms 25292 KB Output is correct
11 Correct 115 ms 28816 KB Output is correct
12 Correct 98 ms 28560 KB Output is correct
13 Correct 118 ms 29632 KB Output is correct
14 Correct 111 ms 31384 KB Output is correct
15 Correct 111 ms 25432 KB Output is correct
16 Correct 301 ms 30488 KB Output is correct
17 Correct 311 ms 29956 KB Output is correct
18 Correct 371 ms 31156 KB Output is correct
19 Correct 446 ms 32796 KB Output is correct
20 Incorrect 453 ms 35924 KB Output isn't correct
21 Halted 0 ms 0 KB -