結果
問題 | No.307 最近色塗る問題多くない? |
ユーザー | maspy |
提出日時 | 2020-03-22 12:09:39 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,699 ms / 4,000 ms |
コード長 | 1,943 bytes |
コンパイル時間 | 246 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 24,028 KB |
最終ジャッジ日時 | 2024-06-06 22:30:07 |
合計ジャッジ時間 | 10,040 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 24 ms
10,752 KB |
testcase_01 | AC | 25 ms
11,008 KB |
testcase_02 | AC | 25 ms
10,752 KB |
testcase_03 | AC | 24 ms
10,752 KB |
testcase_04 | AC | 30 ms
10,880 KB |
testcase_05 | AC | 27 ms
10,752 KB |
testcase_06 | AC | 27 ms
10,752 KB |
testcase_07 | AC | 55 ms
12,160 KB |
testcase_08 | AC | 214 ms
19,128 KB |
testcase_09 | AC | 114 ms
14,592 KB |
testcase_10 | AC | 51 ms
12,160 KB |
testcase_11 | AC | 104 ms
14,592 KB |
testcase_12 | AC | 25 ms
10,752 KB |
testcase_13 | AC | 80 ms
14,308 KB |
testcase_14 | AC | 38 ms
11,264 KB |
testcase_15 | AC | 44 ms
11,648 KB |
testcase_16 | AC | 38 ms
11,136 KB |
testcase_17 | AC | 93 ms
13,952 KB |
testcase_18 | AC | 151 ms
16,128 KB |
testcase_19 | AC | 169 ms
17,280 KB |
testcase_20 | AC | 33 ms
11,136 KB |
testcase_21 | AC | 153 ms
17,792 KB |
testcase_22 | AC | 334 ms
17,920 KB |
testcase_23 | AC | 177 ms
17,536 KB |
testcase_24 | AC | 174 ms
17,920 KB |
testcase_25 | AC | 242 ms
20,480 KB |
testcase_26 | AC | 245 ms
20,748 KB |
testcase_27 | AC | 1,699 ms
20,208 KB |
testcase_28 | AC | 1,057 ms
22,480 KB |
testcase_29 | AC | 65 ms
13,184 KB |
testcase_30 | AC | 249 ms
23,232 KB |
testcase_31 | AC | 804 ms
23,644 KB |
testcase_32 | AC | 146 ms
23,260 KB |
testcase_33 | AC | 583 ms
18,384 KB |
testcase_34 | AC | 531 ms
24,028 KB |
testcase_35 | AC | 553 ms
24,024 KB |
ソースコード
#!/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_border = [[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_border[x] += self.component_border[y] 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() uf.component_border[i].clear() 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) 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))