結果

問題 No.697 池の数はいくつか
ユーザー vwxyzvwxyz
提出日時 2023-11-28 15:34:26
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,826 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 439,980 KB
最終ジャッジ日時 2023-11-28 15:34:42
合計ジャッジ時間 14,086 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 41 ms
55,608 KB
testcase_03 AC 46 ms
55,608 KB
testcase_04 AC 42 ms
55,608 KB
testcase_05 AC 40 ms
55,608 KB
testcase_06 AC 40 ms
55,608 KB
testcase_07 AC 40 ms
55,608 KB
testcase_08 AC 41 ms
55,608 KB
testcase_09 AC 40 ms
55,608 KB
testcase_10 AC 42 ms
55,608 KB
testcase_11 AC 41 ms
55,608 KB
testcase_12 AC 40 ms
55,608 KB
testcase_13 AC 41 ms
55,608 KB
testcase_14 AC 41 ms
55,608 KB
testcase_15 AC 40 ms
55,608 KB
testcase_16 AC 41 ms
55,608 KB
testcase_17 AC 40 ms
55,608 KB
testcase_18 AC 40 ms
55,608 KB
testcase_19 AC 41 ms
55,608 KB
testcase_20 AC 42 ms
55,608 KB
testcase_21 AC 43 ms
55,608 KB
testcase_22 AC 41 ms
55,608 KB
testcase_23 AC 41 ms
55,608 KB
testcase_24 AC 191 ms
85,516 KB
testcase_25 AC 186 ms
85,516 KB
testcase_26 AC 187 ms
85,516 KB
testcase_27 AC 198 ms
85,516 KB
testcase_28 AC 225 ms
85,516 KB
testcase_29 MLE -
testcase_30 AC 1,342 ms
165,260 KB
testcase_31 MLE -
testcase_32 AC 1,077 ms
165,388 KB
testcase_33 AC 1,094 ms
165,388 KB
testcase_34 AC 1,143 ms
165,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

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

    def Find(self,x):
        stack=[]
        while self.parents[x]>=0:
            stack.append(x)
            x=self.parents[x]
        for y in stack:
            self.parents[y]=x
        return x

    def Union(self,x,y):
        x=self.Find(x)
        y=self.Find(y)
        if x==y:
            return
        if self.parents[x]>self.parents[y]:
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x

    def Size(self,x):
        return -self.parents[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Members(self,x):
        root = self.Find(x)
        return [i for i in range(self.n) if self.Find(i)==root]

    def Roots(self):
        return [i for i, x in enumerate(self.parents) if x<0]

    def Group_Count(self):
        return len(self.Roots())

    def All_Group_Members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.Find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.All_Group_Members().items())

H,W=map(int,readline().split())
A=[list(map(int,readline().split())) for h in range(H)]
ans=0
for h in range(H):
    for w in range(W):
        if A[h][w]:
            queue=[(h,w)]
            A[h][w]=0
            while queue:
                hh,ww=queue.pop()
                for dh,dw in ((0,1),(1,0),(0,-1),(-1,0)):
                    if 0<=hh+dh<H and 0<=ww+dw<W and A[hh+dh][ww+dw]:
                        A[hh+dh][ww+dw]=0
                        queue.append((hh+dh,ww+dw))
            ans+=1
print(ans)
0