結果

問題 No.1479 Matrix Eraser
ユーザー tamatotamato
提出日時 2021-04-16 21:33:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,127 ms / 3,000 ms
コード長 3,541 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,664 KB
実行使用メモリ 165,456 KB
最終ジャッジ日時 2023-09-15 23:43:39
合計ジャッジ時間 25,612 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
80,524 KB
testcase_01 AC 112 ms
80,472 KB
testcase_02 AC 112 ms
80,368 KB
testcase_03 AC 111 ms
80,528 KB
testcase_04 AC 111 ms
80,348 KB
testcase_05 AC 110 ms
80,356 KB
testcase_06 AC 110 ms
80,588 KB
testcase_07 AC 256 ms
103,008 KB
testcase_08 AC 285 ms
104,944 KB
testcase_09 AC 421 ms
112,460 KB
testcase_10 AC 841 ms
128,720 KB
testcase_11 AC 508 ms
115,228 KB
testcase_12 AC 265 ms
103,808 KB
testcase_13 AC 302 ms
105,108 KB
testcase_14 AC 271 ms
104,864 KB
testcase_15 AC 121 ms
81,572 KB
testcase_16 AC 300 ms
104,400 KB
testcase_17 AC 1,108 ms
141,988 KB
testcase_18 AC 1,110 ms
141,392 KB
testcase_19 AC 1,091 ms
140,560 KB
testcase_20 AC 1,097 ms
142,876 KB
testcase_21 AC 1,096 ms
142,900 KB
testcase_22 AC 1,089 ms
141,300 KB
testcase_23 AC 1,105 ms
141,976 KB
testcase_24 AC 1,107 ms
141,980 KB
testcase_25 AC 1,127 ms
141,992 KB
testcase_26 AC 1,127 ms
141,456 KB
testcase_27 AC 808 ms
130,432 KB
testcase_28 AC 790 ms
129,408 KB
testcase_29 AC 849 ms
129,616 KB
testcase_30 AC 850 ms
130,144 KB
testcase_31 AC 811 ms
130,480 KB
testcase_32 AC 492 ms
165,192 KB
testcase_33 AC 475 ms
165,456 KB
testcase_34 AC 443 ms
165,120 KB
testcase_35 AC 482 ms
165,408 KB
testcase_36 AC 496 ms
165,180 KB
testcase_37 AC 155 ms
106,380 KB
testcase_38 AC 631 ms
142,432 KB
testcase_39 AC 207 ms
107,720 KB
testcase_40 AC 106 ms
80,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline
    from collections import deque

    class Dinic:
        def __init__(self, N):
            self.N = N
            self.adj = [[] for _ in range(N + 1)]
            self.lv = None
            self.progress = None
            self.inf = 1 << 60

        def add_edge(self, fr, to, cap):
            # [to, cap, rev]
            forward = [to, cap, None]
            backward = forward[2] = [fr, 0, forward]
            self.adj[fr].append(forward)
            self.adj[to].append(backward)

        def bfs(self, s, t):
            que = deque([s])
            lv = [-1] * (self.N + 1)
            lv[s] = 0
            while que:
                v = que.popleft()
                lvv = lv[v]
                for u, cap, _ in self.adj[v]:
                    if cap and lv[u] == -1:
                        que.append(u)
                        lv[u] = lvv + 1
                        if u == t:
                            break
            self.lv = lv

        def dfs(self, s, t, flow):
            st = deque([s])
            st_f = deque([flow])
            while st:
                v = st[-1]
                f = st_f[-1]
                if v == t:
                    for i in range(len(st) - 1, 0, -1):
                        p = st[i - 1]
                        edge = self.adj[p][self.progress[p] - 1]
                        edge[1] -= f
                        edge[2][1] += f
                    return f
                if self.progress[v] == len(self.adj[v]):
                    st.pop()
                    st_f.pop()
                    continue
                else:
                    adj_v = self.adj[v]
                    for i in range(self.progress[v], len(adj_v)):
                        i = self.progress[v]
                        self.progress[v] = i + 1
                        u, cap, rev = adj_v[i]
                        if not cap or self.lv[u] != self.lv[v] + 1:
                            continue
                        else:
                            st.append(u)
                            st_f.append(min(st_f[-1], cap))
                            break

        def max_flow(self, s, t):
            flow = 0
            while True:
                self.bfs(s, t)
                if self.lv[t] < 0:
                    return flow
                self.progress = [0] * (self.N + 1)
                flow_new = self.dfs(s, t, self.inf)
                while flow_new:
                    flow += flow_new
                    flow_new = self.dfs(s, t, self.inf)

    H, W = map(int, input().split())
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))

    D = Dinic(H + W + 2)
    s = H + W + 1
    t = s + 1
    edge = [[] for _ in range(5 * 10 ** 5 + 1)]
    for h in range(H):
        for w in range(W):
            a = A[h][w]
            edge[a].append((h, w))
    ans = 0
    for a in range(5 * 10 ** 5, 0, -1):
        if edge[a]:
            if len(edge[a]) == 1:
                ans += 1
                continue
            H_set = set()
            W_set = set()
            for h, w in edge[a]:
                D.add_edge(h+1, H+w+1, 1)
                H_set.add(h)
                W_set.add(w)
            for h in H_set:
                D.add_edge(s, h+1, 1)
            for w in W_set:
                D.add_edge(H+w+1, t, 1)
            ans += D.max_flow(s, t)
            D = Dinic(H + W + 2)
    print(ans)


if __name__ == '__main__':
    main()
0