結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-12-06 21:28:31 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 896 bytes | 
| コンパイル時間 | 271 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 1,244,100 KB | 
| 最終ジャッジ日時 | 2024-11-25 16:43:45 | 
| 合計ジャッジ時間 | 69,812 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 MLE * 2 | 
| other | AC * 26 TLE * 3 MLE * 3 | 
ソースコード
import sys
import numpy as np
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix
input = sys.stdin.buffer.readline
def main():
    H, W = map(int, input().split())
    A = np.array([tuple(map(int, input().split())) for _ in range(H)], dtype=bool)
    F = np.arange(H * W).reshape((H, W))
    HC = A[1:] & A[:-1]
    WC = A[:, 1:] & A[:, :-1]
    ZW = np.zeros(W, dtype=bool).reshape(1, W)
    ZH = np.zeros(H, dtype=bool).reshape(H, 1)
    frm = np.hstack((F[np.concatenate([ZW, HC], 0)],
                     F[np.concatenate([ZH, WC], 1)]))
    to = np.hstack((F[np.concatenate([HC, ZW], 0)],
                    F[np.concatenate([WC, ZH], 1)]))
    L = len(frm)
    matr = csr_matrix(([1] * L, (frm, to)), shape=(H * W, H * W))
    p = connected_components(matr, return_labels=False)
    ans = p - np.count_nonzero(~A)
    return ans
print(main())