結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-07 21:11:50 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 5,000 ms |
| コード長 | 988 bytes |
| コンパイル時間 | 184 ms |
| コンパイル使用メモリ | 82,216 KB |
| 実行使用メモリ | 77,056 KB |
| 最終ジャッジ日時 | 2024-11-29 05:11:58 |
| 合計ジャッジ時間 | 1,903 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
def solve(h, w, cells, mapping):
for i, c in enumerate(cells):
mapping[c[0]][c[1]] = i
n = len(cells)
dsu = [i for i in range(n)]
def root(x):
if x != dsu[x]:
dsu[x] = root(dsu[x])
return dsu[x]
dx = [0, 1]
dy = [1, 0]
for c in cells:
for i in range(2):
x, y = c[0] + dx[i], c[1] + dy[i]
if x < 0 or x >= h or y < 0 or y >= w or mapping[x][y] == -1:
continue
u = root(mapping[c[0]][c[1]])
v = root(mapping[x][y])
if u != v:
dsu[u] = v
else:
return True
for i, c in enumerate(cells):
mapping[c[0]][c[1]] = -1
return False
w, h = list(map(int, input().split()))
cells = [[] for i in range(1000)]
mapping = [None] * h
for i in range(h):
a = list(map(int, input().split()))
mapping[i] = [-1] * w
for j in range(w):
cells[a[j] - 1].append((i, j))
flag = False
for e in cells:
if solve(h, w, e, mapping):
flag = True
break
print('possible' if flag else 'impossible')