106492024-04-07 18:13:26Valaki2Labirintuscpp17Accepted 100/100560ms72992 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;
    }
    changes.pb(cur);
    return par[cur.fi][cur.se] = 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 guard[1 + maxn + 1][1 + maxn + 1];

bool can_escape(int N, vector<int> U, vector<int> V) {
    for(int i = 0; i < N; i++) {
        guard[U[i] + 1][V[i] + 1] = true;
    }
    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] || guard[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];
    }
    for(int i = 0; i < N; i++) {
        guard[U[i] + 1][V[i] + 1] = false;
    }
    changes.clear();
    return ans;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2032 KiB
2Accepted222ms66712 KiB
subtask215/15
3Accepted43ms11280 KiB
4Accepted10ms11344 KiB
5Accepted155ms7328 KiB
6Accepted155ms4848 KiB
7Accepted111ms12216 KiB
8Accepted112ms12112 KiB
subtask318/18
9Accepted289ms39560 KiB
10Accepted233ms3464 KiB
11Accepted560ms67540 KiB
12Accepted517ms67664 KiB
13Accepted536ms67624 KiB
14Accepted560ms67624 KiB
subtask428/28
15Accepted351ms67748 KiB
16Accepted354ms67748 KiB
17Accepted365ms67816 KiB
18Accepted195ms67828 KiB
19Accepted340ms68112 KiB
20Accepted146ms68056 KiB
21Accepted153ms68304 KiB
22Accepted337ms68264 KiB
23Accepted337ms68392 KiB
subtask539/39
24Accepted50ms4348 KiB
25Accepted25ms4248 KiB
26Accepted155ms8656 KiB
27Accepted155ms6032 KiB
28Accepted153ms6516 KiB
29Accepted155ms6104 KiB
30Accepted550ms68428 KiB
31Accepted510ms68560 KiB
32Accepted386ms68556 KiB
33Accepted542ms68704 KiB
34Accepted291ms68700 KiB
35Accepted284ms68556 KiB
36Accepted535ms68812 KiB
37Accepted444ms68836 KiB
38Accepted479ms72992 KiB
39Accepted442ms68836 KiB