結果

問題 No.1479 Matrix Eraser
ユーザー とりゐとりゐ
提出日時 2021-11-11 19:46:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,003 ms / 3,000 ms
コード長 3,148 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 137,728 KB
最終ジャッジ日時 2024-05-02 21:21:26
合計ジャッジ時間 22,676 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
54,400 KB
testcase_01 AC 58 ms
55,040 KB
testcase_02 AC 51 ms
54,912 KB
testcase_03 AC 52 ms
55,168 KB
testcase_04 AC 51 ms
55,040 KB
testcase_05 AC 51 ms
55,168 KB
testcase_06 AC 52 ms
55,040 KB
testcase_07 AC 345 ms
85,888 KB
testcase_08 AC 368 ms
88,960 KB
testcase_09 AC 566 ms
99,328 KB
testcase_10 AC 885 ms
110,760 KB
testcase_11 AC 603 ms
99,712 KB
testcase_12 AC 339 ms
87,296 KB
testcase_13 AC 393 ms
88,832 KB
testcase_14 AC 333 ms
86,016 KB
testcase_15 AC 217 ms
80,384 KB
testcase_16 AC 357 ms
87,040 KB
testcase_17 AC 911 ms
117,844 KB
testcase_18 AC 1,003 ms
117,596 KB
testcase_19 AC 909 ms
117,956 KB
testcase_20 AC 963 ms
117,580 KB
testcase_21 AC 910 ms
117,580 KB
testcase_22 AC 923 ms
117,548 KB
testcase_23 AC 926 ms
117,704 KB
testcase_24 AC 911 ms
117,476 KB
testcase_25 AC 972 ms
117,592 KB
testcase_26 AC 969 ms
117,708 KB
testcase_27 AC 495 ms
102,016 KB
testcase_28 AC 459 ms
100,864 KB
testcase_29 AC 473 ms
100,608 KB
testcase_30 AC 481 ms
99,840 KB
testcase_31 AC 538 ms
102,144 KB
testcase_32 AC 362 ms
137,728 KB
testcase_33 AC 335 ms
127,232 KB
testcase_34 AC 339 ms
126,976 KB
testcase_35 AC 347 ms
127,232 KB
testcase_36 AC 374 ms
137,728 KB
testcase_37 AC 96 ms
76,416 KB
testcase_38 AC 531 ms
109,696 KB
testcase_39 AC 899 ms
127,256 KB
testcase_40 AC 49 ms
54,144 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

from collections import defaultdict
h,w=map(int,input().split())
d=defaultdict(list)
for i in range(h):
  a=list(map(int,input().split()))
  for j in range(w):
    if a[j]!=0:
      d[a[j]].append((i,h+j))
ans=0
for i in d:
  x=set()
  y=set()
  for xi,yi in d[i]:
    x.add(xi)
    y.add(yi)
  X,Y=len(x),len(y)
  mf=MaxFlow(X+Y+2)
  s,t=0,X+Y+1
  id=defaultdict(int)
  tmp=1
  for j in x:
    id[j]=tmp
    mf.add_edge(s,tmp,1)
    tmp+=1
  for j in y:
    id[j]=tmp
    mf.add_edge(tmp,t,1)
    tmp+=1
  for xi,yi in d[i]:
    mf.add_edge(id[xi],id[yi],1)
  ans+=mf.flow(s,t)
print(ans)
0