結果

問題 No.13 囲みたい!
ユーザー ytftytft
提出日時 2022-11-03 00:06:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 126 ms / 5,000 ms
コード長 1,329 bytes
コンパイル時間 1,238 ms
コンパイル使用メモリ 86,708 KB
実行使用メモリ 78,476 KB
最終ジャッジ日時 2023-09-24 10:56:42
合計ジャッジ時間 2,727 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,356 KB
testcase_01 AC 67 ms
71,340 KB
testcase_02 AC 68 ms
71,184 KB
testcase_03 AC 118 ms
77,940 KB
testcase_04 AC 126 ms
78,212 KB
testcase_05 AC 111 ms
77,924 KB
testcase_06 AC 109 ms
78,448 KB
testcase_07 AC 117 ms
78,476 KB
testcase_08 AC 83 ms
76,588 KB
testcase_09 AC 81 ms
76,824 KB
testcase_10 AC 78 ms
76,772 KB
testcase_11 AC 84 ms
76,532 KB
testcase_12 AC 71 ms
71,232 KB
testcase_13 AC 87 ms
76,644 KB
testcase_14 AC 85 ms
76,772 KB
testcase_15 AC 70 ms
71,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda:sys.stdin.readline().rstrip()
class unionFind:
    def __init__(self,N):
        self.N=N
        self.parent=[-1 for i in range(N)]
        self.size=[1 for i in range(N)]
    def find(self,x):
        path=[x]
        while self.parent[path[-1]]!=-1:
            path.append(self.parent[path[-1]])
        for i in path[:-1]:
            self.parent[i]=path[-1]
        return path[-1]
    def unite(self,x,y):
        roots=sorted([self.find(x),self.find(y)],key=lambda _:self.parent[_])
        if roots[0]!=roots[1]:
            self.parent[roots[0]]=roots[1]
            self.size[roots[1]]+=self.size[roots[0]]
W,H=map(int,input().split())
field=[list(map(lambda x:int(x)-1,input().split())) for i in range(H)]
u=unionFind(H*W)
islands=[set() for i in range(1000)]
connections=[0 for i in range(1000)]
count=[0 for i in range(1000)]
for i in range(H):
	for j in range(W):
		count[field[i][j]]+=1
		if j<W-1 and field[i][j]==field[i][j+1]:
			u.unite(i*W+j,i*W+j+1)
			connections[field[i][j]]+=1
		if i<H-1 and field[i][j]==field[i+1][j]:
			u.unite(i*W+j,i*W+j+W)
			connections[field[i][j]]+=1
for i in range(H):
	for j in range(W):
		islands[field[i][j]].add(u.find(i*W+j))
for i in range(1000):
	if count[i]-connections[i]-len(islands[i])<0:
		print('possible')
		sys.exit()
print('impossible')
0