結果

問題 No.1479 Matrix Eraser
ユーザー ああいいああいい
提出日時 2022-03-16 00:45:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,004 ms / 3,000 ms
コード長 2,722 bytes
コンパイル時間 1,252 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 158,872 KB
最終ジャッジ日時 2023-10-24 00:16:07
合計ジャッジ時間 17,515 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
75,204 KB
testcase_01 AC 52 ms
75,204 KB
testcase_02 AC 54 ms
75,336 KB
testcase_03 AC 53 ms
75,368 KB
testcase_04 AC 53 ms
75,408 KB
testcase_05 AC 54 ms
75,336 KB
testcase_06 AC 54 ms
75,376 KB
testcase_07 AC 131 ms
95,144 KB
testcase_08 AC 121 ms
94,208 KB
testcase_09 AC 306 ms
119,704 KB
testcase_10 AC 410 ms
131,404 KB
testcase_11 AC 347 ms
124,212 KB
testcase_12 AC 104 ms
93,556 KB
testcase_13 AC 114 ms
93,352 KB
testcase_14 AC 100 ms
93,512 KB
testcase_15 AC 91 ms
95,884 KB
testcase_16 AC 115 ms
93,712 KB
testcase_17 AC 459 ms
148,840 KB
testcase_18 AC 457 ms
148,840 KB
testcase_19 AC 479 ms
148,840 KB
testcase_20 AC 456 ms
148,840 KB
testcase_21 AC 452 ms
148,840 KB
testcase_22 AC 461 ms
148,840 KB
testcase_23 AC 480 ms
148,840 KB
testcase_24 AC 476 ms
148,840 KB
testcase_25 AC 485 ms
148,840 KB
testcase_26 AC 470 ms
148,840 KB
testcase_27 AC 960 ms
154,528 KB
testcase_28 AC 912 ms
152,624 KB
testcase_29 AC 886 ms
153,136 KB
testcase_30 AC 879 ms
152,784 KB
testcase_31 AC 1,004 ms
156,308 KB
testcase_32 AC 376 ms
158,644 KB
testcase_33 AC 375 ms
158,520 KB
testcase_34 AC 359 ms
158,028 KB
testcase_35 AC 369 ms
158,564 KB
testcase_36 AC 377 ms
158,872 KB
testcase_37 AC 129 ms
117,904 KB
testcase_38 AC 464 ms
129,828 KB
testcase_39 AC 219 ms
154,316 KB
testcase_40 AC 52 ms
75,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
 
 
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'))


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_link(Dh[h]+1,base + Dw[w] + 1,inf)
    for i in range(base):
        dinic.add_link(0,i+1,1)
    for i in range(len(Dw)):
        dinic.add_link(base + i + 1,T,1)
    ans += dinic.max_flow(0,T)
print(ans)
    
0