結果

問題 No.421 しろくろチョコレート
ユーザー tonnnura172tonnnura172
提出日時 2020-05-20 05:28:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 3,399 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 12,396 KB
実行使用メモリ 14,188 KB
最終ジャッジ日時 2023-10-24 15:01:15
合計ジャッジ時間 5,058 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
10,832 KB
testcase_01 AC 66 ms
12,864 KB
testcase_02 AC 48 ms
12,552 KB
testcase_03 AC 36 ms
10,960 KB
testcase_04 AC 43 ms
11,960 KB
testcase_05 AC 41 ms
11,324 KB
testcase_06 AC 36 ms
10,928 KB
testcase_07 AC 49 ms
11,904 KB
testcase_08 AC 48 ms
12,712 KB
testcase_09 AC 39 ms
11,064 KB
testcase_10 AC 38 ms
11,188 KB
testcase_11 AC 52 ms
11,700 KB
testcase_12 AC 34 ms
10,776 KB
testcase_13 AC 34 ms
10,780 KB
testcase_14 AC 34 ms
10,780 KB
testcase_15 AC 33 ms
10,712 KB
testcase_16 AC 78 ms
13,496 KB
testcase_17 AC 86 ms
13,608 KB
testcase_18 AC 76 ms
13,660 KB
testcase_19 AC 33 ms
10,712 KB
testcase_20 AC 65 ms
12,808 KB
testcase_21 AC 68 ms
13,124 KB
testcase_22 AC 51 ms
11,700 KB
testcase_23 AC 33 ms
10,712 KB
testcase_24 AC 34 ms
10,776 KB
testcase_25 AC 33 ms
10,780 KB
testcase_26 AC 33 ms
10,780 KB
testcase_27 AC 34 ms
10,780 KB
testcase_28 AC 51 ms
13,256 KB
testcase_29 AC 54 ms
13,384 KB
testcase_30 AC 71 ms
13,344 KB
testcase_31 AC 59 ms
14,188 KB
testcase_32 AC 66 ms
13,352 KB
testcase_33 AC 74 ms
13,344 KB
testcase_34 AC 46 ms
12,916 KB
testcase_35 AC 46 ms
12,920 KB
testcase_36 AC 52 ms
12,296 KB
testcase_37 AC 82 ms
13,368 KB
testcase_38 AC 97 ms
13,608 KB
testcase_39 AC 49 ms
12,288 KB
testcase_40 AC 58 ms
11,964 KB
testcase_41 AC 39 ms
11,200 KB
testcase_42 AC 38 ms
11,148 KB
testcase_43 AC 48 ms
12,392 KB
testcase_44 AC 76 ms
12,808 KB
testcase_45 AC 41 ms
11,820 KB
testcase_46 AC 42 ms
11,596 KB
testcase_47 AC 58 ms
12,676 KB
testcase_48 AC 69 ms
13,360 KB
testcase_49 AC 51 ms
12,600 KB
testcase_50 AC 44 ms
11,996 KB
testcase_51 AC 77 ms
12,764 KB
testcase_52 AC 51 ms
11,700 KB
testcase_53 AC 41 ms
11,720 KB
testcase_54 AC 47 ms
12,720 KB
testcase_55 AC 38 ms
11,144 KB
testcase_56 AC 37 ms
11,124 KB
testcase_57 AC 41 ms
11,460 KB
testcase_58 AC 58 ms
12,720 KB
testcase_59 AC 45 ms
11,508 KB
testcase_60 AC 105 ms
13,924 KB
testcase_61 AC 107 ms
13,872 KB
testcase_62 AC 35 ms
10,804 KB
testcase_63 AC 35 ms
10,804 KB
testcase_64 AC 43 ms
12,228 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