結果

問題 No.1479 Matrix Eraser
ユーザー ayaoniayaoni
提出日時 2021-04-17 03:18:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 770 ms / 3,000 ms
コード長 2,809 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 158,400 KB
最終ジャッジ日時 2024-07-03 09:15:41
合計ジャッジ時間 18,872 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
76,376 KB
testcase_01 AC 57 ms
76,448 KB
testcase_02 AC 53 ms
76,588 KB
testcase_03 AC 54 ms
76,584 KB
testcase_04 AC 54 ms
77,020 KB
testcase_05 AC 53 ms
77,216 KB
testcase_06 AC 52 ms
77,480 KB
testcase_07 AC 260 ms
101,140 KB
testcase_08 AC 283 ms
102,316 KB
testcase_09 AC 411 ms
109,088 KB
testcase_10 AC 581 ms
117,132 KB
testcase_11 AC 454 ms
110,668 KB
testcase_12 AC 266 ms
100,656 KB
testcase_13 AC 288 ms
103,032 KB
testcase_14 AC 245 ms
100,504 KB
testcase_15 AC 145 ms
98,388 KB
testcase_16 AC 259 ms
101,624 KB
testcase_17 AC 661 ms
120,812 KB
testcase_18 AC 688 ms
120,036 KB
testcase_19 AC 683 ms
120,428 KB
testcase_20 AC 678 ms
121,964 KB
testcase_21 AC 670 ms
121,172 KB
testcase_22 AC 651 ms
119,664 KB
testcase_23 AC 723 ms
120,424 KB
testcase_24 AC 707 ms
120,816 KB
testcase_25 AC 669 ms
120,476 KB
testcase_26 AC 690 ms
120,528 KB
testcase_27 AC 733 ms
136,044 KB
testcase_28 AC 696 ms
134,152 KB
testcase_29 AC 741 ms
135,332 KB
testcase_30 AC 726 ms
135,228 KB
testcase_31 AC 770 ms
136,672 KB
testcase_32 AC 375 ms
157,868 KB
testcase_33 AC 344 ms
158,400 KB
testcase_34 AC 318 ms
157,956 KB
testcase_35 AC 356 ms
157,796 KB
testcase_36 AC 375 ms
157,852 KB
testcase_37 AC 115 ms
116,136 KB
testcase_38 AC 381 ms
124,852 KB
testcase_39 AC 642 ms
123,748 KB
testcase_40 AC 50 ms
76,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class Dinic:
    def __init__(self, N, inf):
        self.N = N
        self.inf = inf
        self.G = [[] for _ in range(N)]
        self.level = [0]*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):
        self.level = [-1]*self.N
        deq = deque([s])
        self.level[s] = 0
        while deq:
            v = deq.pop()
            lv = self.level[v] + 1
            for w, cap, _ in self.G[v]:
                if cap > 0 and self.level[w] == -1:
                    self.level[w] = lv
                    deq.appendleft(w)

    def dfs(self, v, t, f):
        if v == t:
            return f
        for e in self.iter[v]:
            w, cap, rev = e
            if cap > 0 and self.level[v] < self.level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d > 0:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t] == -1:
                return flow
            *self.iter, = map(iter, self.G)
            f = self.inf
            while f > 0:
                f = self.dfs(s, t, self.inf)
                flow += f


H,W = MI()
A = [LI() for _ in range(H)]

X = [[] for _ in range(5*10**5+1)]
for i in range(H):
    for j in range(W):
        a = A[i][j]
        X[a].append((i,j))

ans = 0
inf = 10**18
for i in range(1,5*10**5+1):
    if not X[i]:
        continue

    A = set()
    B = set()
    for h,w in X[i]:
        A.add(h)
        B.add(w)

    A = list(A)
    B = list(B)
    lA = len(A)
    lB = len(B)

    Di = Dinic(lA+lB+2,inf)
    S = lA+lB
    T = lA+lB+1
    dic_A = {}
    dic_B = {}
    for j in range(lA):
        dic_A[A[j]] = j
        Di.add_edge(S,j,1)
    for j in range(lB):
        dic_B[B[j]] = j+lA
        Di.add_edge(j+lA,T,1)

    for h,w in X[i]:
        Di.add_edge(dic_A[h],dic_B[w],1)

    ans += Di.flow(S,T)

print(ans)
0