結果
問題 | No.1479 Matrix Eraser |
ユーザー | ntuda |
提出日時 | 2021-12-18 22:58:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,137 ms / 3,000 ms |
コード長 | 2,646 bytes |
コンパイル時間 | 293 ms |
コンパイル使用メモリ | 82,124 KB |
実行使用メモリ | 162,588 KB |
最終ジャッジ日時 | 2024-09-15 14:04:31 |
合計ジャッジ時間 | 25,181 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,272 KB |
testcase_01 | AC | 46 ms
55,296 KB |
testcase_02 | AC | 48 ms
54,784 KB |
testcase_03 | AC | 44 ms
54,656 KB |
testcase_04 | AC | 45 ms
55,168 KB |
testcase_05 | AC | 44 ms
54,912 KB |
testcase_06 | AC | 44 ms
54,656 KB |
testcase_07 | AC | 335 ms
87,340 KB |
testcase_08 | AC | 379 ms
100,480 KB |
testcase_09 | AC | 589 ms
111,256 KB |
testcase_10 | AC | 962 ms
140,744 KB |
testcase_11 | AC | 692 ms
120,176 KB |
testcase_12 | AC | 345 ms
91,336 KB |
testcase_13 | AC | 372 ms
100,568 KB |
testcase_14 | AC | 335 ms
91,524 KB |
testcase_15 | AC | 180 ms
80,276 KB |
testcase_16 | AC | 360 ms
94,332 KB |
testcase_17 | AC | 1,093 ms
145,884 KB |
testcase_18 | AC | 1,095 ms
145,736 KB |
testcase_19 | AC | 1,137 ms
145,880 KB |
testcase_20 | AC | 1,135 ms
145,736 KB |
testcase_21 | AC | 1,129 ms
145,620 KB |
testcase_22 | AC | 1,063 ms
145,752 KB |
testcase_23 | AC | 1,100 ms
145,876 KB |
testcase_24 | AC | 1,094 ms
146,004 KB |
testcase_25 | AC | 1,096 ms
145,996 KB |
testcase_26 | AC | 1,093 ms
145,884 KB |
testcase_27 | AC | 850 ms
133,132 KB |
testcase_28 | AC | 809 ms
134,044 KB |
testcase_29 | AC | 790 ms
134,724 KB |
testcase_30 | AC | 813 ms
134,184 KB |
testcase_31 | AC | 797 ms
134,772 KB |
testcase_32 | AC | 413 ms
153,812 KB |
testcase_33 | AC | 392 ms
153,424 KB |
testcase_34 | AC | 376 ms
154,568 KB |
testcase_35 | AC | 409 ms
154,272 KB |
testcase_36 | AC | 411 ms
154,068 KB |
testcase_37 | AC | 123 ms
108,752 KB |
testcase_38 | AC | 472 ms
124,640 KB |
testcase_39 | AC | 968 ms
162,588 KB |
testcase_40 | AC | 46 ms
54,656 KB |
ソースコード
# Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] dic = {} AF = set() #存在する値のセット for i in range(H): for j in range(W): a = A[i][j] if a in dic: dic[a].append([i,j]) else: dic[a] = [[i,j]] AF.add(A[i][j]) if 0 in AF: AF.remove(0) AF = sorted(AF,reverse = True) ans = 0 for a in AF: Vs = dic[a] setH = set() setW = set() for i,j in Vs: setH.add(i) setW.add(j) H1 = len(setH) W1 = len(setW) B = sorted(list(set(setH))) dicH = dict(zip(B, range(len(B)))) B = sorted(list(set(setW))) dicW = dict(zip(B, range(len(B)))) for i in range(len(Vs)): Vs[i][0] = dicH[Vs[i][0]] Vs[i][1] = dicW[Vs[i][1]] N = H1 + W1 + 2 dinic = Dinic(N) for i in range(H1): dinic.add_edge(0,i+1,1) for i in range(W1): dinic.add_edge(H1 + i + 1,N-1,1) for i,j in Vs: dinic.add_edge(i+1,H1 + j + 1,1) ans += dinic.flow(0, N-1) print(ans)