結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2020-03-22 12:06:47 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,085 bytes |
| コンパイル時間 | 155 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 93,488 KB |
| 最終ジャッジ日時 | 2024-12-24 17:05:22 |
| 合計ジャッジ時間 | 79,670 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 TLE * 14 |
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W = map(int, readline().split())
A = []
for _ in range(H):
A += list(map(int, readline().split()))
Q = int(readline())
m = map(int, read().split())
RCX = zip(m, m, m)
class UnionFind:
def __init__(self, N):
self.root = list(range(N))
self.size = [1] * (N)
self.component = [set([i]) for i in range(N)]
self.component_border = [set([i]) for i in range(N)]
def find_root(self, x):
root = self.root
while root[x] != x:
root[x] = root[root[x]]
x = root[x]
return x
def merge(self, x, y):
x = self.find_root(x)
y = self.find_root(y)
if x == y:
return False
assert A[x] == A[y]
sx, sy = self.size[x], self.size[y]
if sx > sy:
sx, sy = sy, sx
x, y = y, x
self.root[y] = x
self.size[x] += sy
self.component[x] |= self.component[y]
self.component_border[x] |= self.component_border[y]
self.component[y].clear()
self.component_border[y].clear()
return True
N = H * W
uf = UnionFind(N)
find = uf.find_root
merge = uf.merge
for i in range(N):
x, y = divmod(i, W)
if y and A[i - 1] == A[i]:
merge(i - 1, i)
if x and A[i - W] == A[i]:
merge(i - W, i)
def paint(i, c):
i = find(i)
if A[i] == c:
return
A[i] ^= 1
border = uf.component_border[i].copy()
for v in border:
x, y = divmod(v, W)
if 0 < y:
merge(v - 1, v)
if y < W - 1:
merge(v + 1, v)
if 0 < x:
merge(v - W, v)
if x < H - 1:
merge(v + W, v)
i = find(i)
uf.component_border[i] -= border
for R, C, X in RCX:
R -= 1
C -= 1
i = R * W + C
paint(i, X)
A = [A[find(i)] for i in range(N)]
rows = (A[i: i + W] for i in range(0, N, W))
print('\n'.join(' '.join(map(str, row)) for row in rows))
maspy