結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 00:52:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,856 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 11,276 KB
実行使用メモリ 29,432 KB
最終ジャッジ日時 2023-09-30 11:33:28
合計ジャッジ時間 15,762 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,808 KB
testcase_01 AC 16 ms
8,384 KB
testcase_02 AC 16 ms
8,420 KB
testcase_03 AC 16 ms
8,532 KB
testcase_04 AC 23 ms
9,024 KB
testcase_05 AC 17 ms
8,180 KB
testcase_06 AC 17 ms
8,120 KB
testcase_07 AC 72 ms
10,364 KB
testcase_08 AC 698 ms
20,004 KB
testcase_09 AC 318 ms
14,612 KB
testcase_10 AC 73 ms
10,688 KB
testcase_11 AC 235 ms
13,704 KB
testcase_12 AC 17 ms
8,440 KB
testcase_13 AC 118 ms
8,560 KB
testcase_14 AC 40 ms
9,596 KB
testcase_15 AC 64 ms
9,972 KB
testcase_16 AC 56 ms
9,724 KB
testcase_17 AC 533 ms
14,580 KB
testcase_18 AC 1,816 ms
19,928 KB
testcase_19 AC 2,152 ms
22,120 KB
testcase_20 AC 28 ms
9,464 KB
testcase_21 AC 2,944 ms
25,100 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