結果
| 問題 |
No.1479 Matrix Eraser
|
| ユーザー |
|
| 提出日時 | 2021-04-29 09:03:06 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,973 ms / 3,000 ms |
| コード長 | 1,850 bytes |
| コンパイル時間 | 74 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 189,560 KB |
| 最終ジャッジ日時 | 2024-07-08 08:11:19 |
| 合計ジャッジ時間 | 47,030 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
ソースコード
def add_edge(u, v):
global match
G[u].append(v)
G[v].append(u)
def dfs(v):
global match, G, used
used[v] = True
for i in range(len(G[v])):
u = G[v][i]
w = match[u]
if w < 0 or not used[w] and dfs(w):
match[v] = u
match[u] = v
return True
return False
def bipartite_matching():
global match, V, used
res = 0
match = [-1] * V
for v in range(V):
if match[v] < 0:
used = [False] * V
if dfs(v):
res += 1
return res
def mpc(xn, yn):
res = bipartite_matching()
matched = match[h:h+w]
left = xn
right = 0
redges = matched
for v in range(xn):
if v not in matched:
left -= 1
for u in G[v]:
right += 1
if redges[u-h] >= 0:
left -= 1
return left + right
if __name__ == '__main__':
H, W = map(int, input().split())
S = dict()
for i in range(H):
for j, a in enumerate(list(map(int, input().split()))):
if a > 0:
if a not in S:
S[a] = [dict(), dict()]
if i in S[a][0]:
ai = S[a][0][i]
else:
ai = len(S[a][0])
S[a][0][i] = ai
if j in S[a][1]:
aj = S[a][1][j]
else:
aj = len(S[a][1])
S[a][1][j] = aj
S[a].append((ai, aj))
cnt = 0
for a, pairs in S.items():
h = len(pairs[0])
w = len(pairs[1])
V = h + w
G = [[] for _ in range(V)]
for i, j in pairs[2:]:
add_edge(i, j+h)
n = mpc(h, w)
cnt += n
print(cnt)