結果

問題 No.1479 Matrix Eraser
ユーザー ayaoniayaoni
提出日時 2021-04-16 21:38:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,912 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 221,840 KB
最終ジャッジ日時 2023-09-15 23:52:40
合計ジャッジ時間 9,754 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
79,708 KB
testcase_01 AC 83 ms
79,912 KB
testcase_02 AC 82 ms
79,756 KB
testcase_03 AC 81 ms
80,092 KB
testcase_04 AC 84 ms
79,912 KB
testcase_05 AC 85 ms
79,848 KB
testcase_06 AC 84 ms
80,028 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

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 FordFulkerson():
    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]

    def add(self,a,b,cap):  # 頂点aから頂点bへのコストcの辺(0-indexed)
        forward = [b,cap,None]
        backward = [a,0,forward]
        forward[2] = backward
        self.G[a].append(forward)
        self.G[b].append(backward)

    def dfs(self,start,end,f):
        if start == end:
            return f
        used = self.used
        used[start] = 1
        for e in self.G[start]:
            b,cap,backward = e
            if cap > 0 and used[b] == 0:
                d = self.dfs(b,end,min(f,cap))
                if d > 0:
                    e[1] -= d
                    backward[1] += d
                    return d
        return 0

    def flow(self,start,end):
        flow = 0
        inf = 10**9+7
        f = inf
        while f:
            self.used = [0]*self.N
            f = self.dfs(start,end,inf)
            flow += f
        return flow


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
for i in range(1,5*10**5+1):
    if not X[i]:
        continue

    F = FordFulkerson(H+W+2)
    S = H+W
    T = H+W+1
    for j in range(H):
        F.add(S,j,1)
    for k in range(H,H+W):
        F.add(k,T,1)
    for h,w in X[i]:
        F.add(h,H+w,1)

    ans += F.flow(S,T)

print(ans)
0