結果

問題 No.1954 CHECKER×CHECKER(2)
ユーザー gr1msl3ygr1msl3y
提出日時 2022-05-25 23:14:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 74 ms / 1,000 ms
コード長 1,539 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 81,588 KB
実行使用メモリ 73,464 KB
最終ジャッジ日時 2023-10-20 19:15:46
合計ジャッジ時間 4,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 34 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 34 ms
53,460 KB
testcase_06 AC 34 ms
53,460 KB
testcase_07 AC 35 ms
53,460 KB
testcase_08 AC 33 ms
53,460 KB
testcase_09 AC 36 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 37 ms
53,460 KB
testcase_13 AC 74 ms
72,968 KB
testcase_14 AC 36 ms
53,468 KB
testcase_15 AC 36 ms
53,468 KB
testcase_16 AC 39 ms
53,468 KB
testcase_17 AC 38 ms
53,468 KB
testcase_18 AC 37 ms
53,468 KB
testcase_19 AC 39 ms
55,516 KB
testcase_20 AC 38 ms
55,516 KB
testcase_21 AC 59 ms
72,528 KB
testcase_22 AC 62 ms
73,016 KB
testcase_23 AC 38 ms
55,516 KB
testcase_24 AC 67 ms
72,372 KB
testcase_25 AC 62 ms
72,468 KB
testcase_26 AC 37 ms
53,468 KB
testcase_27 AC 61 ms
72,496 KB
testcase_28 AC 61 ms
72,336 KB
testcase_29 AC 34 ms
53,468 KB
testcase_30 AC 61 ms
73,464 KB
testcase_31 AC 35 ms
53,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1]*n
        self.rank = [0]*n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            return self.find(self.parents[x])

    def size(self, x):
        return -self.parents[self.find(x)]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    def root(self):
        return [i for i in range(self.n) if self.parents[i] < 0]


H, W = map(int, input().split())
S = [input() for _ in range(H)]
M = int(input())
A = [list(map(int, input().split())) for _ in range(M)]

X = [0]*H
Y = [0]*W
for i in range(H):
    for j in range(W):
        X[i] += int(S[i][j] == '#') << j
        Y[j] += int(S[i][j] == '#') << i

uf1 = UnionFind(H)
uf2 = UnionFind(W)
for i in range(H-1):
    v = X[i] ^ X[i+1]
    if v == (1 << W)-1:
        uf1.union(i, i+1)
    elif v:
        print('No')
        exit()
for i in range(W-1):
    v = Y[i] ^ Y[i+1]
    if v == (1 << H)-1:
        uf2.union(i, i+1)
    elif v:
        print('No')
        exit()

for t, n in A:
    if t == 1:
        if n < H:
            uf1.union(n-1, n)
    else:
        if n < W:
            uf2.union(n-1, n)

print('Yes' if uf1.size(0) == H and uf2.size(0) == W else 'No')
0