結果

問題 No.421 しろくろチョコレート
ユーザー legosuke
提出日時 2020-03-04 05:30:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 2,060 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 201,160 KB
最終ジャッジ日時 2025-01-09 04:21:36
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

class BipartiteMatching {
    bool dfs(int v) {
        used[v] = timestamp;
        for (auto& u : g[v]) {
            int w = match[u];
            if (!alive[u]) continue;
            if (w < 0 || (used[w] != timestamp && dfs(w))) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
        return false;
    }
    
public:
    vector<vector<int>> g;
    vector<int> match, alive, used;
    int timestamp;

    BipartiteMatching(int n) : g(n), match(n, -1), alive(n, 1), used(n, 0), timestamp(0) {}

    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }

    int bipartite_matching() {
        int res = 0;
        for (int i = 0; i < g.size(); ++i) {
            if (!alive[i] || ~match[i]) continue;
            ++timestamp;
            res += dfs(i);
        }
        return res;
    }

    void print() {
        for (int i = 0; i < g.size(); ++i) {
            if (i < match[i]) {
                cout << i << " - " << match[i] << endl;
            }
        }
    }
};

const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
int N, M;
string S[50];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M;
    REP (i, N) cin >> S[i];
    BipartiteMatching g(N * M);
    REP (i, N) REP (j, M) if ((i + j) % 2 == 0 && S[i][j] != '.') {
        REP (k, 4) {
            int ny = i + dy[k];
            int nx = j + dx[k];
            if (ny >= 0 && ny < N && nx >= 0 && nx < M && S[ny][nx] != '.') {
                g.add_edge(i * M + j, ny * M + nx);
            }
        }
    }
    int w = 0, b = 0;
    REP (i, N) REP (j, M) {
        w += (S[i][j] == 'w');
        b += (S[i][j] == 'b');
    }
    int num = g.bipartite_matching();
    w -= num, b -= num;
    cout << 100 * num + 10 * min(w, b) + max(w, b) - min(w, b) << endl;
}
0