54282023-05-19 00:24:36TomaSajtLabirintuscpp17Accepted 100/100428ms43796 KiB
// the code before the refactor was originally from mraron on njudge

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

int M, R, C, BL, TR;
vector<vector<int>> labyrinth;
vector<int> sz, par;
stack<pair<int, int>> merged;

int get_root(int x) {
  if (x == par[x]) return x;
  return get_root(par[x]);
}

void merge_comps(int r1, int c1, int r2, int c2, bool push) {
  int x = r1 * C + c1;
  int y = r2 == R  ? BL :
          c2 == -1 ? BL :
          r2 == -1 ? TR :
          c2 == C  ? TR :
                     r2 * C + c2;
  x = get_root(x), y = get_root(y);
  if (x == y) return;
  if (sz[x] < sz[y]) swap(x, y);
  sz[x] += sz[y];
  par[y] = x;
  if (push) merged.push({x, y});
}

bool is_edge(int r, int c) { return r == -1 || r == R || c == -1 || c == C; }

void init_labyrinth(int r, int c, vector<vector<int>> l) {
  R = r, C = c;
  M = R * C;
  BL = M;
  TR = M + 1;
  labyrinth = l;
  sz.assign(M + 2, 1);
  par.resize(M + 2), iota(par.begin(), par.end(), 0);
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (labyrinth[i][j] == 0) continue;
      for (int x = i - 1; x <= i + 1; x++)
        for (int y = j - 1; y <= j + 1; y++) {
          if (!is_edge(x, y) && labyrinth[x][y] == 0) continue;
          merge_comps(i, j, x, y, 0);
        }
    }
  }
}

bool can_escape(int n, vector<int> u, vector<int> v) {
  for (int k = 0; k < n; k++) {
    int i = u[k], j = v[k];
    for (int x = i - 1; x <= i + 1; x++) {
      for (int y = j - 1; y <= j + 1; y++) {
        if (!is_edge(x, y) && labyrinth[x][y] == 0) continue;
        merge_comps(i, j, x, y, 1);
      }
    }
    labyrinth[i][j] = 1;
  }
  bool res = get_root(BL) != get_root(TR);
  while (!merged.empty()) {
    auto [x, y] = merged.top();
    par[y] = y;
    sz[x] -= sz[y];
    merged.pop();
  }

  for (int k = 0; k < n; k++) labyrinth[u[k]][v[k]] = 0;

  return res;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1816 KiB
2Accepted180ms40868 KiB
subtask215/15
3Accepted180ms40868 KiB
4Accepted32ms3672 KiB
5Accepted8ms4004 KiB
6Accepted118ms3268 KiB
7Accepted118ms3076 KiB
8Accepted82ms4276 KiB
9Accepted82ms4396 KiB
subtask318/18
10Accepted82ms4396 KiB
11Accepted214ms4068 KiB
12Accepted186ms3588 KiB
13Accepted423ms42208 KiB
14Accepted391ms42480 KiB
15Accepted402ms42436 KiB
16Accepted428ms42432 KiB
subtask428/28
17Accepted428ms42432 KiB
18Accepted261ms42804 KiB
19Accepted259ms42616 KiB
20Accepted266ms42868 KiB
21Accepted158ms42952 KiB
22Accepted256ms43136 KiB
23Accepted122ms43164 KiB
24Accepted123ms43104 KiB
25Accepted254ms43624 KiB
26Accepted252ms43572 KiB
subtask539/39
27Accepted252ms43572 KiB
28Accepted37ms4456 KiB
29Accepted18ms4428 KiB
30Accepted118ms5012 KiB
31Accepted118ms4556 KiB
32Accepted116ms4660 KiB
33Accepted118ms4464 KiB
34Accepted411ms43160 KiB
35Accepted386ms43252 KiB
36Accepted259ms43336 KiB
37Accepted407ms43596 KiB
38Accepted229ms43620 KiB
39Accepted229ms43520 KiB
40Accepted286ms43796 KiB
41Accepted316ms43572 KiB
42Accepted293ms43488 KiB
43Accepted296ms43596 KiB