結果
問題 | No.1479 Matrix Eraser |
ユーザー | ああいい |
提出日時 | 2022-03-16 00:29:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,100 ms / 3,000 ms |
コード長 | 5,389 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,220 KB |
実行使用メモリ | 167,252 KB |
最終ジャッジ日時 | 2024-09-22 17:05:10 |
合計ジャッジ時間 | 16,154 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
75,136 KB |
testcase_01 | AC | 55 ms
75,568 KB |
testcase_02 | AC | 56 ms
76,004 KB |
testcase_03 | AC | 54 ms
75,904 KB |
testcase_04 | AC | 54 ms
75,624 KB |
testcase_05 | AC | 54 ms
75,828 KB |
testcase_06 | AC | 54 ms
75,792 KB |
testcase_07 | AC | 105 ms
95,732 KB |
testcase_08 | AC | 119 ms
94,716 KB |
testcase_09 | AC | 271 ms
120,528 KB |
testcase_10 | AC | 393 ms
132,184 KB |
testcase_11 | AC | 275 ms
125,140 KB |
testcase_12 | AC | 102 ms
93,996 KB |
testcase_13 | AC | 114 ms
93,696 KB |
testcase_14 | AC | 100 ms
94,116 KB |
testcase_15 | AC | 93 ms
96,660 KB |
testcase_16 | AC | 112 ms
94,252 KB |
testcase_17 | AC | 409 ms
149,332 KB |
testcase_18 | AC | 415 ms
149,376 KB |
testcase_19 | AC | 431 ms
149,248 KB |
testcase_20 | AC | 416 ms
149,292 KB |
testcase_21 | AC | 417 ms
149,476 KB |
testcase_22 | AC | 418 ms
149,296 KB |
testcase_23 | AC | 420 ms
149,388 KB |
testcase_24 | AC | 448 ms
149,252 KB |
testcase_25 | AC | 432 ms
149,428 KB |
testcase_26 | AC | 439 ms
149,368 KB |
testcase_27 | AC | 992 ms
146,764 KB |
testcase_28 | AC | 998 ms
146,076 KB |
testcase_29 | AC | 967 ms
146,652 KB |
testcase_30 | AC | 958 ms
145,976 KB |
testcase_31 | AC | 1,100 ms
150,692 KB |
testcase_32 | AC | 431 ms
160,924 KB |
testcase_33 | AC | 403 ms
159,916 KB |
testcase_34 | AC | 395 ms
161,580 KB |
testcase_35 | AC | 421 ms
160,160 KB |
testcase_36 | AC | 441 ms
167,252 KB |
testcase_37 | AC | 131 ms
116,992 KB |
testcase_38 | AC | 451 ms
127,540 KB |
testcase_39 | AC | 225 ms
154,624 KB |
testcase_40 | AC | 53 ms
75,008 KB |
ソースコード
#Dinic法で最大流を求める #deque のimport が必要 #逆辺追加しなきゃいけないから、 #グラフの構成はadd_edgeで行う #最大流は flow メソッドで """ from collections import deque class Dinic: def __init__(self,N): self.N = N self.G = [[] for _ in range(N)] self.level = None self.progress = None self.edge = [] 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) self.edge.append(forward) 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) self.edge.append(edge1) def get_edge(self,i): return self.edge[i] # i 回目に追加した辺のポインタを返す # 0-index, 順辺のみ def bfs(self,s,t): self.level = level = [None] * self.N q = deque([s]) level[s] = 0 G = self.G while q: v = q.popleft() lv = level[v] + 1 for w,cap,_ in G[v]: if cap and level[w] is None: level[w] = lv q.append(w) return level[t] is not None def dfs(self,v,t,f): if v == t:return f level = self.level Gv = self.G[v] for i in range(self.progress[v],len(Gv)): self.progress[v] = i w,cap,rev = e = Gv[i] 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 = 1 << 30 G = self.G while self.bfs(s,t): self.progress = [0] * self.N f = inf while f: f = self.dfs(s,t,inf) flow += f return flow def min_cut(self,s): #最小カットを実現する頂点の分割を与える #True なら source側 #False なら sink側 visited = [False for i in range(self.N)] q = deque([s]) while q: now = q.popleft() visited[now] = True for to,cap,_ in self.G[now]: if cap and not visited[to]: visited[to] = True q.append(to) return visited """ 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) else: pass 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 else: pass else: pass 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 import sys rr = sys.stdin H,W = map(int,rr.readline().split()) A = [list(map(int,rr.readline().split())) for _ in range(H)] d = [[] for _ in range(5 * 10 ** 5 + 1)] s = set() for h in range(H): for w in range(W): d[A[h][w]].append((h,w)) s.add(A[h][w]) inf = 1 << 30 ans = 0 for k in s: if k == 0:continue l = d[k] if len(l) == 1: ans += 1 continue elif len(l) == 2: if l[0][0] == l[1][0] or l[0][1] == l[1][1]: ans += 1 else: ans += 2 continue ch = set() cw = set() for h,w in l: ch.add(h) cw.add(w) rDh = sorted(ch) rDw = sorted(cw) Dh = {v:i for i,v in enumerate(rDh)} Dw = {v:i for i,v in enumerate(rDw)} n = len(Dh) + len(Dw) dinic = Dinic(n + 2) T = n + 1 base = len(Dh) for h,w in l: dinic.add_edge(Dh[h]+1,base + Dw[w] + 1,inf) for i in range(base): dinic.add_edge(0,i+1,1) for i in range(len(Dw)): dinic.add_edge(base + i + 1,T,1) ans += dinic.flow(0,T) print(ans)