結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,868 KB
testcase_01 AC 17 ms
8,468 KB
testcase_02 AC 15 ms
8,396 KB
testcase_03 AC 15 ms
8,464 KB
testcase_04 AC 24 ms
8,988 KB
testcase_05 AC 16 ms
8,468 KB
testcase_06 AC 15 ms
8,132 KB
testcase_07 AC 64 ms
11,080 KB
testcase_08 AC 696 ms
24,692 KB
testcase_09 AC 354 ms
16,088 KB
testcase_10 AC 85 ms
11,160 KB
testcase_11 AC 348 ms
15,376 KB
testcase_12 AC 15 ms
8,520 KB
testcase_13 AC 66 ms
11,736 KB
testcase_14 AC 33 ms
9,456 KB
testcase_15 AC 47 ms
10,076 KB
testcase_16 AC 32 ms
9,568 KB
testcase_17 AC 192 ms
14,440 KB
testcase_18 AC 402 ms
18,340 KB
testcase_19 AC 517 ms
20,668 KB
testcase_20 AC 28 ms
9,300 KB
testcase_21 AC 410 ms
21,224 KB
testcase_22 TLE -
testcase_23 AC 287 ms
20,628 KB
testcase_24 AC 295 ms
20,952 KB
testcase_25 AC 420 ms
23,456 KB
testcase_26 AC 377 ms
23,916 KB
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_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_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()
    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