結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 01:18:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,690 bytes
コンパイル時間 353 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 84,964 KB
最終ジャッジ日時 2023-09-30 12:02:31
合計ジャッジ時間 24,120 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,660 KB
testcase_01 AC 71 ms
71,464 KB
testcase_02 AC 69 ms
71,348 KB
testcase_03 AC 74 ms
71,444 KB
testcase_04 AC 134 ms
78,624 KB
testcase_05 AC 76 ms
75,472 KB
testcase_06 AC 86 ms
76,248 KB
testcase_07 AC 232 ms
79,736 KB
testcase_08 AC 682 ms
82,452 KB
testcase_09 AC 350 ms
81,100 KB
testcase_10 AC 393 ms
80,460 KB
testcase_11 AC 1,286 ms
80,848 KB
testcase_12 AC 75 ms
75,412 KB
testcase_13 AC 113 ms
77,732 KB
testcase_14 AC 172 ms
79,492 KB
testcase_15 AC 201 ms
80,444 KB
testcase_16 AC 178 ms
80,072 KB
testcase_17 AC 424 ms
81,160 KB
testcase_18 AC 868 ms
81,988 KB
testcase_19 AC 768 ms
82,260 KB
testcase_20 AC 147 ms
79,020 KB
testcase_21 AC 599 ms
81,480 KB
testcase_22 AC 448 ms
82,480 KB
testcase_23 AC 2,878 ms
81,244 KB
testcase_24 AC 1,417 ms
80,904 KB
testcase_25 AC 1,775 ms
81,548 KB
testcase_26 AC 3,495 ms
81,576 KB
testcase_27 TLE -
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]

Q=int(input())
for tests 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

        if Nodes[k]==H*W:
            continue

        U=[]

        for i in range(H):
            for j in range(W):
                if find(i*W+j)==k:
                    for z,w in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
                        if 0<=z<H and 0<=w<W and find(z*W+w)!=k:
                            U.append(find(z*W+w))

        for u in U:
            Union(u,k)

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