結果

問題 No.307 最近色塗る問題多くない?
ユーザー rpy3cpprpy3cpp
提出日時 2015-11-28 00:24:02
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 629 ms / 4,000 ms
コード長 3,367 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 86,776 KB
実行使用メモリ 141,504 KB
最終ジャッジ日時 2023-08-14 03:54:38
合計ジャッジ時間 9,769 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,320 KB
testcase_01 AC 67 ms
71,296 KB
testcase_02 AC 70 ms
71,296 KB
testcase_03 AC 69 ms
71,432 KB
testcase_04 AC 122 ms
77,984 KB
testcase_05 AC 73 ms
71,312 KB
testcase_06 AC 75 ms
75,160 KB
testcase_07 AC 214 ms
81,132 KB
testcase_08 AC 373 ms
88,164 KB
testcase_09 AC 294 ms
84,268 KB
testcase_10 AC 205 ms
80,576 KB
testcase_11 AC 289 ms
83,640 KB
testcase_12 AC 68 ms
71,248 KB
testcase_13 AC 133 ms
83,112 KB
testcase_14 AC 127 ms
78,652 KB
testcase_15 AC 153 ms
78,984 KB
testcase_16 AC 134 ms
78,960 KB
testcase_17 AC 272 ms
83,356 KB
testcase_18 AC 296 ms
94,028 KB
testcase_19 AC 289 ms
94,616 KB
testcase_20 AC 126 ms
78,248 KB
testcase_21 AC 224 ms
99,936 KB
testcase_22 AC 231 ms
102,744 KB
testcase_23 AC 333 ms
115,268 KB
testcase_24 AC 313 ms
116,692 KB
testcase_25 AC 321 ms
113,360 KB
testcase_26 AC 441 ms
128,176 KB
testcase_27 AC 227 ms
93,716 KB
testcase_28 AC 629 ms
141,504 KB
testcase_29 AC 128 ms
82,528 KB
testcase_30 AC 407 ms
123,112 KB
testcase_31 AC 263 ms
97,704 KB
testcase_32 AC 179 ms
96,812 KB
testcase_33 AC 144 ms
81,448 KB
testcase_34 AC 197 ms
88,284 KB
testcase_35 AC 206 ms
88,264 KB
権限があれば一括ダウンロードができます

ソースコード

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)
        self._init2()

    def _init2(self):
        ds = self.ds
        color = self.color
        W = self.W
        H = self.H
        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):
        ds = self.ds
        p = ds.find_set(r * self.W + c)
        if self.color[p] == x:
            return
        self.color[p] = x
        self._update(p, x)

    def _update(self, 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)
            new_neighbour |= Es[pv]
            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)


def read_data():
    H, W = map(int, input().split())
    color = []
    for r in range(H):
        color.extend(list(map(int, input().split())))
    Q = int(input())
    query = []
    for q in range(Q):
        r, c, x = map(int, input().split())
        query.append((r-1, c-1, x))
    return H, W, color, Q, query


def solve(H, W, color, Q, query):
    paintset = PaintSet(color, H, W)
    for r, c, x in query:
        paintset.paint(r, c, x)
    paintset.pprint()

H, W, color, Q, query = read_data()
solve(H, W, color, Q, query)
0