結果

問題 No.13 囲みたい!
ユーザー rlangevinrlangevin
提出日時 2023-08-25 08:57:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 645 ms / 5,000 ms
コード長 1,691 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 86,988 KB
実行使用メモリ 71,864 KB
最終ジャッジ日時 2023-08-25 10:14:25
合計ジャッジ時間 6,643 ms
ジャッジサーバーID
(参考情報)
judge13 / judge000
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
61,948 KB
testcase_01 AC 64 ms
61,944 KB
testcase_02 AC 177 ms
70,228 KB
testcase_03 AC 95 ms
64,288 KB
testcase_04 AC 82 ms
63,128 KB
testcase_05 AC 571 ms
71,696 KB
testcase_06 AC 109 ms
64,784 KB
testcase_07 AC 645 ms
71,864 KB
testcase_08 AC 327 ms
71,740 KB
testcase_09 AC 158 ms
71,540 KB
testcase_10 AC 221 ms
71,592 KB
testcase_11 AC 297 ms
71,376 KB
testcase_12 AC 207 ms
71,120 KB
testcase_13 AC 281 ms
71,560 KB
testcase_14 AC 252 ms
71,432 KB
testcase_15 AC 190 ms
71,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    

W, H = map(int, input().split())
M = []
for i in range(H):
    M.append(list(map(int, input().split())))

for n in range(1005):
    U = UnionFind(10**4+5)
    for a in range(H):
        for b in range(W):
            if a != H - 1:
                if M[a][b] == M[a + 1][b] == n:
                    i1 = a * W + b
                    i2 = (a + 1) * W + b
                    if U.is_same(i1, i2):
                        print("possible")
                        exit()
                    U.union(i1, i2)
            if b != W - 1:
                if M[a][b] == M[a][b + 1] == n:
                    i1 = a * W + b
                    i2 = a * W + b + 1
                    if U.is_same(i1, i2):
                        print("possible")
                        exit()
                    U.union(i1, i2)

print("impossible")
0