結果

問題 No.697 池の数はいくつか
ユーザー 👑 KazunKazun
提出日時 2020-07-04 03:04:06
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,791 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,592 KB
実行使用メモリ 849,676 KB
最終ジャッジ日時 2024-05-04 07:01:05
合計ジャッジ時間 8,459 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,948 KB
testcase_01 AC 33 ms
52,792 KB
testcase_02 AC 32 ms
53,080 KB
testcase_03 AC 32 ms
53,548 KB
testcase_04 AC 33 ms
53,480 KB
testcase_05 AC 35 ms
52,592 KB
testcase_06 AC 33 ms
53,448 KB
testcase_07 AC 33 ms
53,408 KB
testcase_08 AC 34 ms
53,056 KB
testcase_09 AC 37 ms
53,112 KB
testcase_10 AC 32 ms
52,832 KB
testcase_11 AC 36 ms
53,444 KB
testcase_12 AC 34 ms
52,836 KB
testcase_13 AC 34 ms
52,408 KB
testcase_14 AC 32 ms
53,236 KB
testcase_15 AC 32 ms
53,136 KB
testcase_16 AC 31 ms
53,932 KB
testcase_17 AC 32 ms
53,816 KB
testcase_18 AC 33 ms
52,752 KB
testcase_19 AC 33 ms
53,420 KB
testcase_20 AC 34 ms
52,748 KB
testcase_21 AC 33 ms
53,076 KB
testcase_22 AC 33 ms
52,824 KB
testcase_23 AC 33 ms
52,708 KB
testcase_24 AC 370 ms
150,572 KB
testcase_25 AC 359 ms
150,768 KB
testcase_26 AC 362 ms
150,512 KB
testcase_27 AC 400 ms
151,340 KB
testcase_28 AC 364 ms
149,980 KB
testcase_29 MLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[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):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
#-----------------------------------------------------------
H,W=map(int,input().split())
I=0
A=[]
D={}
L=[]

X=0
for i in range(H):
    F=list(map(int,input().split()))
    A.append(F)
    for j in range(W):
        if F[j]==1:
            D[I]=X
            X+=1
        I+=1

Z=Union_Find(X)

I=0
for y in range(H):
    for x in range(W):
        if x!=W-1:
            if A[y][x]==A[y][x+1]==1:
                J=y*W+x
                Q=D[J]
                Z.union(Q,Q+1)

        if y!=H-1:
            if A[y][x]==A[y+1][x]==1:
                J=y*W+x
                Z.union(D[J],D[J+W])            
            
print(Z.group_count())
0