結果

問題 No.2238 Rock and Hole
ユーザー umimelumimel
提出日時 2023-03-03 23:19:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 3,000 ms
コード長 4,101 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 194,784 KB
実行使用メモリ 45,476 KB
最終ジャッジ日時 2023-10-18 03:37:12
合計ジャッジ時間 3,929 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 6 ms
5,700 KB
testcase_11 AC 29 ms
19,384 KB
testcase_12 AC 29 ms
19,884 KB
testcase_13 AC 28 ms
18,936 KB
testcase_14 AC 16 ms
12,036 KB
testcase_15 AC 9 ms
7,444 KB
testcase_16 AC 79 ms
42,388 KB
testcase_17 AC 81 ms
45,476 KB
testcase_18 AC 40 ms
29,480 KB
testcase_19 AC 87 ms
21,608 KB
testcase_20 AC 56 ms
23,300 KB
testcase_21 AC 74 ms
22,364 KB
testcase_22 AC 34 ms
18,440 KB
testcase_23 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct Dinic{
    struct Edge{
        ll to;
        ll cap;
        ll rev;
    };

    vector<vector<Edge>> G;
    vector<ll> level;
    vector<ll> iter;
    ll N;

    Dinic(vector<vector<pll>> &G_){
        N = (ll)G_.size();
        G.resize(N);
        level.resize(N);
        iter.resize(N);

        rep(from, N){
            for(pll p : G_[from]){
                ll to = p.fi;
                ll cap = p.se;
                G[from].pb((Edge){to, cap, (ll)G[to].size()});
                G[to].pb((Edge){from, 0, (ll)G[from].size()-1});
            }
        }
    }

    void bfs(ll s, vector<vector<Edge>> &H){
        rep(i, N) level[i]=-1;

        queue<ll> Q;
        level[s] = 0;
        Q.push(s);
        while(!Q.empty()){
            ll v = Q.front();
            Q.pop();

            for(ll i=0; i<(ll)H[v].size(); i++){
                Edge &e = H[v][i];

                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    Q.push(e.to);
                }
            }
        }
    }

    ll dfs(ll v, ll t, ll f, vector<vector<Edge>> &H){
        if(v == t) return f;
        for(ll &i=iter[v]; i<(ll)H[v].size(); i++){
            Edge &e = H[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap), H);
                if(d > 0){
                    e.cap -= d;
                    H[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }

        return 0;
    }

    ll max_flow(ll s, ll t){
        vector<vector<Edge>> H(N);
        rep(from, N) for(Edge e : G[from]) H[from].pb(e);

        ll flow = 0;
        for(;;){
            bfs(s, H);
            if(level[t] < 0) return flow;
            rep(i, N) iter[i] = 0;
            ll f;
            while((f = dfs(s, t, INF, H)) > 0){
                flow += f;
            }
        }
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll h, w; cin >> h >> w;
    vector<string> s(h); rep(i, h) cin >> s[i];

    ll hall = 0, rock = 0;
    vector<vector<ll>> alloc(h, vector<ll>(w, INF));
    rep(i, h) rep(j, w) if(s[i][j]=='h') alloc[i][j]=hall++;
    rep(i, h) rep(j, w) if(s[i][j]=='r') alloc[i][j]=hall+(rock++);

    if(hall < rock){
        cout << "No" << endl;
        return 0;
    }

    vector<vector<ll>> line_hall(h), row_hall(w);
    rep(i, h) rep(j, w) if(s[i][j]=='h') line_hall[i].pb(j);
    rep(j, w) rep(i, h) if(s[i][j]=='h') row_hall[j].pb(i);

    vector<vector<pll>> G(hall+rock+2);
    ll S = hall+rock;
    ll T = hall+rock+1;
    rep(v, hall) G[S].pb({v, 1});
    rep(i, h) rep(j, w){
        if(s[i][j]=='r'){
            //上側とマッチ
            auto itr = lower_bound(all(row_hall[j]), i);
            if(itr!=row_hall[j].begin()){
                itr--;
                G[alloc[*itr][j]].pb({alloc[i][j], 1});
            }

            //下側とマッチ
            itr = lower_bound(all(row_hall[j]), i);
            if(itr!=row_hall[j].end()){
                G[alloc[*itr][j]].pb({alloc[i][j], 1});
            }

            //左
            itr = lower_bound(all(line_hall[i]), j);
            if(itr!=line_hall[i].begin()){
                itr--;
                G[alloc[i][*itr]].pb({alloc[i][j], 1});
            }

            //右
            itr = lower_bound(all(line_hall[i]), j);
            if(itr!=line_hall[i].end()){
                G[alloc[i][*itr]].pb({alloc[i][j], 1});
            }
        }
    }

    for(ll v=hall; v<S; v++) G[v].pb({T, 1});
    Dinic dinic(G);
    if(dinic.max_flow(S, T)==rock){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}
0