結果

問題 No.307 最近色塗る問題多くない?
ユーザー maspymaspy
提出日時 2020-03-22 12:05:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,085 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 11,212 KB
実行使用メモリ 44,044 KB
最終ジャッジ日時 2023-08-26 03:30:10
合計ジャッジ時間 8,637 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
44,044 KB
testcase_01 WA -
testcase_02 AC 15 ms
8,232 KB
testcase_03 AC 14 ms
8,392 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 44 ms
12,476 KB
testcase_11 AC 90 ms
19,076 KB
testcase_12 WA -
testcase_13 RE -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 27 ms
9,932 KB
testcase_21 RE -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = [set([i]) for i in range(N)]
        self.component_border = [set([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[x] |= self.component[y]
        self.component_border[x] |= self.component_border[y]
        self.component[y].clear()
        self.component_border[x].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()
    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)
    uf.component_border[i] -= border


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