54282023-05-19 00:24:36TomaSajtLabirintuscpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1816 KiB
2Elfogadva180ms40868 KiB
subtask215/15
3Elfogadva180ms40868 KiB
4Elfogadva32ms3672 KiB
5Elfogadva8ms4004 KiB
6Elfogadva118ms3268 KiB
7Elfogadva118ms3076 KiB
8Elfogadva82ms4276 KiB
9Elfogadva82ms4396 KiB
subtask318/18
10Elfogadva82ms4396 KiB
11Elfogadva214ms4068 KiB
12Elfogadva186ms3588 KiB
13Elfogadva423ms42208 KiB
14Elfogadva391ms42480 KiB
15Elfogadva402ms42436 KiB
16Elfogadva428ms42432 KiB
subtask428/28
17Elfogadva428ms42432 KiB
18Elfogadva261ms42804 KiB
19Elfogadva259ms42616 KiB
20Elfogadva266ms42868 KiB
21Elfogadva158ms42952 KiB
22Elfogadva256ms43136 KiB
23Elfogadva122ms43164 KiB
24Elfogadva123ms43104 KiB
25Elfogadva254ms43624 KiB
26Elfogadva252ms43572 KiB
subtask539/39
27Elfogadva252ms43572 KiB
28Elfogadva37ms4456 KiB
29Elfogadva18ms4428 KiB
30Elfogadva118ms5012 KiB
31Elfogadva118ms4556 KiB
32Elfogadva116ms4660 KiB
33Elfogadva118ms4464 KiB
34Elfogadva411ms43160 KiB
35Elfogadva386ms43252 KiB
36Elfogadva259ms43336 KiB
37Elfogadva407ms43596 KiB
38Elfogadva229ms43620 KiB
39Elfogadva229ms43520 KiB
40Elfogadva286ms43796 KiB
41Elfogadva316ms43572 KiB
42Elfogadva293ms43488 KiB
43Elfogadva296ms43596 KiB