結果
問題 |
No.307 最近色塗る問題多くない?
|
ユーザー |
![]() |
提出日時 | 2022-11-10 01:18:57 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,690 bytes |
コンパイル時間 | 361 ms |
コンパイル使用メモリ | 82,248 KB |
実行使用メモリ | 131,712 KB |
最終ジャッジ日時 | 2024-07-23 06:02:13 |
合計ジャッジ時間 | 22,424 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 TLE * 1 -- * 8 |
ソースコード
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])