結果

問題 No.1479 Matrix Eraser
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-16 23:02:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,725 ms / 3,000 ms
コード長 3,452 bytes
コンパイル時間 842 ms
コンパイル使用メモリ 87,336 KB
実行使用メモリ 281,468 KB
最終ジャッジ日時 2023-09-16 02:21:38
合計ジャッジ時間 34,769 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
114,384 KB
testcase_01 AC 188 ms
136,152 KB
testcase_02 AC 185 ms
136,324 KB
testcase_03 AC 184 ms
136,072 KB
testcase_04 AC 187 ms
136,196 KB
testcase_05 AC 185 ms
136,360 KB
testcase_06 AC 185 ms
136,228 KB
testcase_07 AC 446 ms
155,208 KB
testcase_08 AC 550 ms
165,744 KB
testcase_09 AC 932 ms
195,512 KB
testcase_10 AC 1,458 ms
236,556 KB
testcase_11 AC 1,065 ms
203,892 KB
testcase_12 AC 503 ms
159,584 KB
testcase_13 AC 588 ms
166,500 KB
testcase_14 AC 556 ms
161,472 KB
testcase_15 AC 327 ms
143,868 KB
testcase_16 AC 543 ms
163,812 KB
testcase_17 AC 1,564 ms
252,400 KB
testcase_18 AC 1,725 ms
253,488 KB
testcase_19 AC 1,537 ms
254,580 KB
testcase_20 AC 1,550 ms
254,460 KB
testcase_21 AC 1,503 ms
254,376 KB
testcase_22 AC 1,532 ms
253,628 KB
testcase_23 AC 1,513 ms
252,888 KB
testcase_24 AC 1,532 ms
253,108 KB
testcase_25 AC 1,485 ms
253,428 KB
testcase_26 AC 1,494 ms
253,060 KB
testcase_27 AC 605 ms
163,916 KB
testcase_28 AC 609 ms
163,084 KB
testcase_29 AC 566 ms
162,052 KB
testcase_30 AC 607 ms
163,608 KB
testcase_31 AC 576 ms
162,996 KB
testcase_32 AC 450 ms
193,016 KB
testcase_33 AC 444 ms
193,052 KB
testcase_34 AC 454 ms
193,244 KB
testcase_35 AC 453 ms
193,356 KB
testcase_36 AC 443 ms
193,400 KB
testcase_37 AC 186 ms
119,332 KB
testcase_38 AC 672 ms
198,696 KB
testcase_39 AC 1,698 ms
281,468 KB
testcase_40 AC 170 ms
114,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
sys.setrecursionlimit(10**6)
class MaxFlow:
    inf = 10**18

    class E:
        def __init__(self,to,cap):
            self.to = to
            self.cap = cap
            self.rev = None

    def __init__(self,n):
        self.n = n
        self.graph = [[] for _ in range(n)]

    def add_edge(self, fr, to, cap):
        graph = self.graph
        edge = self.E(to,cap)
        edge2 = self.E(fr,0)
        edge.rev = edge2
        edge2.rev = edge
        graph[fr].append(edge)
        graph[to].append(edge2)

    def bfs(self, s, t):
        level = self.level = [self.n]*self.n
        q = deque([s])
        level[s] = 0
        while q:
            now = q.popleft()
            lw = level[now]+1
            for e in self.graph[now]:
                if e.cap and level[e.to]> lw:
                    level[e.to] = lw
                    if e.to == t:
                        return True
                    q.append(e.to)
        return False

    def dfs(self, s, t, up):
        graph = self.graph
        it = self.it
        level = self.level

        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = graph[w][it[w]].rev
                    flow = min(flow, e.cap)
                for w in st:
                    e = graph[w][it[w]]
                    e.cap += flow
                    e.rev.cap -= flow
                return flow
            lv = level[v]-1
            while it[v] < len(graph[v]):
                e = graph[v][it[v]]
                re = e.rev
                if re.cap == 0 or lv != level[e.to]:
                    it[v] += 1
                    continue
                st.append(e.to)
                break
            if it[v] == len(graph[v]):
                st.pop()
                level[v] = self.n

        return 0

    def flow(self,s,t,flow_limit=inf):
        flow = 0
        while flow < flow_limit and self.bfs(s,t):
            self.it = [0]*self.n
            while flow < flow_limit:
                f = self.dfs(s,t,flow_limit-flow)
                if f == 0:
                    break
                flow += f
        return flow

    def min_cut(self,s):
        visited = [0]*self.n
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = 1
            for e in self.graph[v]:
                if e.cap and not visited[e.to]:
                    q.append(e.to)
        return visited
h,w = map(int,input().split())
A = [list(map(int,input().split())) for i in range(h)]

xy = [[] for i in range(5*10**5+5)]
X = [set() for i in range(5*10**5+5)]
Y = [set() for i in range(5*10**5+5)]
for i in range(h):
    for j in range(w):
        num = A[i][j]
        if num == 0:
            continue
        X[num].add(i)
        Y[num].add(j)
        xy[num].append((i,j))

ans = 0
for i in range(5*10**5,0,-1):
    if xy[i] == []:
        continue
    nh = len(X[i])
    nw = len(Y[i])
    dich = {x:j for j,x in enumerate(sorted(list(X[i])))}
    dicw = {x:j for j,x in enumerate(sorted(list(Y[i])))}
    s = nh+nw
    t = s+1
    maxflow = MaxFlow(t+1)
    for c in X[i]:
        maxflow.add_edge(s, dich[c], 1)
    for c in Y[i]:
        maxflow.add_edge(nh+dicw[c], t, 1)
    for x,y in xy[i]:
        maxflow.add_edge(dich[x], nh+dicw[y], 1)
    ans += maxflow.flow(s, t)
print(ans)
0