結果

問題 No.13 囲みたい!
ユーザー maspymaspy
提出日時 2020-03-17 03:34:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,096 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 10,760 KB
最終ジャッジ日時 2023-08-19 11:31:04
合計ジャッジ時間 1,426 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,356 KB
testcase_01 AC 14 ms
8,180 KB
testcase_02 AC 14 ms
8,288 KB
testcase_03 AC 28 ms
9,852 KB
testcase_04 AC 38 ms
10,760 KB
testcase_05 AC 33 ms
9,656 KB
testcase_06 AC 25 ms
9,344 KB
testcase_07 AC 33 ms
9,292 KB
testcase_08 AC 31 ms
9,132 KB
testcase_09 AC 33 ms
9,188 KB
testcase_10 AC 17 ms
8,504 KB
testcase_11 AC 27 ms
8,964 KB
testcase_12 AC 15 ms
8,372 KB
testcase_13 AC 20 ms
8,708 KB
testcase_14 AC 20 ms
8,644 KB
testcase_15 AC 14 ms
8,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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')
0