結果

問題 No.421 しろくろチョコレート
ユーザー ふーらくたるふーらくたる
提出日時 2016-09-17 00:58:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 69,732 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:48:59
合計ジャッジ時間 2,223 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 7 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 4 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 6 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 6 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 4 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 6 ms
4,348 KB
testcase_61 AC 7 ms
4,348 KB
testcase_62 AC 1 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

const int MAX_N = 60;

struct BipartiteMatching {
    int V;  // 頂点数
    vector<vector<int> > graph; // グラフの隣接リスト表現
    vector<int> match;  // 各頂点ごとのマッチングの対応
    vector<bool> used;  // DFSの際に使用

    BipartiteMatching(int v) {
        V = v;
        graph = vector<vector<int> >(V);
        match = vector<int>(V);
        used = vector<bool>(V);
    }

    // uとvを連結にする
    void AddEdge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    bool DFS(int v) {
        used[v] = true;
        for (int i = 0; i < graph[v].size(); i++) {
            int u = graph[v][i], w = match[u];
            if (w < 0 || (!used[w] && DFS(w))) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
        return false;
    }

    // 二部マッチングを解く
    int Solve() {
        int res = 0;
        fill(match.begin(), match.end(), -1);
        for (int v = 0; v < V; v++) {
            if (match[v] < 0) {
                fill(used.begin(), used.end(), false);
                if (DFS(v)) {
                    res++;
                }
            }
        }
        return res;
    }
};

int n, m;

int conv_id(int row, int col) {
    return row * m + col;
}

string s[MAX_N];

int main() {
    cin >> n >> m;

    BipartiteMatching bm(n * m);

    int w_num = 0, b_num = 0;
    for (int r = 0; r < n; r++) {
        cin >> s[r];

        for (int c = 0; c < m; c++) {
            if (s[r][c] == 'w') w_num++;
            else if (s[r][c] == 'b') b_num++;
        }
    }

    for (int r = 0; r < n; r++) {
        for (int c = 0; c < m; c++) {
            if (s[r][c] == 'w') {
                if (r - 1 >= 0 && s[r - 1][c] == 'b') {
                    bm.AddEdge(conv_id(r, c), conv_id(r - 1, c));
                }
                if (r + 1 < n && s[r + 1][c] == 'b') {
                    bm.AddEdge(conv_id(r, c), conv_id(r + 1, c));
                }
                if (c - 1 >= 0 && s[r][c - 1] == 'b') {
                    bm.AddEdge(conv_id(r, c), conv_id(r, c - 1));
                }
                if (c + 1 < m && s[r][c + 1] == 'b') {
                    bm.AddEdge(conv_id(r, c), conv_id(r, c + 1));
                }
            }
        }
    }

    int matching = bm.Solve(),
        score3 = matching * 100,
        score2 = min(w_num - matching, b_num - matching) * 10,
        score1 = (w_num + b_num) - matching * 2 - score2 / 10 * 2;
    cout << score1 + score2 + score3 << endl;
    /*
    cout << "w: " << w_num << " " << "b: " << b_num << endl;
    cout << score1 << " " << score2 << " " << score3 << endl;
    */

    return 0;
}
0