92222024-02-18 19:00:22CattLabirintuscpp14Elfogadva 100/100744ms79652 KiB
#include <bits/stdc++.h>
#include "labirintus.h"
using namespace std;

struct me {
    int p, s;
    pair<bool, bool> b;

    me(){}

    me(int _p, int _s, pair<bool, bool> _b) : p(_p), s(_s), b(_b){} 
};

map<int, me > memo;

int n,m;
vector<vector<int> > v;
vector<pair<int, int> > falak;

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;
vector<pair<bool, bool> > block;

bool mo;

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

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

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

bool init;
void unio(int a, int b) {
    if(!init)a = holvanslow(a);
    else a = holvan(a);
    if(!init)b = holvanslow(b);
    else b = holvan(b);

    if(a == b) return;

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

    if(!init) {
        if(memo.find(a) == memo.end()) memo[a] = {a, s[a], block[a]}; 
        if(memo.find(b) == memo.end()) memo[b] = {b, s[b], block[b]}; 
    }
    p[b] = a;
    s[a] += s[b];
    block[a] = {block[a].first | block[b].first, block[a].second | block[b].second};
    if(block[a].first && block[a].second) mo = 1;
}


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

    v.resize(n+1, vector<int>(m+1, 0));

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

    p.resize(n*m+1);
    s.resize(n*m+1, 1);
    block.resize(n*m+1, {0, 0});
    for(int i = 0; i < n*m; i++) p[i] = i;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(i == n-1 || j == 0) block[postoind({i, j})] = {1, 0};
            else if(i == 0 || j == m-1) block[postoind({i, j})] = {0, 1};
        }
    }

    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(fal.first + i >= 0 && fal.second+j >= 0 && v[fal.first + i][fal.second + j]) {
                    unio(postoind(fal), postoind({fal.first+i, fal.second+j}));
                }
            }
        }
    }
    init = 0;
}

bool can_escape(int N, vector<int> U, vector<int> V){
    if(mo) return 0;

    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(cur.first + i >= 0 && cur.second+j >= 0 && v[cur.first + i][cur.second + j]) {
                    unio(postoind(cur), postoind({cur.first+i, cur.second+j}));
                }
            }
        }

        v[U[i]][V[i]] = 1;
    }


    /*map<int, vector<pair<int, int>> > comp;
    for(int i = 0; i < N; i++) {
        pair<int, int> cur = {U[i], V[i]};
        int ind = holvanslow(postoind(cur));
        comp[ind].push_back(cur);
    }
    //cout << "itt1" << endl;

    for(auto i: falak) {
        int ind = holvanslow(postoind(i));
        comp[ind].push_back(i);
    }

    for(auto i : comp) {
        cout << "component: \n";
        for(auto j : i.second) cout << j.first << ", "  << j.second << endl;
        cout << "-------------\n";
    }*/

    bool ret = !mo;
    mo = 0;

    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;

        v[U[i]][V[i]] = 0;
    }

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

    memo.clear();

    return ret;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1960 KiB
2Elfogadva254ms52788 KiB
subtask215/15
3Elfogadva79ms4372 KiB
4Elfogadva8ms4576 KiB
5Elfogadva250ms3868 KiB
6Elfogadva244ms3020 KiB
7Elfogadva194ms4812 KiB
8Elfogadva194ms4948 KiB
subtask318/18
9Elfogadva363ms4260 KiB
10Elfogadva400ms3564 KiB
11Elfogadva592ms55924 KiB
12Elfogadva579ms58268 KiB
13Elfogadva587ms60152 KiB
14Elfogadva588ms61976 KiB
subtask428/28
15Elfogadva455ms62516 KiB
16Elfogadva460ms64064 KiB
17Elfogadva469ms65724 KiB
18Elfogadva207ms65956 KiB
19Elfogadva433ms67084 KiB
20Elfogadva148ms66012 KiB
21Elfogadva136ms66004 KiB
22Elfogadva437ms67172 KiB
23Elfogadva437ms67172 KiB
subtask539/39
24Elfogadva45ms16612 KiB
25Elfogadva23ms16416 KiB
26Elfogadva250ms17452 KiB
27Elfogadva244ms16644 KiB
28Elfogadva246ms16912 KiB
29Elfogadva244ms16644 KiB
30Elfogadva583ms69252 KiB
31Elfogadva566ms70772 KiB
32Elfogadva744ms69824 KiB
33Elfogadva591ms73556 KiB
34Elfogadva361ms73764 KiB
35Elfogadva370ms73720 KiB
36Elfogadva569ms74856 KiB
37Elfogadva500ms76456 KiB
38Elfogadva651ms78020 KiB
39Elfogadva490ms79652 KiB