結果

問題 No.421 しろくろチョコレート
ユーザー roarisroaris
提出日時 2019-11-01 14:22:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,876 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 81,920 KB
最終ジャッジ日時 2023-10-13 00:45:51
合計ジャッジ時間 11,367 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 RE -
testcase_03 WA -
testcase_04 RE -
testcase_05 WA -
testcase_06 AC 101 ms
76,448 KB
testcase_07 WA -
testcase_08 RE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 94 ms
71,460 KB
testcase_13 AC 94 ms
71,812 KB
testcase_14 AC 94 ms
71,684 KB
testcase_15 RE -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 RE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 RE -
testcase_24 AC 92 ms
71,716 KB
testcase_25 AC 92 ms
71,688 KB
testcase_26 AC 94 ms
71,412 KB
testcase_27 AC 91 ms
71,528 KB
testcase_28 AC 126 ms
77,700 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 119 ms
77,648 KB
testcase_32 AC 126 ms
77,584 KB
testcase_33 WA -
testcase_34 RE -
testcase_35 RE -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 RE -
testcase_40 AC 117 ms
77,504 KB
testcase_41 AC 110 ms
77,308 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 120 ms
77,432 KB
testcase_45 RE -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 120 ms
77,484 KB
testcase_49 RE -
testcase_50 RE -
testcase_51 AC 123 ms
78,052 KB
testcase_52 WA -
testcase_53 RE -
testcase_54 RE -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 AC 95 ms
71,384 KB
testcase_63 AC 95 ms
71,624 KB
testcase_64 AC 106 ms
77,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class Unionfind:
    def __init__(self, N):
        self.par = [-1] * N
        self.rank = [1] * N
        
    def root(self, x):
        if self.par[x] < 0:
            return x
        
        self.par[x] = self.root(self.par[x])
        return self.par[x]
    
    def unite(self, x, y):
        rx, ry = self.root(x), self.root(y)
        
        if rx != ry:
            if self.rank[rx] >= self.rank[ry]:
                self.par[rx] += self.par[ry]
                self.par[ry] = rx
                
                if self.rank[rx] == self.rank[ry]:
                    self.rank[rx] += 1
            else:
                self.par[ry] += self.par[rx]
                self.par[rx] = ry
    
    def is_same(self, x, y):
        return self.root(x) == self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]
        
N, M = map(int, input().split())
S = [input() for _ in range(N)]
uf = Unionfind(N*M)

for i in range(N):
    for j in range(M):
            for ni, nj in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                if 0<=ni<N and 0<=nj<M and S[ni][nj] != '.':
                    uf.unite(M*i+j, M*ni+nj)

group = defaultdict(dict)

for i in range(N):
    for j in range(M):
        if S[i][j] == 'w':
            if 0 not in group[uf.root(M*i+j)]:
                group[uf.root(M*i+j)][0] = 1
            else:
                group[uf.root(M*i+j)][0] += 1
        elif S[i][j] == 'b':
            if 1 not in group[uf.root(M*i+j)]:
                group[uf.root(M*i+j)][1]= 1
            else:
                group[uf.root(M*i+j)][1] += 1

pair_sum = 0
w_amari = 0
b_amari = 0

for k in group:
    pair = min(group[k][0], group[k][1])
    pair_sum += pair
    w_amari += group[k][0]-pair
    b_amari += group[k][1]-pair

print(pair_sum*100+min(w_amari, b_amari)*10+abs(w_amari-b_amari))
0