結果

問題 No.421 しろくろチョコレート
ユーザー tonnnura172tonnnura172
提出日時 2020-05-20 05:28:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 3,399 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2024-09-23 07:26:18
合計ジャッジ時間 5,604 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
11,520 KB
testcase_01 AC 63 ms
13,568 KB
testcase_02 AC 50 ms
13,184 KB
testcase_03 AC 38 ms
11,776 KB
testcase_04 AC 46 ms
12,544 KB
testcase_05 AC 42 ms
11,904 KB
testcase_06 AC 41 ms
11,520 KB
testcase_07 AC 51 ms
12,672 KB
testcase_08 AC 49 ms
13,440 KB
testcase_09 AC 40 ms
11,648 KB
testcase_10 AC 42 ms
11,776 KB
testcase_11 AC 56 ms
12,672 KB
testcase_12 AC 35 ms
11,392 KB
testcase_13 AC 36 ms
11,392 KB
testcase_14 AC 35 ms
11,392 KB
testcase_15 AC 35 ms
11,392 KB
testcase_16 AC 80 ms
14,208 KB
testcase_17 AC 85 ms
14,208 KB
testcase_18 AC 78 ms
14,336 KB
testcase_19 AC 36 ms
11,392 KB
testcase_20 AC 67 ms
13,312 KB
testcase_21 AC 72 ms
13,824 KB
testcase_22 AC 56 ms
12,544 KB
testcase_23 AC 36 ms
11,392 KB
testcase_24 AC 35 ms
11,392 KB
testcase_25 AC 36 ms
11,520 KB
testcase_26 AC 36 ms
11,520 KB
testcase_27 AC 35 ms
11,392 KB
testcase_28 AC 54 ms
13,952 KB
testcase_29 AC 58 ms
13,952 KB
testcase_30 AC 77 ms
14,080 KB
testcase_31 AC 63 ms
14,848 KB
testcase_32 AC 66 ms
14,080 KB
testcase_33 AC 76 ms
14,080 KB
testcase_34 AC 48 ms
13,696 KB
testcase_35 AC 50 ms
13,696 KB
testcase_36 AC 54 ms
12,928 KB
testcase_37 AC 84 ms
14,080 KB
testcase_38 AC 96 ms
14,464 KB
testcase_39 AC 50 ms
13,056 KB
testcase_40 AC 61 ms
12,672 KB
testcase_41 AC 42 ms
11,904 KB
testcase_42 AC 41 ms
11,904 KB
testcase_43 AC 53 ms
13,056 KB
testcase_44 AC 78 ms
13,312 KB
testcase_45 AC 44 ms
12,416 KB
testcase_46 AC 43 ms
12,160 KB
testcase_47 AC 60 ms
13,312 KB
testcase_48 AC 71 ms
14,080 KB
testcase_49 AC 54 ms
13,184 KB
testcase_50 AC 46 ms
12,672 KB
testcase_51 AC 80 ms
13,440 KB
testcase_52 AC 54 ms
12,544 KB
testcase_53 AC 44 ms
12,288 KB
testcase_54 AC 50 ms
13,568 KB
testcase_55 AC 41 ms
11,776 KB
testcase_56 AC 39 ms
11,904 KB
testcase_57 AC 43 ms
12,032 KB
testcase_58 AC 60 ms
13,312 KB
testcase_59 AC 45 ms
12,160 KB
testcase_60 AC 103 ms
14,464 KB
testcase_61 AC 104 ms
14,464 KB
testcase_62 AC 35 ms
11,520 KB
testcase_63 AC 35 ms
11,392 KB
testcase_64 AC 45 ms
13,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from math import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


class HopcroftKarp:
    def __init__(self, N0, N1):
        self.N0 = N0
        self.N1 = N1
        self.N = N = 2+N0+N1
        self.G = [[] for i in range(N)]
        for i in range(N0):
            forward = [2+i, 1, None]
            forward[2] = backward = [0, 0, forward]
            self.G[0].append(forward)
            self.G[2+i].append(backward)
        self.backwards = bs = []
        for i in range(N1):
            forward = [1, 1, None]
            forward[2] = backward = [2+N0+i, 0, forward]
            bs.append(backward)
            self.G[2+N0+i].append(forward)
            self.G[1].append(backward)
    def add_edge(self, fr, to):
        #assert 0 <= fr < self.N0
        #assert 0 <= to < self.N1
        v0 = 2 + fr
        v1 = 2 + self.N0 + to
        forward = [v1, 1, None]
        forward[2] = backward = [v0, 0, forward]
        self.G[v0].append(forward)
        self.G[v1].append(backward)
    def bfs(self):
        G = self.G
        level = [None]*self.N
        deq = deque([0])
        level[0] = 0
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        self.level = level
        return level[1] is not None
    def dfs(self, v, t):
        if v == t:
            return 1
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w] and self.dfs(w, t):
                e[1] = 0
                rev[1] = 1
                return 1
        return 0
    def flow(self):
        flow = 0
        G = self.G
        bfs = self.bfs; dfs = self.dfs
        while bfs():
            *self.it, = map(iter, G)
            while dfs(0, 1):
                flow += 1
        return flow
    def matching(self):
        return [cap for _, cap, _ in self.backwards]
  

H, W = MAP()
S = [input() for _ in range(H)]

U = H*W
hk = HopcroftKarp(U, U)

dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]

nb = 0
nw = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "b":
            nb += 1
        elif S[i][j] == "w":
            nw += 1

        if (i+j)%2 == 0 and S[i][j] == "w":
            for k in range(4):
                ny = i+dy[k]
                nx = j+dx[k]
                if 0 <= ny <= H-1 and 0 <= nx <= W-1 and S[ny][nx] == "b":
                    hk.add_edge(i*W+j, ny*W+nx)

ans = 0
f = hk.flow()
ans += 100*f
nb, nw = nb-f, nw-f
m = min(nb, nw)
ans += 10*m
nb, nw = nb-m, nw-m
ans += max(nb, nw)

print(ans)
0