結果

問題 No.307 最近色塗る問題多くない?
ユーザー titiatitia
提出日時 2022-11-10 00:37:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,910 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 11,220 KB
実行使用メモリ 25,704 KB
最終ジャッジ日時 2023-09-30 11:17:39
合計ジャッジ時間 7,623 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,076 KB
testcase_01 AC 16 ms
8,092 KB
testcase_02 AC 16 ms
8,060 KB
testcase_03 AC 16 ms
8,172 KB
testcase_04 WA -
testcase_05 AC 16 ms
8,156 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 54 ms
10,620 KB
testcase_11 AC 117 ms
13,616 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 28 ms
9,404 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 312 ms
25,272 KB
testcase_28 WA -
testcase_29 AC 79 ms
8,448 KB
testcase_30 WA -
testcase_31 AC 369 ms
25,344 KB
testcase_32 AC 281 ms
25,544 KB
testcase_33 AC 219 ms
20,044 KB
testcase_34 AC 305 ms
20,000 KB
testcase_35 AC 303 ms
20,052 KB
権限があれば一括ダウンロードができます

ソースコード

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

        for to in E[k]:
            Union(to,k)

        X=set()
        k=find(x*W+y)

        for to in E[k]:
            if find(to)==find(k):
                continue
            else:
                X.add(to)

        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