結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 00:57:09
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,901 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 86,596 KB
実行使用メモリ 95,688 KB
最終ジャッジ日時 2023-09-30 11:38:59
合計ジャッジ時間 10,145 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 74 ms
70,968 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 159 ms
90,380 KB
testcase_33 RE -
testcase_34 AC 211 ms
82,296 KB
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

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]=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