結果

問題 No.13 囲みたい!
ユーザー chocorusk
提出日時 2020-09-08 10:51:54
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,097 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-11-29 11:49:37
合計ジャッジ時間 1,310 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n=n
        self.par=[-1]*n
    def find(self, x):
        if self.par[x]<0:
            return x
        self.par[x]=self.find(self.par[x])
        return self.par[x]
    def unite(self, x, y):
        x=self.find(x)
        y=self.find(y)
        if x==y:
            return False
        if self.par[x]<self.par[y]:
            x, y=y, x
        self.par[y]+=self.par[x]
        self.par[x]=y
        return True
    def same(self, x, y):
        return self.find(x)==self.find(y)
    def size(self, x):
        return -self.par[self.find(x)]
w, h=map(int, input().split())
m=[]
for i in range(h):
    m.append(list(map(int, input().split())))
uf=UnionFind(h*w)
for i in range(h):
    for j in range(w-1):
        if m[i][j]==m[i][j+1]:
            if not uf.unite(i*w+j, i*w+j+1):
                print("possible")
                exit()
for i in range(h-1):
    for j in range(w):
        if m[i][j]==m[i+1][j]:
            if not uf.unite(i*w+j, (i+1)*w+j):
                print("possible")
                exit()
print("impossible")
0