結果

問題 No.1479 Matrix Eraser
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-09-13 14:24:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,239 ms / 3,000 ms
コード長 2,445 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 86,940 KB
実行使用メモリ 216,060 KB
最終ジャッジ日時 2023-09-13 14:24:55
合計ジャッジ時間 30,522 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
72,760 KB
testcase_01 AC 104 ms
72,904 KB
testcase_02 AC 105 ms
72,828 KB
testcase_03 AC 104 ms
72,748 KB
testcase_04 AC 104 ms
72,536 KB
testcase_05 AC 104 ms
72,752 KB
testcase_06 AC 105 ms
72,568 KB
testcase_07 AC 366 ms
96,944 KB
testcase_08 AC 425 ms
113,712 KB
testcase_09 AC 634 ms
135,472 KB
testcase_10 AC 964 ms
173,692 KB
testcase_11 AC 664 ms
139,204 KB
testcase_12 AC 384 ms
102,880 KB
testcase_13 AC 451 ms
113,700 KB
testcase_14 AC 389 ms
104,116 KB
testcase_15 AC 231 ms
85,276 KB
testcase_16 AC 406 ms
107,688 KB
testcase_17 AC 1,084 ms
191,316 KB
testcase_18 AC 1,110 ms
191,508 KB
testcase_19 AC 1,061 ms
191,712 KB
testcase_20 AC 1,097 ms
191,560 KB
testcase_21 AC 1,095 ms
191,392 KB
testcase_22 AC 1,088 ms
191,360 KB
testcase_23 AC 1,098 ms
191,380 KB
testcase_24 AC 1,239 ms
191,452 KB
testcase_25 AC 1,100 ms
191,700 KB
testcase_26 AC 1,134 ms
191,704 KB
testcase_27 AC 1,093 ms
145,760 KB
testcase_28 AC 1,045 ms
144,072 KB
testcase_29 AC 989 ms
143,560 KB
testcase_30 AC 1,003 ms
141,424 KB
testcase_31 AC 1,039 ms
146,684 KB
testcase_32 AC 413 ms
141,340 KB
testcase_33 AC 426 ms
142,356 KB
testcase_34 AC 393 ms
141,872 KB
testcase_35 AC 411 ms
141,312 KB
testcase_36 AC 418 ms
141,116 KB
testcase_37 AC 159 ms
99,200 KB
testcase_38 AC 633 ms
153,408 KB
testcase_39 AC 983 ms
216,060 KB
testcase_40 AC 147 ms
72,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])
 
    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth
 
    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))
                

H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
ans = 0
V = defaultdict(set)
E = defaultdict(list)
N = H*W
for i in range(H):
    for j in range(W):
        a = A[i][j]
        V[a].add(i)
        V[a].add(j+N)
        E[a].append((i,j+N))

for k in V.keys():
    if k==0:
        continue
    v = list(V[k])+[-1,2*N]
    v.sort()
    n = len(v)
    D = Dinic(n)
    conv = {v[i]:i for i in range(n)}
    S = set()
    T = set()
    for i,j in E[k]:
        S.add(conv[i])
        T.add(conv[j])
        D.add_link(conv[i],conv[j],1)
    for s in S:
        D.add_link(0,s,1)
    for t in T:
        D.add_link(t,n-1,1)
    ans += D.max_flow(0,n-1)
print(ans)
    
0