結果

問題 No.2412 YOU Grow Bigger!
ユーザー k1suxuk1suxu
提出日時 2023-08-12 00:14:27
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 6,000 ms
コード長 5,394 bytes
コンパイル時間 3,551 ms
コンパイル使用メモリ 261,880 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-01 06:38:58
合計ジャッジ時間 6,087 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 248 ms
6,948 KB
testcase_04 AC 252 ms
6,940 KB
testcase_05 AC 185 ms
6,940 KB
testcase_06 AC 229 ms
6,940 KB
testcase_07 AC 251 ms
6,940 KB
testcase_08 AC 203 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 7 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 2 ms
6,948 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 31 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 6 ms
6,944 KB
testcase_22 AC 10 ms
6,940 KB
testcase_23 AC 64 ms
6,944 KB
testcase_24 AC 125 ms
6,944 KB
testcase_25 AC 57 ms
6,940 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 15 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 5000;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

template<typename T>
struct Edge {
    int to;
    T cap;
    int rev;

    Edge() = default;
    Edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
};

template<typename T>
struct Dinic {
    const T e = numeric_limits<T>::max();
    const T zero = 0;

    int n;
    vector<vector<Edge<T>>> g;
    vector<int> level, iter;
    vector<pair<int, int>> Edge_ID;

    Dinic(int n) : n(n), g(n) {}

    int add_edge(int from, int to, T cap) {
        Edge<T> f(to, cap, g[to].size());
        Edge<T> t(from, zero, g[from].size());

        int Now_Edge_ID = (int)Edge_ID.size();
        Edge_ID.emplace_back(from, (int)g[from].size());

        g[from].push_back(f);
        g[to].push_back(t);
        
        return Now_Edge_ID;
    }

    bool bfs(int s, int t) {
        level.assign(n, -1);
        level[s] = 0;
        queue<int> que;
        que.push(s);

        while(que.size()) {
            int v = que.front();
            que.pop();

            for(auto e : g[v]) {
                if(e.cap > zero && level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }

        return level[t] != -1;
    }

    T dfs(int v, int t, T f) {
        if(v == t) return f;

        for(int& i = iter[v]; i < g[v].size(); i++) {
            Edge<T>& e = g[v][i];

            if(e.cap > zero && level[v] < level[e.to]) {
                T d = dfs(e.to, t, min(f, e.cap));

                if(d > zero) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }

        return zero;
    }

    T flow(int s, int t) {
        T f = zero;

        while(bfs(s, t)) {
            iter.assign(n, 0);
            T d;

            while((d = dfs(s, t, e)) > zero) f += d;
        }

        return f;
    }

    //sから見たときのmin_cutにおいてs側に所属するかどうかを全頂点に対して返すO(N)
    vector<bool> min_cut(int s) {
        vector<bool> visited(n, false);
        queue<int> que;
        visited[s] = true;
        que.push(s);
        while(!que.empty()) {
            int v = que.front();
            que.pop();
            for(auto e : g[v]) if(e.cap != 0 && !visited[e.to]) {
                visited[e.to] = true;
                que.push(e.to);
            }
        }
        return visited;
    }

    Edge<T> get_edge(int edge_id) {
        return g[Edge_ID[edge_id].first][Edge_ID[edge_id].second];
    }
};

//bipartite matching 
//TODO: make faster Dinic using dynamic tree structure and current edge structure
//燃やす埋めるref: https://qiita.com/ningenMe/items/69ed7ce43c9cd0a2de38

//add_edgeにintの返り値を持たせて、id = add_edge(), get_edge(id) = 元の辺 みたいにしたい。
//min_cut : 右側ならばtrueのvector<bool>を返す。
//cut[from] = true && cut[to] = false --- from->toの辺が最小カットで切られる辺の一つ

void solve() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    FOR(h) cin >> s[i];

    auto reach = [&]() -> bool {
        vector<vector<bool>> can(h-2, vector<bool>(w-2, true));
        rep(i, h-2) rep(j, w-2) {
            rep(px, 3) rep(py, 3) {
                if(s[i+px][j+py] == '#') can[i][j] = false;
            }
        }

        queue<pii> que;
        vector<vector<bool>> vis(h-2, vector<bool>(w-2, false));
        vis[0][0] = true;
        que.emplace(0, 0);

        while(!que.empty()) {
            int x, y;
            tie(x, y) = que.front();
            que.pop();

            rep(d, 8) {
                int nx = x + dx[d];
                int ny = y + dy[d];

                if(nx < 0 || ny < 0 || nx+2 >= h || ny+2 >= w) continue;
                if(!can[nx][ny] || vis[nx][ny]) continue;

                vis[nx][ny] = true;
                que.emplace(nx, ny);
            }
        }

        return vis[h-3][w-3];
    };

    if(!reach()) {
        cout << 0 << endl;
        return;
    }
    rep(i, h) rep(j, w) if(s[i][j] != '#') {
        if(i<3&&j<3) continue;
        if(i>=h-3&&j>=w-3) continue;

        s[i][j] = '#';
        if(!reach()) {
            cout << 1 << endl;
            return;
        }
        s[i][j] = '.';
    }
    cout << 2 << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0