結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  maspy | 
| 提出日時 | 2020-03-17 03:34:15 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 63 ms / 5,000 ms | 
| コード長 | 1,096 bytes | 
| コンパイル時間 | 100 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 13,056 KB | 
| 最終ジャッジ日時 | 2024-11-29 03:53:32 | 
| 合計ジャッジ時間 | 1,581 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#!/usr/bin/env python3.8
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
# %%
W, H = map(int, readline().split())
M = tuple(map(int, read().split()))
# %%
N = W * H
graph = [[] for _ in range(N)]
for i, j in itertools.product(range(W - 1), range(H)):
    x = W * j + i
    if M[x] == M[x + 1]:
        graph[x].append(x + 1)
        graph[x + 1].append(x)
for i, j in itertools.product(range(W), range(H - 1)):
    x = W * j + i
    if M[x] == M[x + W]:
        graph[x].append(x + W)
        graph[x + W].append(x)
# %%
visited = [False] * N
found = False
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    V = 1
    E = 0
    stack = [i]
    while stack:
        v = stack.pop()
        E += len(graph[v])
        for w in graph[v]:
            if visited[w]:
                continue
            V += 1
            visited[w] = True
            stack.append(w)
    E //= 2
    if V <= E:
        found = True
        break
print('possible' if found else 'impossible')
            
            
            
        