結果

問題 No.697 池の数はいくつか
ユーザー rlangevinrlangevin
提出日時 2023-01-26 20:29:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 1,439 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 506,356 KB
最終ジャッジ日時 2023-09-09 19:20:20
合計ジャッジ時間 16,818 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
12,520 KB
testcase_01 AC 16 ms
8,020 KB
testcase_02 AC 16 ms
8,048 KB
testcase_03 AC 16 ms
8,036 KB
testcase_04 AC 17 ms
8,068 KB
testcase_05 AC 16 ms
8,048 KB
testcase_06 AC 16 ms
8,052 KB
testcase_07 AC 16 ms
8,048 KB
testcase_08 AC 16 ms
8,084 KB
testcase_09 AC 17 ms
8,060 KB
testcase_10 AC 17 ms
8,044 KB
testcase_11 AC 16 ms
8,056 KB
testcase_12 AC 16 ms
7,980 KB
testcase_13 AC 16 ms
8,088 KB
testcase_14 AC 17 ms
8,112 KB
testcase_15 AC 16 ms
8,224 KB
testcase_16 AC 16 ms
8,088 KB
testcase_17 AC 17 ms
8,160 KB
testcase_18 AC 16 ms
8,072 KB
testcase_19 AC 16 ms
8,004 KB
testcase_20 AC 16 ms
8,048 KB
testcase_21 AC 16 ms
8,048 KB
testcase_22 AC 17 ms
8,032 KB
testcase_23 AC 16 ms
8,124 KB
testcase_24 AC 1,378 ms
63,120 KB
testcase_25 AC 1,374 ms
63,172 KB
testcase_26 AC 1,378 ms
62,976 KB
testcase_27 AC 1,392 ms
63,056 KB
testcase_28 AC 1,417 ms
63,088 KB
testcase_29 MLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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