Submission #428812

# Submission time Handle Problem Language Result Execution time Memory
428812 2021-06-15T14:38:28 Z Hegdahl Swapping Cities (APIO20_swap) C++17
0 / 100
216 ms 35980 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]);

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

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

int getMinimumFuelCapacity(int i, int j) {
  return -1; // 2nd subtask, unless i misunderstood

  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 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 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 9904 KB Output is correct
7 Correct 10 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 76 ms 25300 KB Output is correct
10 Correct 110 ms 28880 KB Output is correct
11 Correct 104 ms 28672 KB Output is correct
12 Correct 113 ms 29584 KB Output is correct
13 Correct 122 ms 31336 KB Output is correct
14 Correct 90 ms 25420 KB Output is correct
15 Correct 157 ms 30496 KB Output is correct
16 Correct 150 ms 29960 KB Output is correct
17 Correct 180 ms 31248 KB Output is correct
18 Correct 216 ms 32820 KB Output is correct
19 Incorrect 67 ms 15052 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Incorrect 151 ms 35980 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 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 9904 KB Output is correct
7 Correct 10 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Incorrect 5 ms 9676 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9676 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 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 9904 KB Output is correct
7 Correct 10 ms 9804 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 76 ms 25300 KB Output is correct
10 Correct 110 ms 28880 KB Output is correct
11 Correct 104 ms 28672 KB Output is correct
12 Correct 113 ms 29584 KB Output is correct
13 Correct 122 ms 31336 KB Output is correct
14 Correct 90 ms 25420 KB Output is correct
15 Correct 157 ms 30496 KB Output is correct
16 Correct 150 ms 29960 KB Output is correct
17 Correct 180 ms 31248 KB Output is correct
18 Correct 216 ms 32820 KB Output is correct
19 Incorrect 151 ms 35980 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9676 KB Output isn't correct