結果

問題 No.2412 YOU Grow Bigger!
ユーザー k1suxuk1suxu
提出日時 2023-08-12 00:02:45
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,950 bytes
コンパイル時間 3,762 ms
コンパイル使用メモリ 263,568 KB
実行使用メモリ 5,464 KB
最終ジャッジ日時 2024-04-29 15:17:34
合計ジャッジ時間 4,174 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 WA -
testcase_23 AC 3 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

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() / 2;
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];

    Dinic<int> dinic(h*w*2);
    rep(i, h) rep(j, w) dinic.add_edge(i*w+j, i*w+j+h*w, 1);

    rep(i, h-2) rep(j, w-2) {
        bool can_from = true;
        rep(ip, 3) rep(jp, 3) if(s[i+ip][j+jp] == '#') {
            can_from = false;
        }
        if(!can_from) continue;

        rep(d, 8) {
            int ni = i + dx[d];
            int nj = j + dy[d];

            if(ni < 0 || nj < 0 || ni+2 >= h || nj+2 >= w) continue;
            bool flag = true;
            rep(ip, 3) rep(jp, 3) if(s[ni+ip][nj+jp] == '#') flag = false;
            if(!flag) continue;
            
            dinic.add_edge(i*w+j+h*w, ni*w+nj, INF);
            // printf("(%lld,%lld)->(%lld,%lld)\n", i, j, ni, nj);
        }
    }
    cout << dinic.flow(h*w, (h-3)*w+(w-3)) << endl;
}

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