91952024-02-18 13:42:51CattLabirintuscpp17Time limit exceeded 15/1002.092s163516 KiB
#include <bits/stdc++.h>
#include "labirintus.h"
using namespace std;
using ll = long long;

int n,m;
set<pair<int, int> > falak, orok;

int postoind(pair<int, int> pos) {
    return pos.first * m + pos.second;
}

pair<int, int> indtopos(int ind) {
    return {ind / m, ind % m};
}

vector<int> p, s;

int holvan(int a ){
    if(p[a] == a) return a;

    return (p[a] = holvan(p[a]));
}

void unio(int a, int b) {
    a = holvan(a);
    b = holvan(b);

    if(a == b) return;

    if(s[a] < s[b]) swap(a, b);

    p[b] = a;
    s[a] += s[b]; 
}

map<pair<int, int>, pair<int, int> > memo;

void init_labyrinth(int R, int C, vector<vector<int> > L) {
    n = R;
    m = C;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(L[i][j]) falak.insert({i, j});
        }
    }

    p.resize(n*m+1);
    s.resize(n*m+1, 1);
    for(int i = 0; i < n*m; i++) p[i] = i;

    for(auto fal : falak) {
        for(int i = -1; i <= 1; i++) {
            for(int j = -1; j <= 1; j++) {
                if(i == 0 && j == 0) continue;

                if(falak.find({fal.first+i, fal.second+j}) != falak.end()) {
                    unio(postoind(fal), postoind({fal.first+i, fal.second+j}));
                }
            }
        }
    }

    for(auto fal : falak) {
        memo[fal] = {p[postoind(fal)], s[postoind(fal)]};
    }
}

bool can_escape(int N, vector<int> U, vector<int> V){
    for(int i = 0; i < N; i++) {
        pair<int, int> cur = {U[i], V[i]};

        for(int i = -1; i <= 1; i++) {
            for(int j = -1; j <= 1; j++) {
                if(i == 0 && j == 0) continue;

                if(falak.find({cur.first+i, cur.second+j}) != falak.end()) {
                    unio(postoind(cur), postoind({cur.first+i, cur.second+j}));
                }

                if(orok.find({cur.first+i, cur.second+j}) != orok.end()) {
                    unio(postoind(cur), postoind({cur.first+i, cur.second+j}));
                }
            }
        }

        orok.insert(cur);
    }

    map<int, vector<pair<int, int>>> comp;

    for(int i = 0; i < N; i++) {
        pair<int, int> cur = {U[i], V[i]};
        int ind = holvan(postoind(cur));

        comp[ind].push_back({U[i], V[i]});
    }

    for(auto fal : falak) {
        int ind = holvan(postoind(fal));

        comp[ind].push_back(fal);
    }

    bool ret = 1;
    for(auto c : comp) {
        bool b1 = 0, b2 = 0;

        for(auto i : c.second) {
            if(i.first == n-1 || i.second == 0) b1 = 1;
            if(i.first == 0 || i.second == m-1) b2 = 1;
        }

        /*cout << "component: \n";
        for(auto i : c.second) cout << i.first << ", " << i.second << endl;
        cout << "b1: " << b1 << ", b2: " << b2 << endl;
        cout << "------------------\n";*/
        
        ret &= !(b1 & b2);
    }

    for(auto i : memo) {
        p[postoind(i.first)] = i.second.first;
        s[postoind(i.first)] = i.second.second;
    }

    for(int i = 0; i < N; i++) {
        pair<int, int> cur = {U[i], V[i]};
        int ind = postoind(cur);

        p[ind]  = ind;
        s[ind] = 1;
    }
    orok.clear();

    return ret;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2152 KiB
2Time limit exceeded2.085s78024 KiB
subtask215/15
3Accepted402ms9920 KiB
4Accepted126ms10208 KiB
5Accepted430ms8164 KiB
6Accepted419ms10348 KiB
7Accepted809ms17744 KiB
8Accepted809ms19844 KiB
subtask30/18
9Time limit exceeded2.046s14488 KiB
10Time limit exceeded2.073s15112 KiB
11Time limit exceeded2.065s92928 KiB
12Time limit exceeded2.079s95916 KiB
13Time limit exceeded2.071s97164 KiB
14Time limit exceeded2.092s97912 KiB
subtask40/28
15Time limit exceeded2.068s88720 KiB
16Time limit exceeded2.062s90320 KiB
17Time limit exceeded2.078s91616 KiB
18Time limit exceeded2.051s93216 KiB
19Time limit exceeded2.078s97428 KiB
20Accepted1.48s163516 KiB
21Time limit exceeded2.071s96520 KiB
22Time limit exceeded2.078s100932 KiB
23Time limit exceeded2.069s102360 KiB
subtask50/39
24Accepted61ms33396 KiB
25Accepted29ms33500 KiB
26Accepted432ms37616 KiB
27Accepted419ms39500 KiB
28Accepted418ms42620 KiB
29Accepted418ms45144 KiB
30Time limit exceeded2.088s122988 KiB
31Time limit exceeded2.072s125788 KiB
32Time limit exceeded2.069s116924 KiB
33Time limit exceeded2.065s127960 KiB
34Time limit exceeded2.069s125824 KiB
35Time limit exceeded2.076s126140 KiB
36Time limit exceeded2.071s120652 KiB
37Time limit exceeded2.056s124080 KiB
38Time limit exceeded2.042s129600 KiB
39Time limit exceeded2.046s125268 KiB