結果

問題 No.307 最近色塗る問題多くない?
ユーザー maspymaspy
提出日時 2020-03-22 12:09:39
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 1,724 ms / 4,000 ms
コード長 1,943 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 11,096 KB
実行使用メモリ 21,680 KB
最終ジャッジ日時 2023-08-26 03:33:49
合計ジャッジ時間 10,504 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,328 KB
testcase_01 AC 16 ms
8,416 KB
testcase_02 AC 17 ms
8,320 KB
testcase_03 AC 16 ms
8,288 KB
testcase_04 AC 21 ms
8,652 KB
testcase_05 AC 17 ms
8,420 KB
testcase_06 AC 17 ms
8,432 KB
testcase_07 AC 48 ms
9,948 KB
testcase_08 AC 205 ms
16,764 KB
testcase_09 AC 106 ms
12,260 KB
testcase_10 AC 44 ms
9,748 KB
testcase_11 AC 100 ms
12,224 KB
testcase_12 AC 17 ms
8,364 KB
testcase_13 AC 73 ms
11,664 KB
testcase_14 AC 29 ms
8,948 KB
testcase_15 AC 35 ms
9,080 KB
testcase_16 AC 29 ms
8,824 KB
testcase_17 AC 88 ms
11,500 KB
testcase_18 AC 144 ms
13,712 KB
testcase_19 AC 163 ms
14,924 KB
testcase_20 AC 23 ms
8,764 KB
testcase_21 AC 149 ms
15,516 KB
testcase_22 AC 332 ms
15,732 KB
testcase_23 AC 172 ms
15,104 KB
testcase_24 AC 173 ms
15,308 KB
testcase_25 AC 247 ms
18,116 KB
testcase_26 AC 249 ms
18,352 KB
testcase_27 AC 1,724 ms
17,676 KB
testcase_28 AC 1,062 ms
20,224 KB
testcase_29 AC 57 ms
10,628 KB
testcase_30 AC 240 ms
20,832 KB
testcase_31 AC 823 ms
20,956 KB
testcase_32 AC 138 ms
20,792 KB
testcase_33 AC 556 ms
16,088 KB
testcase_34 AC 513 ms
21,664 KB
testcase_35 AC 541 ms
21,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, readline().split())
A = []
for _ in range(H):
    A += list(map(int, readline().split()))
Q = int(readline())
m = map(int, read().split())
RCX = zip(m, m, m)


class UnionFind:
    def __init__(self, N):
        self.root = list(range(N))
        self.size = [1] * (N)
        self.component_border = [[i] for i in range(N)]

    def find_root(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def merge(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return False
        assert A[x] == A[y]
        sx, sy = self.size[x], self.size[y]
        if sx > sy:
            sx, sy = sy, sx
            x, y = y, x
        self.root[y] = x
        self.size[x] += sy
        self.component_border[x] += self.component_border[y]
        self.component_border[y].clear()
        return True


N = H * W
uf = UnionFind(N)
find = uf.find_root
merge = uf.merge
for i in range(N):
    x, y = divmod(i, W)
    if y and A[i - 1] == A[i]:
        merge(i - 1, i)
    if x and A[i - W] == A[i]:
        merge(i - W, i)


def paint(i, c):
    i = find(i)
    if A[i] == c:
        return
    A[i] ^= 1
    border = uf.component_border[i].copy()
    uf.component_border[i].clear()
    for v in border:
        x, y = divmod(v, W)
        if 0 < y:
            merge(v - 1, v)
        if y < W - 1:
            merge(v + 1, v)
        if 0 < x:
            merge(v - W, v)
        if x < H - 1:
            merge(v + W, v)
    i = find(i)


for R, C, X in RCX:
    R -= 1
    C -= 1
    i = R * W + C
    paint(i, X)

A = [A[find(i)] for i in range(N)]
rows = (A[i: i + W] for i in range(0, N, W))
print('\n'.join(' '.join(map(str, row)) for row in rows))
0