結果

問題 No.697 池の数はいくつか
ユーザー vwxyzvwxyz
提出日時 2023-11-28 15:28:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,792 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 87,364 KB
最終ジャッジ日時 2023-11-28 15:28:58
合計ジャッジ時間 15,567 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,240 KB
testcase_01 AC 28 ms
10,112 KB
testcase_02 AC 29 ms
10,112 KB
testcase_03 AC 29 ms
10,112 KB
testcase_04 AC 28 ms
10,112 KB
testcase_05 AC 30 ms
10,112 KB
testcase_06 AC 28 ms
10,112 KB
testcase_07 AC 28 ms
10,112 KB
testcase_08 AC 29 ms
10,112 KB
testcase_09 AC 30 ms
10,112 KB
testcase_10 AC 29 ms
10,112 KB
testcase_11 AC 31 ms
10,112 KB
testcase_12 AC 28 ms
10,112 KB
testcase_13 AC 28 ms
10,112 KB
testcase_14 AC 29 ms
10,112 KB
testcase_15 AC 28 ms
10,112 KB
testcase_16 AC 28 ms
10,112 KB
testcase_17 AC 28 ms
10,112 KB
testcase_18 AC 31 ms
10,112 KB
testcase_19 AC 28 ms
10,112 KB
testcase_20 AC 28 ms
10,112 KB
testcase_21 AC 27 ms
10,112 KB
testcase_22 AC 28 ms
10,112 KB
testcase_23 AC 28 ms
10,112 KB
testcase_24 AC 1,032 ms
44,268 KB
testcase_25 AC 1,017 ms
44,396 KB
testcase_26 AC 1,046 ms
44,396 KB
testcase_27 AC 1,037 ms
44,396 KB
testcase_28 AC 1,030 ms
44,396 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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()))
ans=0
UF=UnionFind(H*W)
for w in range(W-1):
    if A[w] and A[w+1]:
        UF.Union(w,w+1)
ans-=A.count(0)
for h in range(1,H):
    prev=A
    A=list(map(int,readline().split()))
    for w in range(W-1):
        if A[w] and A[w+1]:
            UF.Union(h*W+w,h*W+w+1)
    for w in range(W):
        if prev[w] and A[w]:
            UF.Union((h-1)*W+w,h*W+w)
    ans-=A.count(0)
ans+=UF.Group_Count()
print(ans)
0