結果

問題 No.13 囲みたい!
ユーザー convexineqconvexineq
提出日時 2020-11-24 05:55:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 1,499 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2023-10-01 00:47:17
合計ジャッジ時間 2,679 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,316 KB
testcase_01 AC 71 ms
71,444 KB
testcase_02 AC 71 ms
71,496 KB
testcase_03 AC 89 ms
76,824 KB
testcase_04 AC 95 ms
77,440 KB
testcase_05 AC 88 ms
76,836 KB
testcase_06 AC 92 ms
76,640 KB
testcase_07 AC 93 ms
76,828 KB
testcase_08 AC 78 ms
76,736 KB
testcase_09 AC 81 ms
76,708 KB
testcase_10 AC 79 ms
76,616 KB
testcase_11 AC 80 ms
76,532 KB
testcase_12 AC 73 ms
71,492 KB
testcase_13 AC 79 ms
76,372 KB
testcase_14 AC 76 ms
76,336 KB
testcase_15 AC 71 ms
71,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]
        

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

#a,b,c = map(int,readline().split())


w,h = map(int,input().split())
b = [list(map(int,input().split())) for _ in range(h)]
UF = UnionFind(w*h)
ok = 0
for i in range(h):
    for j in range(w):
        v = i*w+j
        if i+1 < h and b[i][j]==b[i+1][j]:
            if not UF.merge(v,v+w): ok = 1
        if j+1 < w and b[i][j]==b[i][j+1]:
            if not UF.merge(v,v+1): ok = 1

print("possible" if ok else "impossible")
0