結果

問題 No.307 最近色塗る問題多くない?
ユーザー rpy3cpprpy3cpp
提出日時 2015-11-28 01:55:23
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
TLE  
実行時間 -
コード長 3,029 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 11,244 KB
実行使用メモリ 25,296 KB
最終ジャッジ日時 2023-10-12 01:57:16
合計ジャッジ時間 10,523 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,548 KB
testcase_01 AC 17 ms
8,404 KB
testcase_02 AC 17 ms
8,472 KB
testcase_03 AC 17 ms
8,428 KB
testcase_04 AC 21 ms
9,024 KB
testcase_05 AC 17 ms
8,576 KB
testcase_06 AC 18 ms
8,660 KB
testcase_07 AC 54 ms
10,396 KB
testcase_08 AC 206 ms
19,048 KB
testcase_09 AC 94 ms
14,168 KB
testcase_10 AC 54 ms
10,668 KB
testcase_11 AC 136 ms
13,232 KB
testcase_12 AC 17 ms
8,452 KB
testcase_13 AC 155 ms
8,624 KB
testcase_14 AC 27 ms
9,436 KB
testcase_15 AC 33 ms
9,876 KB
testcase_16 AC 28 ms
9,444 KB
testcase_17 AC 87 ms
13,696 KB
testcase_18 AC 157 ms
17,720 KB
testcase_19 AC 178 ms
19,728 KB
testcase_20 AC 24 ms
9,308 KB
testcase_21 AC 193 ms
19,960 KB
testcase_22 AC 223 ms
20,796 KB
testcase_23 AC 221 ms
19,140 KB
testcase_24 AC 196 ms
19,660 KB
testcase_25 AC 375 ms
18,984 KB
testcase_26 AC 354 ms
19,736 KB
testcase_27 AC 787 ms
20,044 KB
testcase_28 TLE -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        self.num = n  # number of disjoint sets

    def union(self, x, y):
        self._link(self.find_set(x), self.find_set(y))

    def _link(self, x, y):
        if x == y:
            return
        self.num -= 1
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
        else:
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def find_set(self, x):
        xp = self.parent[x]
        if xp != x:
            self.parent[x] = self.find_set(xp)
        return self.parent[x]

class PaintSet(object):
    def __init__(self, color, H, W):
        self.color = color
        self.H = H
        self.W = W
        self.ds = DisjointSet(H * W)
        ds = self.ds
        pos = 0
        for r in range(H):
            for c in range(W):
                col = color[pos]
                if r+1 < H and color[pos + W] == col:
                    ds.union(pos, pos + W)
                if c+1 < W and color[pos + 1] == col:
                    ds.union(pos, pos + 1)
                pos += 1
        Es = [set() for i in range(W * H)]
        pos = 0
        for r in range(H):
            for c in range(W):
                col = color[pos]
                p = ds.find_set(pos)
                if r+1 < H and color[pos + W] != col:
                    p2 = ds.find_set(pos + W)
                    Es[p].add(p2)
                    Es[p2].add(p)
                if c+1 < W and color[pos + 1] != col:
                    p2 = ds.find_set(pos + 1)
                    Es[p].add(p2)
                    Es[p2].add(p)
                pos += 1
        self.Es = Es

    def paint(self, r, c, x):
        p = self.ds.find_set(r * self.W + c)
        if self.color[p] == x:
            return
        self.color[p] = x
        if self.ds.num == 1:
            return
        Es = self.Es
        ds = self.ds
        new_neighbour = set()
        for v in Es[p]:
            pv = ds.find_set(v)
            ds.union(p, pv)
            for u in Es[pv]:
                new_neighbour.add(ds.find_set(u))
            Es[pv] = set()
        if p in new_neighbour:
            new_neighbour.remove(p)
        newp = ds.find_set(p)
        Es[newp] = new_neighbour

    def pprint(self):
        W = self.W
        H = self.H
        color = self.color
        ds = self.ds
        pos = 0
        for r in range(H):
            row = []
            for c in range(W):
                col = color[ds.find_set(pos)]
                row.append(col)
                pos += 1
            print(*row)


if __name__ == "__main__":
    H, W = map(int, input().split())
    color = []
    for r in range(H):
        color.extend(list(map(int, input().split())))
    paintset = PaintSet(color, H, W)
    Q = int(input())
    for q in range(Q):
        r, c, x = map(int, input().split())
        paintset.paint(r-1, c-1, x)
    paintset.pprint()
0