結果

問題 No.421 しろくろチョコレート
ユーザー Coki628Coki628
提出日時 2020-02-12 12:27:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 3,788 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 12,400 KB
実行使用メモリ 11,364 KB
最終ジャッジ日時 2023-10-24 14:59:44
合計ジャッジ時間 4,255 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,472 KB
testcase_01 AC 35 ms
11,048 KB
testcase_02 AC 32 ms
11,000 KB
testcase_03 AC 31 ms
10,508 KB
testcase_04 AC 32 ms
10,788 KB
testcase_05 AC 32 ms
10,596 KB
testcase_06 AC 31 ms
10,488 KB
testcase_07 AC 35 ms
10,740 KB
testcase_08 AC 34 ms
11,024 KB
testcase_09 AC 32 ms
10,528 KB
testcase_10 AC 31 ms
10,584 KB
testcase_11 AC 35 ms
10,776 KB
testcase_12 AC 30 ms
10,444 KB
testcase_13 AC 30 ms
10,444 KB
testcase_14 AC 30 ms
10,444 KB
testcase_15 AC 30 ms
10,444 KB
testcase_16 AC 39 ms
11,192 KB
testcase_17 AC 42 ms
11,248 KB
testcase_18 AC 45 ms
11,220 KB
testcase_19 AC 30 ms
10,444 KB
testcase_20 AC 37 ms
10,988 KB
testcase_21 AC 37 ms
11,084 KB
testcase_22 AC 34 ms
10,776 KB
testcase_23 AC 30 ms
10,444 KB
testcase_24 AC 30 ms
10,444 KB
testcase_25 AC 31 ms
10,444 KB
testcase_26 AC 31 ms
10,444 KB
testcase_27 AC 30 ms
10,444 KB
testcase_28 AC 37 ms
11,172 KB
testcase_29 AC 38 ms
11,188 KB
testcase_30 AC 36 ms
11,172 KB
testcase_31 AC 49 ms
11,364 KB
testcase_32 AC 36 ms
11,180 KB
testcase_33 AC 38 ms
11,176 KB
testcase_34 AC 33 ms
11,120 KB
testcase_35 AC 33 ms
11,120 KB
testcase_36 AC 33 ms
10,852 KB
testcase_37 AC 49 ms
11,176 KB
testcase_38 AC 60 ms
11,272 KB
testcase_39 AC 34 ms
10,932 KB
testcase_40 AC 40 ms
10,840 KB
testcase_41 AC 35 ms
10,564 KB
testcase_42 AC 33 ms
10,564 KB
testcase_43 AC 34 ms
10,908 KB
testcase_44 AC 44 ms
10,980 KB
testcase_45 AC 33 ms
10,740 KB
testcase_46 AC 32 ms
10,688 KB
testcase_47 AC 35 ms
11,000 KB
testcase_48 AC 76 ms
11,176 KB
testcase_49 AC 34 ms
11,016 KB
testcase_50 AC 33 ms
10,780 KB
testcase_51 AC 60 ms
10,960 KB
testcase_52 AC 33 ms
10,760 KB
testcase_53 AC 32 ms
10,728 KB
testcase_54 AC 33 ms
11,040 KB
testcase_55 AC 32 ms
10,568 KB
testcase_56 AC 31 ms
10,564 KB
testcase_57 AC 31 ms
10,652 KB
testcase_58 AC 35 ms
10,988 KB
testcase_59 AC 34 ms
10,648 KB
testcase_60 AC 66 ms
11,288 KB
testcase_61 AC 44 ms
11,296 KB
testcase_62 AC 31 ms
10,464 KB
testcase_63 AC 31 ms
10,468 KB
testcase_64 AC 34 ms
11,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

class BipartiteMatching:
    """
    XとYの二部グラフの最大マッチング X={0,1,2,...|X|-1} Y={0,1,2,...,|Y|-1}
    edges[x]: xとつながるYの頂点のset
    match1[x]: xとマッチングされたYの頂点
    match2[y]: yとマッチングされたXの頂点
    """

    def __init__(self, n, m):
        self.n = n
        self.m = m
        self.edges = [set() for _ in range(n)]
        self.match1 = [-1] * n
        self.match2 = [-1] * m
 
    def dfs(self, v, visited):
        """
        :param v: X側の未マッチングの頂点の1つ
        :param visited: 空のsetを渡す(外部からの呼び出し時)
        :return: 増大路が見つかればTrue
        """
        for u in self.edges[v]:
            if u in visited:
                continue
            visited.add(u)
            if self.match2[u] == -1 or self.dfs(self.match2[u], visited):
                self.match2[u] = v
                self.match1[v] = u
                return True
        return False
    
    def add(self, a, b):
        self.edges[a].add(b)

    def whois1(self, a):
        """ :param: グループ1の頂点 :return: ペアになるグループ2の頂点 """
        return self.match1[a]

    def whois2(self, a):
        """ :param: グループ2の頂点 :return: ペアになるグループ1の頂点 """
        return self.match2[a]

    def solve(self):
        # 増大路発見に成功したらTrue(=1)。合計することでマッチング数となる
        return sum(self.dfs(i, set()) for i in range(self.n))

def build_grid(H, W, intv, _type, space=True, padding=False):
    # 入力がスペース区切りかどうか
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    # 余白の有無
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

H, W = MAP()
grid = build_grid(H, W, '.', str, space=0, padding=1)

H += 2; W += 2
HW = H * W
bm = BipartiteMatching(HW, HW)
C = {'w': 0, 'b': 0}
for h in range(1, H-1):
    for w in range(1, W-1):
        if grid[h][w] != '.':
            # 白黒それぞれいくつ残っているかをカウントしておく
            C[grid[h][w]] += 1
            # 隣り合うチョコが残っていれば辺を張る
            if grid[h-1][w] != '.':
                if (h+w) % 2 == 0:
                    bm.add(h*W+w, (h-1)*W+w)
                else:
                    bm.add((h-1)*W+w, h*W+w)
            if grid[h][w-1] != '.':
                if (h+w) % 2 == 0:
                    bm.add(h*W+w, h*W+(w-1))
                else:
                    bm.add(h*W+(w-1), h*W+w)

# 隣り合うペア
res = bm.solve()
C['w'] -= res
C['b'] -= res
ans = res * 100
# 隣り合わないペア
mn = min(C['w'], C['b'])
C['w'] -= mn
C['b'] -= mn
ans += mn * 10
# 残り
ans += C['w'] + C['b']
print(ans)
0