結果

問題 No.1479 Matrix Eraser
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-16 23:00:57
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,448 bytes
コンパイル時間 753 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 280,320 KB
最終ジャッジ日時 2024-07-03 03:31:52
合計ジャッジ時間 27,957 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
127,828 KB
testcase_01 AC 149 ms
127,736 KB
testcase_02 AC 149 ms
128,048 KB
testcase_03 AC 148 ms
127,616 KB
testcase_04 AC 146 ms
127,744 KB
testcase_05 AC 146 ms
127,616 KB
testcase_06 AC 151 ms
128,000 KB
testcase_07 AC 464 ms
154,624 KB
testcase_08 AC 583 ms
165,252 KB
testcase_09 AC 881 ms
193,784 KB
testcase_10 RE -
testcase_11 AC 971 ms
203,268 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 512 ms
160,256 KB
testcase_15 AC 302 ms
142,976 KB
testcase_16 RE -
testcase_17 AC 1,625 ms
251,264 KB
testcase_18 RE -
testcase_19 AC 1,723 ms
251,520 KB
testcase_20 AC 1,664 ms
252,544 KB
testcase_21 RE -
testcase_22 AC 1,738 ms
251,904 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 1,752 ms
253,296 KB
testcase_26 RE -
testcase_27 AC 618 ms
163,072 KB
testcase_28 AC 614 ms
163,064 KB
testcase_29 AC 569 ms
161,280 KB
testcase_30 AC 628 ms
163,604 KB
testcase_31 AC 576 ms
161,792 KB
testcase_32 AC 451 ms
188,756 KB
testcase_33 AC 451 ms
188,928 KB
testcase_34 AC 451 ms
188,800 KB
testcase_35 AC 441 ms
188,928 KB
testcase_36 AC 442 ms
189,056 KB
testcase_37 AC 193 ms
133,632 KB
testcase_38 AC 691 ms
196,352 KB
testcase_39 AC 1,803 ms
280,320 KB
testcase_40 AC 149 ms
127,844 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)]
Y = [set() for i in range(5*10**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