結果

問題 No.697 池の数はいくつか
ユーザー rlangevin
提出日時 2023-01-26 20:29:17
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
MLE  
実行時間 -
コード長 1,439 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 679 ms
コンパイル使用メモリ 20,824 KB
実行使用メモリ 523,380 KB
最終ジャッジ日時 2026-03-15 06:59:15
合計ジャッジ時間 52,152 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample MLE * 3
other AC * 24 TLE * 5 MLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

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

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
H, W = map(int, input().split())
U = UnionFind(H * W)
pre = [0] * W
cnt = 0
for i in range(H):
    L = list(map(int, input().split()))
    for j in range(W - 1):
        if L[j] and L[j + 1]:
            if U.is_same(i * W + j, i * W + j + 1):
                continue
            U.union(i * W + j, i * W + j + 1)
            cnt -= 1
    for j in range(W):
        if L[j]:
            cnt += 1
        if pre[j] and L[j]:
            if U.is_same((i - 1) * W + j, i * W + j):
                continue
            U.union((i - 1) * W + j, i * W + j)
            cnt -= 1
    L, pre = pre, L            
print(cnt)
0