結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-01-03 00:18:22 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,158 bytes | 
| コンパイル時間 | 102 ms | 
| コンパイル使用メモリ | 12,928 KB | 
| 実行使用メモリ | 521,480 KB | 
| 最終ジャッジ日時 | 2024-11-25 16:46:34 | 
| 合計ジャッジ時間 | 51,857 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 MLE * 1 | 
| other | AC * 22 TLE * 5 MLE * 5 | 
ソースコード
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.count = 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 unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def size(self, x):
        return self.rank[x]
    def count(self):
        return self.count
h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
UF = UnionFind(h * w)
for i in range(h):
    for j in range(1, w):
        if a[i][j] == a[i][j - 1] == 1:
            UF.unite(i * w + j, i * w + j - 1)
for j in range(w):
    for i in range(1, h):
        if a[i][j] == a[i - 1][j] == 1:
            UF.unite(i * w + j, (i - 1) * w + j)
print(UF.count - sum(i.count(0) for i in a))