結果

問題 No.1479 Matrix Eraser
ユーザー tcltktcltk
提出日時 2021-06-28 02:43:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,039 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 92,040 KB
最終ジャッジ日時 2023-09-07 18:11:14
合計ジャッジ時間 7,264 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 208 ms
85,784 KB
testcase_01 AC 212 ms
81,540 KB
testcase_02 WA -
testcase_03 AC 218 ms
83,720 KB
testcase_04 AC 218 ms
83,416 KB
testcase_05 AC 214 ms
83,556 KB
testcase_06 AC 215 ms
83,388 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = """3 3
# 2 0 3
# 0 3 3
# 1 1 1
# """
# 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, 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, 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)]

n = 0
Done = [[(A[h][w] == 0) for w in range(W)] for h in range(H)]
for h in range(H):
    for w in range(W):
        if Done[h][w]:
            continue
        a = A[h][w]
        Points = []
        for h1 in range(H):
            for w1 in range(W):
                if A[h1][w1] == a:
                    Points.append((h1, w1))
                    Done[h1][w1] = True
        n += solve(Points)
print(n)
0