106472024-04-07 18:03:41Valaki2Labirintuscpp17Time limit exceeded 0/1002.101s119012 KiB
#include "labirintus.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 1000;

vector<pii > directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

int n, m;
bool wall[1 + maxn + 1][1 + maxn + 1];

pii par[1 + maxn + 1][1 + maxn + 1];
int sz[1 + maxn + 1][1 + maxn + 1];

pii par_ori[1 + maxn + 1][1 + maxn + 1];
int sz_ori[1 + maxn + 1][1 + maxn + 1];

pii find_set_compress(pii cur) {
    if(cur == par[cur.fi][cur.se]) {
        return cur;
    }
    return par[cur.fi][cur.se] = find_set_compress(par[cur.fi][cur.se]);
}

void union_sets_compress(pii cur, pii nei) {
    cur = find_set_compress(cur), nei = find_set_compress(nei);
    if(cur == nei) {
        return;
    }
    if(sz[cur.fi][cur.se] > sz[nei.fi][nei.se]) {
        swap(cur, nei);
    }
    par[cur.fi][cur.se] = nei;
    sz[nei.fi][nei.se] += sz[cur.fi][cur.se];
}

void init_labyrinth(int R, int C, vector<vector<int> > L) {
    n = R, m = C;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(L[i - 1][j - 1] == 1) {
                wall[i][j] = true;
            }
        }
    }
    for(int i = 2; i <= n + 1; i++) {
        wall[i][0] = true;
    }
    for(int i = 0; i <= n - 1; i++) {
        wall[i][m + 1] = true;
    }
    for(int j = 2; j <= m + 1; j++) {
        wall[0][j] = true;
    }
    for(int j = 0; j <= m - 1; j++) {
        wall[n + 1][j] = true;
    }
    /*for(int i = 0; i <= n + 1; i++) {
        for(int j = 0; j <= m + 1; j++) {
            cout << wall[i][j];
        }
        cout << "\n";
    }*/
    for(int i = 0; i <= n + 1; i++) {
        for(int j = 0; j <= m + 1; j++) {
            par[i][j] = mp(i, j);
            sz[i][j] = 1;
        }
    }
    for(int i = 0; i <= n + 1; i++) {
        for(int j = 0; j <= m + 1; j++) {
            if(!wall[i][j]) {
                continue;
            }
            for(pii d : directions) {
                int a = i + d.fi, b = j + d.se;
                if(a >= 0 && a <= n + 1 && b >= 0 && b <= m + 1 && wall[a][b]) {
                    union_sets_compress(mp(i, j), mp(a, b));
                }
            }
        }
    }
    for(int i = 0; i <= n + 1; i++) {
        for(int j = 0; j <= m + 1; j++) {
            find_set_compress(mp(i, j));
        }
    }
    for(int i = 0; i <= n + 1; i++) {
        for(int j = 0; j <= m + 1; j++) {
            par_ori[i][j] = par[i][j];
            sz_ori[i][j] = sz[i][j];
        }
    }
}

vector<pii > changes;

pii find_set(pii cur) {
    if(cur == par[cur.fi][cur.se]) {
        return cur;
    }
    return find_set(par[cur.fi][cur.se]);
}

void union_sets(pii cur, pii nei) {
    cur = find_set(cur), nei = find_set(nei);
    if(cur == nei) {
        return;
    }
    if(sz[cur.fi][cur.se] > sz[nei.fi][nei.se]) {
        swap(cur, nei);
    }
    par[cur.fi][cur.se] = nei;
    sz[nei.fi][nei.se] += sz[cur.fi][cur.se];
    changes.pb(cur);
    changes.pb(nei);
}

bool can_escape(int N, vector<int> U, vector<int> V) {
    for(int i = 0; i < N; i++) {
        int x = U[i], y = V[i];
        x++;
        y++;
        for(pii d : directions) {
            int a = x + d.fi, b = y + d.se;
            if(wall[a][b]) {
                union_sets(mp(x, y), mp(a, b));
            }
        }
    }
    bool ans = !(find_set(mp(0, 2)) == find_set(mp(2, 0)));
    for(pii p : changes) {
        par[p.fi][p.se] = par_ori[p.fi][p.se];
        sz[p.fi][p.se] = sz_ori[p.fi][p.se];
    }
    return ans;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2068 KiB
2Time limit exceeded2.101s34788 KiB
subtask20/15
3Accepted83ms16644 KiB
4Accepted10ms13208 KiB
5Wrong answer107ms11892 KiB
6Wrong answer112ms12668 KiB
7Accepted206ms37144 KiB
8Accepted207ms38968 KiB
subtask30/18
9Time limit exceeded2.068s32216 KiB
10Time limit exceeded2.058s16820 KiB
11Time limit exceeded2.062s46628 KiB
12Time limit exceeded2.055s46884 KiB
13Time limit exceeded2.059s46668 KiB
14Time limit exceeded2.086s46980 KiB
subtask40/28
15Time limit exceeded2.065s51388 KiB
16Time limit exceeded2.068s53532 KiB
17Time limit exceeded2.066s54968 KiB
18Accepted640ms84204 KiB
19Time limit exceeded2.065s55300 KiB
20Accepted145ms83892 KiB
21Accepted150ms83816 KiB
22Time limit exceeded2.072s55368 KiB
23Time limit exceeded2.079s55576 KiB
subtask50/39
24Time limit exceeded2.065s21044 KiB
25Wrong answer521ms21672 KiB
26Wrong answer105ms23760 KiB
27Wrong answer112ms21852 KiB
28Wrong answer107ms21768 KiB
29Wrong answer112ms24172 KiB
30Time limit exceeded2.084s54448 KiB
31Time limit exceeded2.059s55612 KiB
32Time limit exceeded2.072s69516 KiB
33Time limit exceeded2.051s58180 KiB
34Time limit exceeded2.075s59104 KiB
35Time limit exceeded2.055s60012 KiB
36Accepted1.149s119012 KiB
37Time limit exceeded2.062s66728 KiB
38Accepted449ms106104 KiB
39Time limit exceeded2.069s69088 KiB