5395 | 2023-05-09 14:12:07 | mraron | Labirintus | cpp17 | Accepted 100/100 | 439ms | 43344 KiB |
#include "labirintus.h"
#include <algorithm>
using namespace std;
int M, R, C;
vector<vector<int>> labyrinth;
vector<int> sz, par;
vector<pair<int,int>> hist;
int find_par(int x) {
return x==par[x] ? x : find_par(par[x]);
}
void unite(int r1, int c1, int r2, int c2, bool save=false) {
int x = r1*C+c1, y;
if (r2<0 || r2>=R) y = (r2==R ? M-2 : M-1);
else if (c2<0 || c2>=C) y = (c2==C ? M-1 : M-2);
else y = r2*C+c2;
x = find_par(x), y = find_par(y);
if (x==y) return;
if (sz[x] < sz[y]) swap(x,y);
sz[x] += sz[y];
par[y] = x;
if (save) hist.push_back({x, y});
}
void init_labyrinth(int _R, int _C, std::vector<std::vector<int>> L) {
R = _R, C = _C;
labyrinth = L;
M = R*C+2;
sz.assign(M, 1), par.resize(M);
for (int i=0; i<M; ++i) par[i] = i;
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 (x<0 || x>=R || y<0 || y>=C || labyrinth[x][y] == 1)
unite(i,j, x,y);
}
}
}
bool can_escape(int N, std::vector<int> U, std::vector<int> V) {
hist.clear();
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 (x<0 || x>=R || y<0 || y>=C || labyrinth[x][y] == 1)
unite(i,j, x,y, true);
labyrinth[i][j] = 1;
}
bool ans = (find_par(M-2) != find_par(M-1));
reverse(hist.begin(), hist.end());
for (auto [x,y] : hist) {
par[y] = y;
sz[x] -= sz[y];
}
for (int k=0; k<N; ++k) {
labyrinth[U[k]][V[k]] = 0;
}
return ans;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1328 KiB | ||||
2 | Accepted | 178ms | 40500 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 32ms | 3180 KiB | ||||
4 | Accepted | 8ms | 3396 KiB | ||||
5 | Accepted | 115ms | 2892 KiB | ||||
6 | Accepted | 115ms | 2608 KiB | ||||
7 | Accepted | 81ms | 3800 KiB | ||||
8 | Accepted | 82ms | 4164 KiB | ||||
subtask3 | 18/18 | ||||||
9 | Accepted | 209ms | 3684 KiB | ||||
10 | Accepted | 182ms | 3084 KiB | ||||
11 | Accepted | 416ms | 41684 KiB | ||||
12 | Accepted | 402ms | 42024 KiB | ||||
13 | Accepted | 398ms | 41976 KiB | ||||
14 | Accepted | 439ms | 42076 KiB | ||||
subtask4 | 28/28 | ||||||
15 | Accepted | 257ms | 42412 KiB | ||||
16 | Accepted | 259ms | 42632 KiB | ||||
17 | Accepted | 263ms | 42576 KiB | ||||
18 | Accepted | 150ms | 42704 KiB | ||||
19 | Accepted | 252ms | 42636 KiB | ||||
20 | Accepted | 120ms | 42592 KiB | ||||
21 | Accepted | 122ms | 42920 KiB | ||||
22 | Accepted | 250ms | 42604 KiB | ||||
23 | Accepted | 246ms | 42864 KiB | ||||
subtask5 | 39/39 | ||||||
24 | Accepted | 35ms | 3960 KiB | ||||
25 | Accepted | 17ms | 3868 KiB | ||||
26 | Accepted | 115ms | 4420 KiB | ||||
27 | Accepted | 115ms | 3960 KiB | ||||
28 | Accepted | 115ms | 4184 KiB | ||||
29 | Accepted | 115ms | 3968 KiB | ||||
30 | Accepted | 432ms | 42984 KiB | ||||
31 | Accepted | 402ms | 43036 KiB | ||||
32 | Accepted | 275ms | 43032 KiB | ||||
33 | Accepted | 405ms | 43236 KiB | ||||
34 | Accepted | 229ms | 43316 KiB | ||||
35 | Accepted | 228ms | 43276 KiB | ||||
36 | Accepted | 310ms | 43180 KiB | ||||
37 | Accepted | 287ms | 43180 KiB | ||||
38 | Accepted | 294ms | 43244 KiB | ||||
39 | Accepted | 289ms | 43344 KiB |