結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,456 KB
testcase_01 AC 35 ms
53,544 KB
testcase_02 AC 37 ms
53,708 KB
testcase_03 AC 32 ms
53,548 KB
testcase_04 AC 38 ms
53,616 KB
testcase_05 AC 34 ms
53,420 KB
testcase_06 AC 34 ms
53,292 KB
testcase_07 AC 32 ms
52,952 KB
testcase_08 AC 35 ms
53,104 KB
testcase_09 AC 33 ms
53,764 KB
testcase_10 AC 35 ms
53,804 KB
testcase_11 AC 34 ms
52,884 KB
testcase_12 AC 33 ms
54,396 KB
testcase_13 AC 34 ms
53,324 KB
testcase_14 AC 35 ms
52,544 KB
testcase_15 AC 33 ms
52,964 KB
testcase_16 AC 31 ms
53,696 KB
testcase_17 AC 33 ms
52,596 KB
testcase_18 AC 33 ms
53,168 KB
testcase_19 AC 32 ms
53,420 KB
testcase_20 AC 33 ms
54,128 KB
testcase_21 AC 32 ms
52,516 KB
testcase_22 AC 34 ms
53,016 KB
testcase_23 AC 32 ms
54,076 KB
testcase_24 AC 436 ms
214,968 KB
testcase_25 AC 448 ms
214,852 KB
testcase_26 AC 422 ms
214,576 KB
testcase_27 AC 437 ms
214,876 KB
testcase_28 AC 503 ms
214,492 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=[]
S=set()
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:
            S.add(I)
            L.append(I)
            D[I]=X
            X+=1
        I+=1

Z=Union_Find(X)

I=0
for y in range(W):
    for x in range(H):
        if I in S:
            if (I+1)%W:
                if I+1 in S:
                    Q=D[I]
                    Z.union(Q,Q+1)
            if I+W in S:
                Z.union(D[I],D[I+W])                    
        I+=1
print(Z.group_count())
0