結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
|
| 提出日時 | 2019-09-06 16:01:56 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,042 bytes |
| コンパイル時間 | 308 ms |
| コンパイル使用メモリ | 82,380 KB |
| 実行使用メモリ | 347,500 KB |
| 最終ジャッジ日時 | 2024-11-25 15:12:50 |
| 合計ジャッジ時間 | 18,459 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 MLE * 6 |
ソースコード
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################
class UnionFind:
def __init__(self, N):
self.rank = [0] * N
self.parent = [i for i in range(N)]
self.n = N
def find(self, x):
if self.parent[x] == x:
return x
else:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if (x == y):
return
if (self.rank[x] < self.rank[y]):
self.parent[x] = y
else:
self.parent[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def same(self, x, y):
return self.find(x) == self.find(y)
def delete(self, idx):
self.parent[idx] = -1
H, W = map(int, input().split())
uf = UnionFind(H*W)
pond = [ [0 for _ in range(W)] for _ in range(H) ]
idx = 0
for i in range(H):
A = list(map(int, input().split()))
tmp = []
for j in range(len(A)):
if A[j] == 0:
tmp.append(-1)
else:
tmp.append(idx)
idx += 1
pond[i] = tmp
#print(pond)
#exit()
idx = -1
for h in range(H):
for w in range(W):
idx += 1
if pond[h][w] == -1:
# 対象外のは壊す
uf.delete(idx)
continue
if h > 0:
if pond[h-1][w] > -1:
uf.unite(pond[h-1][w], pond[h][w])
if w > 0:
if pond[h][w-1] > -1:
uf.unite(pond[h][w-1], pond[h][w])
if w < W-1:
if pond[h][w+1] > -1:
uf.unite(pond[h][w+1], pond[h][w])
if h < H-1:
if pond[h+1][w] > -1:
uf.unite(pond[h+1][w], pond[h][w])
#print(uf.parent)
count = 0
for i in range(H*W):
if uf.parent == -1:
continue
if uf.find(i) == i:
count += 1
if count == H*W:
print(0 if pond[0][0] == 0 else 1)
else:
print(count)