結果

問題 No.307 最近色塗る問題多くない?
ユーザー rpy3cpprpy3cpp
提出日時 2015-11-28 02:21:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,635 ms / 4,000 ms
コード長 3,141 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 181,272 KB
最終ジャッジ日時 2023-08-14 03:59:01
合計ジャッジ時間 11,398 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,952 KB
testcase_01 AC 73 ms
71,308 KB
testcase_02 AC 74 ms
71,380 KB
testcase_03 AC 75 ms
71,148 KB
testcase_04 AC 109 ms
77,736 KB
testcase_05 AC 75 ms
71,156 KB
testcase_06 AC 76 ms
70,972 KB
testcase_07 AC 192 ms
80,080 KB
testcase_08 AC 360 ms
85,072 KB
testcase_09 AC 303 ms
83,040 KB
testcase_10 AC 219 ms
80,752 KB
testcase_11 AC 329 ms
82,232 KB
testcase_12 AC 74 ms
70,896 KB
testcase_13 AC 120 ms
77,092 KB
testcase_14 AC 144 ms
78,528 KB
testcase_15 AC 166 ms
78,992 KB
testcase_16 AC 144 ms
79,448 KB
testcase_17 AC 296 ms
83,468 KB
testcase_18 AC 282 ms
87,160 KB
testcase_19 AC 290 ms
87,356 KB
testcase_20 AC 131 ms
78,552 KB
testcase_21 AC 212 ms
90,568 KB
testcase_22 AC 246 ms
90,756 KB
testcase_23 AC 185 ms
90,128 KB
testcase_24 AC 176 ms
91,208 KB
testcase_25 AC 219 ms
89,888 KB
testcase_26 AC 195 ms
90,944 KB
testcase_27 AC 369 ms
91,632 KB
testcase_28 AC 2,635 ms
181,272 KB
testcase_29 AC 112 ms
77,276 KB
testcase_30 AC 255 ms
92,672 KB
testcase_31 AC 508 ms
92,256 KB
testcase_32 AC 193 ms
90,856 KB
testcase_33 AC 135 ms
79,572 KB
testcase_34 AC 191 ms
81,556 KB
testcase_35 AC 181 ms
81,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)
        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):
        if self.ds.num == 1:
            return
        p = self.ds.find_set(r * self.W + c)
        if self.color[p] == x:
            return
        self.color[p] = x
        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)
    if paintset.ds.num == 1:
        for r in range(H):
            print(' '.join([str(x)] * W))
    else:
        paintset.pprint()
0