結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 00:57:48
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,900 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 265,712 KB
最終ジャッジ日時 2023-09-30 11:39:45
合計ジャッジ時間 13,112 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,256 KB
testcase_01 AC 71 ms
71,544 KB
testcase_02 AC 73 ms
71,564 KB
testcase_03 AC 73 ms
71,304 KB
testcase_04 AC 112 ms
77,768 KB
testcase_05 AC 74 ms
75,212 KB
testcase_06 AC 78 ms
75,364 KB
testcase_07 AC 222 ms
80,160 KB
testcase_08 AC 405 ms
85,848 KB
testcase_09 AC 298 ms
83,228 KB
testcase_10 AC 237 ms
80,760 KB
testcase_11 AC 357 ms
82,868 KB
testcase_12 AC 75 ms
75,124 KB
testcase_13 AC 108 ms
77,364 KB
testcase_14 AC 156 ms
79,768 KB
testcase_15 AC 182 ms
80,164 KB
testcase_16 AC 157 ms
79,724 KB
testcase_17 AC 270 ms
83,336 KB
testcase_18 AC 296 ms
87,544 KB
testcase_19 AC 311 ms
89,096 KB
testcase_20 AC 151 ms
79,548 KB
testcase_21 AC 288 ms
91,888 KB
testcase_22 AC 267 ms
91,856 KB
testcase_23 AC 184 ms
90,460 KB
testcase_24 AC 179 ms
90,800 KB
testcase_25 AC 214 ms
90,616 KB
testcase_26 AC 205 ms
90,868 KB
testcase_27 AC 1,067 ms
93,852 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)]:
                if find(toto)!=find(k):
                    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