結果

問題 No.697 池の数はいくつか
ユーザー H3PO4H3PO4
提出日時 2020-12-06 22:07:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 1,006 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 817,788 KB
最終ジャッジ日時 2023-08-16 23:51:22
合計ジャッジ時間 15,606 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 206 ms
42,672 KB
testcase_01 AC 216 ms
43,224 KB
testcase_02 AC 204 ms
43,036 KB
testcase_03 AC 203 ms
43,072 KB
testcase_04 AC 206 ms
43,184 KB
testcase_05 AC 208 ms
43,092 KB
testcase_06 AC 207 ms
43,128 KB
testcase_07 AC 203 ms
43,204 KB
testcase_08 AC 205 ms
42,784 KB
testcase_09 AC 203 ms
42,836 KB
testcase_10 AC 203 ms
43,200 KB
testcase_11 AC 203 ms
43,036 KB
testcase_12 AC 205 ms
42,784 KB
testcase_13 AC 207 ms
43,208 KB
testcase_14 AC 211 ms
43,268 KB
testcase_15 AC 203 ms
43,236 KB
testcase_16 AC 202 ms
42,844 KB
testcase_17 AC 203 ms
43,100 KB
testcase_18 AC 205 ms
43,204 KB
testcase_19 AC 202 ms
43,032 KB
testcase_20 AC 203 ms
43,160 KB
testcase_21 AC 201 ms
43,084 KB
testcase_22 AC 205 ms
42,780 KB
testcase_23 AC 203 ms
43,160 KB
testcase_24 AC 380 ms
82,300 KB
testcase_25 AC 376 ms
84,416 KB
testcase_26 AC 377 ms
84,492 KB
testcase_27 AC 372 ms
83,072 KB
testcase_28 AC 374 ms
84,468 KB
testcase_29 MLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
from sys import stdin
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix


def main():
    H, W = map(int, stdin.buffer.readline().split())
    A = (np.frombuffer(stdin.buffer.read(), dtype='S1')[::2] == b'1').reshape((H, W))
    F = np.arange(H * W, dtype='u4').reshape((H, W))
    ct_0 = np.count_nonzero(~A)

    HC = A[1:] & A[:-1]
    ZW = np.zeros(W, dtype=bool).reshape(1, W)
    frm1 = F[np.concatenate([ZW, HC], 0)]
    to1 = F[np.concatenate([HC, ZW], 0)]
    del HC, ZW

    WC = A[:, 1:] & A[:, :-1]
    del A
    ZH = np.zeros(H, dtype=bool).reshape(H, 1)
    frm2 = F[np.concatenate([ZH, WC], 1)]
    to2 = F[np.concatenate([WC, ZH], 1)]
    del WC, ZH
    del F

    frm = np.hstack((frm1, frm2))
    to = np.hstack((to1, to2))
    L = len(frm)
    matr = csr_matrix(([1] * L, (frm, to)), shape=(H * W, H * W))
    del frm, to
    p = connected_components(matr, return_labels=False)
    ans = p - ct_0
    return ans


print(main())
0