5428 | 2023-05-19 00:24:36 | TomaSajt | Labirintus | cpp17 | Elfogadva 100/100 | 428ms | 43796 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 | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 3ms | 1816 KiB | ||||
2 | Elfogadva | 180ms | 40868 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Elfogadva | 180ms | 40868 KiB | ||||
4 | Elfogadva | 32ms | 3672 KiB | ||||
5 | Elfogadva | 8ms | 4004 KiB | ||||
6 | Elfogadva | 118ms | 3268 KiB | ||||
7 | Elfogadva | 118ms | 3076 KiB | ||||
8 | Elfogadva | 82ms | 4276 KiB | ||||
9 | Elfogadva | 82ms | 4396 KiB | ||||
subtask3 | 18/18 | ||||||
10 | Elfogadva | 82ms | 4396 KiB | ||||
11 | Elfogadva | 214ms | 4068 KiB | ||||
12 | Elfogadva | 186ms | 3588 KiB | ||||
13 | Elfogadva | 423ms | 42208 KiB | ||||
14 | Elfogadva | 391ms | 42480 KiB | ||||
15 | Elfogadva | 402ms | 42436 KiB | ||||
16 | Elfogadva | 428ms | 42432 KiB | ||||
subtask4 | 28/28 | ||||||
17 | Elfogadva | 428ms | 42432 KiB | ||||
18 | Elfogadva | 261ms | 42804 KiB | ||||
19 | Elfogadva | 259ms | 42616 KiB | ||||
20 | Elfogadva | 266ms | 42868 KiB | ||||
21 | Elfogadva | 158ms | 42952 KiB | ||||
22 | Elfogadva | 256ms | 43136 KiB | ||||
23 | Elfogadva | 122ms | 43164 KiB | ||||
24 | Elfogadva | 123ms | 43104 KiB | ||||
25 | Elfogadva | 254ms | 43624 KiB | ||||
26 | Elfogadva | 252ms | 43572 KiB | ||||
subtask5 | 39/39 | ||||||
27 | Elfogadva | 252ms | 43572 KiB | ||||
28 | Elfogadva | 37ms | 4456 KiB | ||||
29 | Elfogadva | 18ms | 4428 KiB | ||||
30 | Elfogadva | 118ms | 5012 KiB | ||||
31 | Elfogadva | 118ms | 4556 KiB | ||||
32 | Elfogadva | 116ms | 4660 KiB | ||||
33 | Elfogadva | 118ms | 4464 KiB | ||||
34 | Elfogadva | 411ms | 43160 KiB | ||||
35 | Elfogadva | 386ms | 43252 KiB | ||||
36 | Elfogadva | 259ms | 43336 KiB | ||||
37 | Elfogadva | 407ms | 43596 KiB | ||||
38 | Elfogadva | 229ms | 43620 KiB | ||||
39 | Elfogadva | 229ms | 43520 KiB | ||||
40 | Elfogadva | 286ms | 43796 KiB | ||||
41 | Elfogadva | 316ms | 43572 KiB | ||||
42 | Elfogadva | 293ms | 43488 KiB | ||||
43 | Elfogadva | 296ms | 43596 KiB |