結果

問題 No.1479 Matrix Eraser
ユーザー AEnAEn
提出日時 2023-06-02 01:08:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,744 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 160,604 KB
最終ジャッジ日時 2023-08-28 02:14:46
合計ジャッジ時間 30,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 104 ms
72,404 KB
testcase_03 AC 101 ms
72,260 KB
testcase_04 AC 103 ms
72,288 KB
testcase_05 AC 106 ms
72,580 KB
testcase_06 AC 100 ms
72,592 KB
testcase_07 AC 351 ms
88,028 KB
testcase_08 AC 436 ms
94,616 KB
testcase_09 AC 674 ms
111,584 KB
testcase_10 AC 1,049 ms
124,640 KB
testcase_11 AC 760 ms
110,728 KB
testcase_12 AC 380 ms
90,636 KB
testcase_13 AC 432 ms
95,452 KB
testcase_14 AC 402 ms
90,812 KB
testcase_15 AC 219 ms
82,360 KB
testcase_16 AC 400 ms
92,328 KB
testcase_17 AC 1,147 ms
139,516 KB
testcase_18 AC 1,150 ms
139,340 KB
testcase_19 AC 1,181 ms
139,540 KB
testcase_20 AC 1,176 ms
139,520 KB
testcase_21 AC 1,169 ms
139,536 KB
testcase_22 AC 1,169 ms
139,412 KB
testcase_23 AC 1,180 ms
139,628 KB
testcase_24 AC 1,178 ms
139,516 KB
testcase_25 AC 1,194 ms
139,356 KB
testcase_26 AC 1,147 ms
139,564 KB
testcase_27 AC 961 ms
150,028 KB
testcase_28 AC 901 ms
148,484 KB
testcase_29 AC 924 ms
148,896 KB
testcase_30 AC 903 ms
148,556 KB
testcase_31 AC 974 ms
149,580 KB
testcase_32 AC 497 ms
157,988 KB
testcase_33 AC 494 ms
158,744 KB
testcase_34 AC 477 ms
157,572 KB
testcase_35 AC 494 ms
157,936 KB
testcase_36 AC 489 ms
158,344 KB
testcase_37 WA -
testcase_38 AC 747 ms
136,940 KB
testcase_39 AC 1,152 ms
144,300 KB
testcase_40 AC 97 ms
71,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque
import sys
sys.setrecursionlimit(10**7)
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):
        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