結果

問題 No.13 囲みたい!
ユーザー chocoruskchocorusk
提出日時 2020-09-08 10:51:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,097 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 11,028 KB
実行使用メモリ 8,712 KB
最終ジャッジ日時 2023-08-19 17:34:17
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,316 KB
testcase_01 AC 16 ms
8,172 KB
testcase_02 AC 16 ms
8,052 KB
testcase_03 AC 26 ms
8,592 KB
testcase_04 AC 27 ms
8,408 KB
testcase_05 AC 26 ms
8,456 KB
testcase_06 AC 25 ms
8,436 KB
testcase_07 AC 24 ms
8,604 KB
testcase_08 AC 22 ms
8,712 KB
testcase_09 AC 23 ms
8,712 KB
testcase_10 AC 17 ms
8,188 KB
testcase_11 AC 21 ms
8,444 KB
testcase_12 AC 16 ms
8,208 KB
testcase_13 AC 18 ms
8,244 KB
testcase_14 AC 18 ms
8,048 KB
testcase_15 AC 16 ms
8,068 KB
権限があれば一括ダウンロードができます

ソースコード

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