結果

問題 No.1479 Matrix Eraser
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-16 22:27:02
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,302 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 86,764 KB
実行使用メモリ 177,288 KB
最終ジャッジ日時 2023-09-16 01:22:34
合計ジャッジ時間 46,997 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
80,788 KB
testcase_01 AC 100 ms
80,680 KB
testcase_02 AC 99 ms
80,752 KB
testcase_03 AC 99 ms
80,896 KB
testcase_04 AC 96 ms
80,680 KB
testcase_05 AC 97 ms
80,744 KB
testcase_06 AC 100 ms
81,048 KB
testcase_07 AC 392 ms
103,692 KB
testcase_08 AC 561 ms
107,400 KB
testcase_09 AC 1,119 ms
119,676 KB
testcase_10 AC 1,972 ms
150,996 KB
testcase_11 AC 1,184 ms
126,376 KB
testcase_12 AC 465 ms
105,016 KB
testcase_13 AC 520 ms
107,160 KB
testcase_14 AC 488 ms
106,016 KB
testcase_15 AC 230 ms
100,072 KB
testcase_16 AC 478 ms
105,900 KB
testcase_17 AC 2,586 ms
168,772 KB
testcase_18 AC 2,593 ms
168,232 KB
testcase_19 AC 2,576 ms
168,820 KB
testcase_20 AC 2,675 ms
168,072 KB
testcase_21 AC 2,695 ms
170,088 KB
testcase_22 AC 2,721 ms
169,216 KB
testcase_23 AC 2,758 ms
169,860 KB
testcase_24 AC 2,774 ms
168,472 KB
testcase_25 AC 2,774 ms
169,584 KB
testcase_26 AC 2,699 ms
169,352 KB
testcase_27 AC 516 ms
121,316 KB
testcase_28 AC 475 ms
120,668 KB
testcase_29 AC 480 ms
120,728 KB
testcase_30 AC 482 ms
120,644 KB
testcase_31 AC 483 ms
119,956 KB
testcase_32 AC 388 ms
157,184 KB
testcase_33 AC 385 ms
157,680 KB
testcase_34 AC 409 ms
158,860 KB
testcase_35 AC 382 ms
158,672 KB
testcase_36 AC 378 ms
157,524 KB
testcase_37 AC 151 ms
103,992 KB
testcase_38 AC 510 ms
125,244 KB
testcase_39 TLE -
testcase_40 AC 104 ms
80,752 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)]
for i in range(h):
    for j in range(w):
        xy[A[i][j]].append((i,j))

ans = 0
for i in range(5*10**5,0,-1):
    if xy[i] == []:
        continue
    s = h+w
    t = s+1
    maxflow = MaxFlow(t+1)
    #for j in range(h):
      #  maxflow.add_edge(s, j, 1)
    #for j in range(w):
     #   maxflow.add_edge(h+j, t, 1)
    sh = set()
    sw = set()
    for x,y in xy[i]:
        maxflow.add_edge(x, h+y, 1)
        if x not in sh:
            maxflow.add_edge(s, x, 1)
            sh.add(x)
        if y not in sw:
            maxflow.add_edge(h+y, t, 1)
            sw.add(y)
    ans += maxflow.flow(s, t)
print(ans)
0