結果

問題 No.1479 Matrix Eraser
ユーザー AEnAEn
提出日時 2023-06-02 01:10:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 954 ms / 3,000 ms
コード長 2,771 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 158,288 KB
最終ジャッジ日時 2023-08-28 02:15:15
合計ジャッジ時間 23,909 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,476 KB
testcase_01 AC 90 ms
72,216 KB
testcase_02 AC 91 ms
72,180 KB
testcase_03 AC 92 ms
72,428 KB
testcase_04 AC 92 ms
72,388 KB
testcase_05 AC 90 ms
72,140 KB
testcase_06 AC 92 ms
72,264 KB
testcase_07 AC 317 ms
87,732 KB
testcase_08 AC 360 ms
94,592 KB
testcase_09 AC 533 ms
111,384 KB
testcase_10 AC 790 ms
126,156 KB
testcase_11 AC 578 ms
110,336 KB
testcase_12 AC 319 ms
90,732 KB
testcase_13 AC 355 ms
94,992 KB
testcase_14 AC 337 ms
90,748 KB
testcase_15 AC 195 ms
82,096 KB
testcase_16 AC 337 ms
92,324 KB
testcase_17 AC 887 ms
140,640 KB
testcase_18 AC 905 ms
140,788 KB
testcase_19 AC 914 ms
140,788 KB
testcase_20 AC 909 ms
140,536 KB
testcase_21 AC 914 ms
140,816 KB
testcase_22 AC 906 ms
140,664 KB
testcase_23 AC 925 ms
140,640 KB
testcase_24 AC 921 ms
140,604 KB
testcase_25 AC 954 ms
140,644 KB
testcase_26 AC 897 ms
140,632 KB
testcase_27 AC 827 ms
150,080 KB
testcase_28 AC 788 ms
148,232 KB
testcase_29 AC 790 ms
148,696 KB
testcase_30 AC 793 ms
148,220 KB
testcase_31 AC 822 ms
150,008 KB
testcase_32 AC 403 ms
157,668 KB
testcase_33 AC 400 ms
158,288 KB
testcase_34 AC 382 ms
157,576 KB
testcase_35 AC 396 ms
157,764 KB
testcase_36 AC 394 ms
158,112 KB
testcase_37 AC 115 ms
78,504 KB
testcase_38 AC 594 ms
136,508 KB
testcase_39 AC 937 ms
144,112 KB
testcase_40 AC 88 ms
71,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque
import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
 
class Dinic:
    def __init__(self, n):
        """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):
        """sからtへの残余ネットワーク上での最短距離"""
        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
        for i in range(self.progress[v], len(self.links[v])):
            self.progress[v] = i
            cap, to, rev = self.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
            # 残余ネットワークの更新
            self.links[v][i][0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        """最大フローを求める"""
        flow = 0
        while True:
            # tに到達できるか
            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)]

d = defaultdict(list)
for i in range(H):
    for j in range(W):
        if A[i][j]!=0:
            d[A[i][j]].append([i,j])

res = 0
for num in d.keys():
    X = set()
    Y = set()
    for x,y in d[num]:
        X.add(x);Y.add(y)
    X = {num:i for i,num in enumerate(sorted(list(X)))}
    Y = {num:i+len(X) for i,num in enumerate(sorted(list(Y)))}
    mf = Dinic(len(X)+len(Y)+2)
    for x in X:
        mf.add_link(len(X)+len(Y),X[x],1)
    for y in Y:
        mf.add_link(Y[y],len(X)+len(Y)+1,1)
    for x,y in d[num]:
        mf.add_link(X[x],Y[y],1)
    res += mf.max_flow(len(X)+len(Y),len(X)+len(Y)+1)
print(res)
0