結果

問題 No.2946 Puyo
ユーザー 👑 rin204
提出日時 2024-10-26 00:39:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 82,260 KB
実行使用メモリ 128,560 KB
最終ジャッジ日時 2024-10-26 00:39:53
合計ジャッジ時間 16,019 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [-1] * n
        self.group_ = n

    def find(self, x):
        if self.par[x] < 0:
            return x
        lst = []
        while self.par[x] >= 0:
            lst.append(x)
            x = self.par[x]
        for y in lst:
            self.par[y] = x
        return x

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False

        if self.par[x] > self.par[y]:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        self.group_ -= 1
        return True

    def size(self, x):
        return -self.par[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    @property
    def group(self):
        return self.group_


h, w = map(int, input().split())
G = [input() for _ in range(h)]
UF = UnionFind(h * w)

for i in range(h):
    for j in range(w):
        if i > 0 and G[i][j] == G[i - 1][j]:
            UF.unite(i * w + j, (i - 1) * w + j)
        if j > 0 and G[i][j] == G[i][j - 1]:
            UF.unite(i * w + j, i * w + j - 1)

ans = [["."] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if UF.size(i * w + j) < 4:
            ans[i][j] = G[i][j]

for row in ans:
    print(*row, sep="")
0