結果

問題 No.2238 Rock and Hole
ユーザー rniyarniya
提出日時 2023-03-03 23:08:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 3,000 ms
コード長 5,001 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 219,016 KB
実行使用メモリ 7,172 KB
最終ジャッジ日時 2023-10-18 03:26:34
合計ジャッジ時間 3,616 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 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 8 ms
4,876 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 8 ms
5,044 KB
testcase_14 AC 8 ms
5,388 KB
testcase_15 AC 7 ms
4,516 KB
testcase_16 AC 11 ms
7,172 KB
testcase_17 AC 9 ms
5,836 KB
testcase_18 AC 5 ms
4,616 KB
testcase_19 AC 14 ms
5,044 KB
testcase_20 AC 10 ms
4,968 KB
testcase_21 AC 10 ms
5,052 KB
testcase_22 AC 7 ms
4,532 KB
testcase_23 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) void(0)
#endif

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }

const int INF = (1 << 30) - 1;
const long long IINF = (1LL << 60) - 1;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MOD = 998244353;
// const int MOD = 1000000007;

#include <algorithm>
#include <cassert>
#include <queue>
#include <random>
#include <utility>
#include <vector>

struct BipartiteMatching {
    int U, V, t;
    bool solved;
    std::vector<std::vector<int>> G;
    std::vector<int> L, R, visited;

    BipartiteMatching(int U, int V) : U(U), V(V), t(0), solved(false), G(U), L(U, -1), R(V, -1), visited(U, -1) {}

    void add_edge(int u, int v) {
        assert(0 <= u && u < U);
        assert(0 <= v && v < V);
        G[u].emplace_back(v);
    }

    void shuffle() {
        static std::mt19937 mt;
        for (auto& v : G) std::shuffle(v.begin(), v.end(), mt);
    }

    int solve() {
        for (bool updated = true; std::exchange(updated, false); t++) {
            for (int i = 0; i < U; i++) {
                if (L[i] == -1) {
                    updated |= dfs(i);
                }
            }
        }
        solved = true;
        return U - std::count(L.begin(), L.end(), -1);
    }

    std::vector<std::pair<int, int>> max_matching() const {
        assert(solved);
        std::vector<std::pair<int, int>> res;
        for (int i = 0; i < U; i++) {
            if (~L[i]) {
                res.emplace_back(i, L[i]);
            }
        }
        return res;
    }

private:
    bool dfs(int u) {
        if (std::exchange(visited[u], t) == t) return false;
        for (int& v : G[u]) {
            if (R[v] == -1) {
                L[u] = v, R[v] = u;
                return true;
            }
        }
        for (int& v : G[u]) {
            if (dfs(R[v])) {
                L[u] = v, R[v] = u;
                return true;
            }
        }
        return false;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    cin >> S;

    int stone = 0, hole = 0;
    vector<vector<int>> idx(H, vector<int>(W, -1));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == 'r') idx[i][j] = stone++;
            if (S[i][j] == 'h') idx[i][j] = hole++;
        }
    }
    auto rotate = [&]() {
        vector<string> nS(W, string(H, '.'));
        vector<vector<int>> nidx(W, vector<int>(H));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                nS[j][H - 1 - i] = S[i][j];
                nidx[j][H - 1 - i] = idx[i][j];
            }
        }
        swap(H, W);
        swap(S, nS);
        swap(idx, nidx);
    };
    BipartiteMatching G(stone, hole);
    for (int k = 0; k < 4; k++) {
        for (int i = 0; i < H; i++) {
            for (int j = 0, pre = -1; j < W; j++) {
                if (S[i][j] == 'h')
                    pre = idx[i][j];
                else if (S[i][j] == 'r' and pre != -1)
                    G.add_edge(idx[i][j], pre);
            }
        }
        rotate();
    }

    cout << (G.solve() == stone ? "Yes" : "No") << '\n';
    return 0;
}
0