結果

問題 No.1479 Matrix Eraser
ユーザー tcltktcltk
提出日時 2021-06-28 02:56:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,110 ms / 3,000 ms
コード長 3,104 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 146,608 KB
最終ジャッジ日時 2023-09-07 18:11:57
合計ジャッジ時間 30,395 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 208 ms
80,816 KB
testcase_01 AC 210 ms
81,008 KB
testcase_02 AC 215 ms
80,972 KB
testcase_03 AC 216 ms
81,012 KB
testcase_04 AC 214 ms
81,008 KB
testcase_05 AC 213 ms
81,136 KB
testcase_06 AC 213 ms
80,944 KB
testcase_07 AC 463 ms
92,788 KB
testcase_08 AC 513 ms
98,172 KB
testcase_09 AC 702 ms
112,140 KB
testcase_10 AC 954 ms
129,784 KB
testcase_11 AC 749 ms
116,012 KB
testcase_12 AC 469 ms
94,368 KB
testcase_13 AC 516 ms
98,448 KB
testcase_14 AC 475 ms
94,816 KB
testcase_15 AC 340 ms
88,172 KB
testcase_16 AC 483 ms
95,984 KB
testcase_17 AC 1,110 ms
127,244 KB
testcase_18 AC 1,062 ms
127,732 KB
testcase_19 AC 1,069 ms
127,788 KB
testcase_20 AC 1,068 ms
127,664 KB
testcase_21 AC 1,077 ms
127,736 KB
testcase_22 AC 1,110 ms
127,292 KB
testcase_23 AC 1,109 ms
127,828 KB
testcase_24 AC 1,103 ms
127,232 KB
testcase_25 AC 1,101 ms
127,684 KB
testcase_26 AC 1,063 ms
128,016 KB
testcase_27 AC 925 ms
138,056 KB
testcase_28 AC 946 ms
135,284 KB
testcase_29 AC 928 ms
136,948 KB
testcase_30 AC 937 ms
136,092 KB
testcase_31 AC 992 ms
139,656 KB
testcase_32 AC 545 ms
146,500 KB
testcase_33 AC 531 ms
146,328 KB
testcase_34 AC 512 ms
146,608 KB
testcase_35 AC 521 ms
146,352 KB
testcase_36 AC 520 ms
146,324 KB
testcase_37 AC 237 ms
84,908 KB
testcase_38 AC 647 ms
113,040 KB
testcase_39 AC 1,035 ms
137,088 KB
testcase_40 AC 212 ms
80,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq


def input():
    return sys.stdin.readline()[:-1]


sys.setrecursionlimit(1000000)

# _INPUT = """10 10
# 1 10 10 10 10 7 6 5 10 1
# 6 7 10 5 10 10 2 10 2 8
# 5 6 6 5 7 3 10 10 1 10
# 6 5 3 10 3 7 2 5 2 7
# 3 3 2 2 9 1 10 6 7 7
# 7 9 10 2 1 6 6 1 9 6
# 5 6 10 3 3 9 6 5 3 1
# 8 6 1 7 5 7 9 7 3 6
# 4 5 9 4 7 6 1 1 5 4
# 2 4 1 10 7 5 9 10 1 2

# """
# sys.stdin = io.StringIO(_INPUT)


class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])
 
    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = collections.deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth
 
    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, INF)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, INF)

INF = 10**10

def solve(Points):
    h_list = sorted(list(set(h for (h, w) in Points)))
    w_list = sorted(list(set(w for (h, w) in Points)))

    h_dic = {a: i for i, a in enumerate(h_list)}
    w_dic = {a: i for i, a in enumerate(w_list)}

    h_count = len(h_list)
    w_count = len(w_list)
    mf = Dinic(h_count+w_count+2)
    for i in range(h_count):
        mf.add_link(0, i+1, 1)
    for j in range(w_count):
        mf.add_link(j+h_count+1, h_count+w_count+1, 1)
    for h, w in Points:
        h1 = h_dic[h]
        w1 = w_dic[w]
        mf.add_link(h1+1, w1+h_count+1, INF)

    return mf.max_flow(0, h_count+w_count+1)


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

Table = collections.defaultdict(list)
for h in range(H):
    for w in range(W):
        a = A[h][w]
        if a == 0:
            continue
        Table[a].append((h, w))

n = 0
for Points in Table.values():
    n1 = solve(Points)
    n += n1
print(n)
0