結果

問題 No.307 最近色塗る問題多くない?
ユーザー maspymaspy
提出日時 2020-03-22 12:06:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,085 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 38,132 KB
最終ジャッジ日時 2023-08-26 03:31:08
合計ジャッジ時間 15,830 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,556 KB
testcase_01 AC 16 ms
8,480 KB
testcase_02 AC 17 ms
8,552 KB
testcase_03 AC 16 ms
8,396 KB
testcase_04 AC 30 ms
9,372 KB
testcase_05 AC 17 ms
8,524 KB
testcase_06 AC 18 ms
8,468 KB
testcase_07 AC 117 ms
12,948 KB
testcase_08 AC 2,068 ms
38,132 KB
testcase_09 AC 904 ms
23,756 KB
testcase_10 AC 141 ms
13,444 KB
testcase_11 AC 648 ms
21,368 KB
testcase_12 AC 17 ms
8,268 KB
testcase_13 AC 75 ms
11,896 KB
testcase_14 AC 54 ms
10,484 KB
testcase_15 AC 93 ms
11,716 KB
testcase_16 AC 64 ms
10,668 KB
testcase_17 AC 957 ms
21,100 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 39 ms
10,320 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
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[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()
    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