55612023-07-27 11:19:33zsomborLabirintuscpp17Accepted 100/100507ms74028 KiB
#include <iostream>
#include <vector>
#include <queue>
#include "labirintus.h"
using namespace std;

int r,c;
vector <vector <int>> l;
vector <int> p(11e5,0);
vector <int> sz(11e5,1);
queue <int>q;
vector <int> P(11e5,0);
vector <int> SZ(11e5,1);

int holvan(int a){
    return (a==p[a]?a:holvan(p[a]));
}

void unio(int a,int b){
    a=holvan(a);
    b=holvan(b);
    if (a==b) return;
    if (sz[a]>sz[b]) swap(a,b);
    p[a]=b;
    sz[b]+=sz[a];
    q.push(a);
    q.push(b);
}

void betesz(int I,int J){
    l[I][J]=1;
    for (int i=I-1;i<=I+1;i++){
        for (int j=J-1;j<=J+1;j++){
            if (i==r || j==-1) {unio(I*c+J,r*c);continue;}
            if (i==-1 || j==c) {unio(I*c+J,r*c+1);continue;}
            if (l[i][j]) unio(I*c+J,i*c+j);
        }
    }
}

void init_labyrinth(int R, int C, vector <vector <int>> L){
    r=R;c=C;l=L;
    for (int i=0;i<r*c+2;i++) p[i]=i;
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if (l[i][j]) betesz(i,j);
        }
    }
    P=p;SZ=sz;
}

bool can_escape(int N, vector <int> U, vector <int> V){
    for (int i=0;i<N;i++) betesz(U[i],V[i]);
    bool ret=(holvan(r*c)!=holvan(r*c+1));
    for (int i=0;i<N;i++) l[U[i]][V[i]]=0;
    while(q.size()){
        p[q.front()]=P[q.front()];
        sz[q.front()]=SZ[q.front()];
        q.pop();
    }
    return ret;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted19ms36064 KiB
2Accepted193ms68024 KiB
subtask215/15
3Accepted50ms37988 KiB
4Accepted25ms38288 KiB
5Accepted135ms37424 KiB
6Accepted137ms37060 KiB
7Accepted97ms38764 KiB
8Accepted101ms38976 KiB
subtask318/18
9Accepted231ms38664 KiB
10Accepted207ms38140 KiB
11Accepted505ms70756 KiB
12Accepted430ms70792 KiB
13Accepted481ms70796 KiB
14Accepted507ms72588 KiB
subtask428/28
15Accepted282ms71136 KiB
16Accepted280ms71264 KiB
17Accepted286ms71472 KiB
18Accepted166ms71536 KiB
19Accepted266ms72704 KiB
20Accepted136ms71532 KiB
21Accepted131ms71536 KiB
22Accepted270ms72964 KiB
23Accepted268ms72964 KiB
subtask539/39
24Accepted52ms41256 KiB
25Accepted32ms41012 KiB
26Accepted135ms41636 KiB
27Accepted137ms41140 KiB
28Accepted134ms41832 KiB
29Accepted134ms41676 KiB
30Accepted448ms72996 KiB
31Accepted460ms72996 KiB
32Accepted349ms70008 KiB
33Accepted432ms73848 KiB
34Accepted256ms74028 KiB
35Accepted248ms73912 KiB
36Accepted321ms72384 KiB
37Accepted319ms72340 KiB
38Accepted360ms72464 KiB
39Accepted321ms72340 KiB