結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 00:52:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,856 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 95,652 KB
最終ジャッジ日時 2023-09-30 11:34:10
合計ジャッジ時間 12,652 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,324 KB
testcase_01 AC 72 ms
71,264 KB
testcase_02 AC 75 ms
71,296 KB
testcase_03 AC 72 ms
71,492 KB
testcase_04 AC 120 ms
78,572 KB
testcase_05 AC 75 ms
75,236 KB
testcase_06 AC 78 ms
75,152 KB
testcase_07 AC 226 ms
80,356 KB
testcase_08 AC 487 ms
86,328 KB
testcase_09 AC 386 ms
83,736 KB
testcase_10 AC 222 ms
81,156 KB
testcase_11 AC 320 ms
83,528 KB
testcase_12 AC 76 ms
75,348 KB
testcase_13 AC 117 ms
77,524 KB
testcase_14 AC 164 ms
79,368 KB
testcase_15 AC 190 ms
80,412 KB
testcase_16 AC 163 ms
80,052 KB
testcase_17 AC 384 ms
83,976 KB
testcase_18 AC 758 ms
88,436 KB
testcase_19 AC 931 ms
89,856 KB
testcase_20 AC 153 ms
79,632 KB
testcase_21 AC 959 ms
92,860 KB
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 #

import sys
input = sys.stdin.readline

H,W=map(int,input().split())
MAP=[list(map(int,input().split())) for i in range(H)]

# UnionFind

n=H*W

Group = [i for i in range(n+1)] # グループ分け
Nodes = [1]*(n+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

ANS=[-1]*(H*W)

for i in range(H):
    for j in range(W):
        if i+1<H and MAP[i][j]==MAP[i+1][j]:
            Union(i*W+j,(i+1)*W+j)

        if j+1<W and MAP[i][j]==MAP[i][j+1]:
            Union(i*W+j,i*W+j+1)

for i in range(H):
    for j in range(W):
        ANS[find(i*W+j)]=MAP[i][j]

E=[set() for i in range(H*W)]

for i in range(H):
    for j in range(W):
        if i+1<H and MAP[i][j]!=MAP[i+1][j]:
            x=find(i*W+j)
            y=find((i+1)*W+j)
            E[x].add(y)
            E[y].add(x)

        if j+1<W and MAP[i][j]!=MAP[i][j+1]:
            x=find(i*W+j)
            y=find(i*W+j+1)
            E[x].add(y)
            E[y].add(x)

Q=int(input())

for i in range(Q):
    x,y,c=map(int,input().split())
    x-=1
    y-=1

    k=find(x*W+y)

    if ANS[k]==c:
        continue

    else:
        ANS[k]^=1

        X=set()

        for to in E[k]:

            for toto in E[find(to)]:
                X.add(find(toto))

            Union(to,k)

        k=find(x*W+y)

        E[k]=X

LANS=[[0]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        k=find(i*W+j)
        LANS[i][j]=ANS[k]

for i in range(H):
    print(*LANS[i])
0