結果

問題 No.421 しろくろチョコレート
ユーザー masa_aa
提出日時 2021-05-21 00:56:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 348 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 82,584 KB
実行使用メモリ 81,528 KB
最終ジャッジ日時 2024-09-23 07:31:40
合計ジャッジ時間 6,983 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

class BipartiteMatching:
    def __init__(self, n, m):
        self.size = n + m
        self._n = n
        self._g = [[] for _ in range(n)]
        self.matchA = [-1] * n
        self.matchB = [-1] * m

    def add_edge(self, u, v):
        self._g[u].append(v)

    def dfs(self, v):
        self.used[v] = True
        for u in self._g[v]:
            w = self.matchB[u]
            if w < 0 or not self.used[w] and self.dfs(w):
                self.matchA[v] = u
                self.matchB[u] = v
                return True
        return False

    def bipartite_matching(self):
        res = 0
        for v in range(self._n):
            if self.matchA[v] < 0:
                self.used = [0] * self._n
                if self.dfs(v):
                    res += 1
        return res


import sys
input = sys.stdin.readline

n, m = map(int, input().split())
s = [input() for _ in range(n)]
bm = BipartiteMatching(n * m // 2 + 1, n * m // 2)
b, w = 0, 0
for i in range(n):
    for j in range(m):
        if s[i][j] == ".":
            continue
        if s[i][j] == "b":
            b += 1
        else:
            w += 1

        if (i + j) % 2:
            if j < m - 1:
                if s[i][j + 1] != ".":
                    bm.add_edge((i * m + j + 1) // 2, (i * m + j) // 2)
            if i < n - 1:
                if s[i + 1][j] != ".":
                    bm.add_edge(((i + 1) * m + j) // 2, (i * m + j) // 2)
        else:
            if j < m - 1:
                if s[i][j + 1] != ".":
                    bm.add_edge((i * m + j) // 2, (i * m + j + 1) // 2)
            if i < n - 1:
                if s[i + 1][j] != ".":
                    bm.add_edge((i * m + j) // 2, ((i + 1) * m + j) // 2)

match = bm.bipartite_matching()
b -= match
w -= match
k = min(b, w)
print(100 * match + 10 * k + (b + w - k - k))
0