結果

問題 No.421 しろくろチョコレート
ユーザー Navier_Boltzmann
提出日時 2023-06-17 14:07:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,560 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,208 KB
最終ジャッジ日時 2024-06-25 04:48:05
合計ジャッジ時間 6,860 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline
class DSU:
def __init__(self, n):
self._n = n
self.parent_or_size = [-1] * n
self.member = [[i] for i in range(n)]
def merge(self, a, b):
assert 0 <= a < self._n
assert 0 <= b < self._n
x, y = self.leader(a), self.leader(b)
if x == y: return x
if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
self.parent_or_size[x] += self.parent_or_size[y]
for tmp in self.member[y]:
self.member[x].append(tmp)
self.parent_or_size[y] = x
return x
def members(self,a):
return self.member[self.leader(a)]
def same(self, a, b):
assert 0 <= a < self._n
assert 0 <= b < self._n
return self.leader(a) == self.leader(b)
def leader(self, a):
assert 0 <= a < self._n
if self.parent_or_size[a] < 0: return a
self.parent_or_size[a] = self.leader(self.parent_or_size[a])
return self.parent_or_size[a]
def size(self, a):
assert 0 <= a < self._n
return -self.parent_or_size[self.leader(a)]
def groups(self):
leader_buf = [self.leader(i) for i in range(self._n)]
result = [[] for _ in range(self._n)]
for i in range(self._n): result[leader_buf[i]].append(i)
return [r for r in result if r != []]
N,M = map(int,input().split())
S = [list(input())[:-1] for _ in range(N)]
H,W = N,M
if H>W:
H,W = W,H
S = list(zip(*S))
def nb(x,y):
tmp = []
if x+1<H:
tmp.append((x+1,y))
if x-1>=0:
tmp.append((x-1,y))
if y+1<W:
tmp.append((x,y+1))
if y-1>=0:
tmp.append((x,y-1))
return tmp
D = DSU(N*M)
for i in range(H):
for j in range(W):
if S[i][j]=='.':
continue
for ix,iy in nb(i,j):
if S[ix][iy]!='.':
D.merge(W*ix + iy,W*i+j)
C = Counter([])
ans = 0
for g in D.groups():
i,j = divmod(g[0],W)
if S[i][j]=='.':
continue
b = 0
w = 0
for i in g:
ix,iy = divmod(i,W)
if S[ix][iy]=='w':
w += 1
else:
b += 1
if b==w:
ans += b*100
elif b>w:
ans += w*100
C['b'] += b-w
else:
b,w = w,b
ans += w*100
C['w'] += b-w
a,b = C['b'],C['w']
M = max(a,b)
m = min(a,b)
ans += 10*m
ans += (M-m)
print(ans)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0