結果

問題 No.2884 Pieces on Squares
ユーザー Iroha_3856Iroha_3856
提出日時 2024-09-02 18:54:16
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,897 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 72,060 KB
最終ジャッジ日時 2024-09-06 21:42:00
合計ジャッジ時間 5,238 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#Union-Findライブラリ(ac-library python)
#https://github.com/not522/ac-library-python/blob/master/atcoder/dsu.py
import typing

class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = 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]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))
#ライブラリ終わり

H, W = map(int, input().split())
d = DSU(H+W);
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '@':
            d.merge(i, j+H)
res = d.groups()
K = len(res)
dpmin = [[10**9 for j in range(H+1)] for i in range(K+1)]
dpmax = [[-10**9 for j in range(H+1)] for i in range(K+1)]
dpmin[0][0] = 0
dpmax[0][0] = 0
for i in range(K):
    rcnt, ccnt = 0, 0
    for v in res[i]:
        if v < H:
            rcnt+=1
        else:
            ccnt+=1
    for j in range(H):
        if dpmin[i][j] != 10**9:
            dpmin[i+1][j+rcnt] = min(dpmin[i+1][j+rcnt], dpmin[i][j] + ccnt)
            dpmax[i+1][j+rcnt] = max(dpmax[i+1][j+rcnt], dpmax[i][j] + ccnt)
        dpmin[i+1][j] = min(dpmin[i+1][j], dpmin[i][j])
        dpmax[i+1][j] = max(dpmax[i+1][j], dpmax[i][j])
ans = 0
for i in range(H):
    if dpmin[K][i] != 10**9:
        ans = max(ans, i*(W-dpmin[K][i])+(H-i)*dpmin[K][i])
        ans = max(ans, i*(W-dpmax[K][i])+(H-i)*dpmax[K][i])
print(ans)
0