結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,452 KB
testcase_01 AC 75 ms
71,412 KB
testcase_02 AC 75 ms
71,260 KB
testcase_03 AC 76 ms
71,260 KB
testcase_04 AC 114 ms
78,092 KB
testcase_05 AC 75 ms
75,188 KB
testcase_06 AC 80 ms
75,392 KB
testcase_07 AC 219 ms
80,960 KB
testcase_08 AC 352 ms
86,100 KB
testcase_09 AC 303 ms
83,520 KB
testcase_10 AC 238 ms
81,476 KB
testcase_11 AC 352 ms
83,260 KB
testcase_12 AC 75 ms
75,248 KB
testcase_13 AC 109 ms
77,244 KB
testcase_14 AC 157 ms
79,644 KB
testcase_15 AC 185 ms
80,576 KB
testcase_16 AC 159 ms
79,476 KB
testcase_17 AC 275 ms
83,416 KB
testcase_18 AC 298 ms
87,400 KB
testcase_19 AC 309 ms
89,204 KB
testcase_20 AC 151 ms
79,272 KB
testcase_21 AC 216 ms
90,860 KB
testcase_22 AC 258 ms
91,144 KB
testcase_23 AC 184 ms
90,500 KB
testcase_24 AC 197 ms
90,376 KB
testcase_25 AC 219 ms
90,092 KB
testcase_26 AC 220 ms
90,816 KB
testcase_27 AC 689 ms
92,456 KB
testcase_28 MLE -
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)

        Y=set()

        for x in X:
            if find(x)==k:
                continue
            else:
                Y.add(x)

        E[k]=Y

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