結果

問題 No.1479 Matrix Eraser
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-14 02:21:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,705 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 82,064 KB
実行使用メモリ 69,876 KB
最終ジャッジ日時 2024-09-18 07:51:57
合計ジャッジ時間 4,093 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #


from collections import defaultdict
from typing import List
from collections import deque
from typing import List, Tuple


class Hungarian:
    """
    軽量化Dinic法
    ref : https://snuke.hatenablog.com/entry/2019/05/07/013609
    """

    @staticmethod
    def fromEdges(n: int, edges: List[Tuple[int, int]]) -> "Hungarian":
        """从无向边列表构造"""
        adjList = [[] for _ in range(n)]
        for u, v in edges:
            adjList[u].append(v)
            adjList[v].append(u)
        colors, ok = isBipartite(n, adjList)
        if not ok:
            raise ValueError("not bipartite")
        h = Hungarian()
        for u, v in edges:
            if colors[u] == 1:
                u, v = v, u
            h.addEdge(u, v)
        return h

    __slots__ = ("_row", "_col", "_to")

    def __init__(self):
        self._row = 0
        self._col = 0
        self._to = [[]]

    def addEdge(self, u: int, v: int) -> None:
        """男孩u和女孩v连边"""
        if self._col <= v:
            self._col = v + 1
        if self._row <= u:
            self._row = u + 1
            while len(self._to) <= u:
                self._to.append([])
        self._to[u].append(v)

    def work(self) -> List[Tuple[int, int]]:
        """返回最大匹配"""
        n, m, to = self._row, self._col, self._to
        pre = [-1] * n
        root = [-1] * n
        p = [-1] * n
        q = [-1] * m
        upd = True
        while upd:
            upd = False
            s = []
            s_front = 0
            for i in range(n):
                if p[i] == -1:
                    root[i] = i
                    s.append(i)
            while s_front < len(s):
                v = s[s_front]
                s_front += 1
                if p[root[v]] != -1:
                    continue
                for u in to[v]:
                    if q[u] == -1:
                        while u != -1:
                            q[u] = v
                            p[v], u = u, p[v]
                            v = pre[v]
                        upd = True
                        break
                    u = q[u]
                    if pre[u] != -1:
                        continue
                    pre[u] = v
                    root[u] = root[v]
                    s.append(u)
            if upd:
                for i in range(n):
                    pre[i] = -1
                    root[i] = -1
        return [(v, p[v]) for v in range(n) if p[v] != -1]


def isBipartite(n: int, adjList: List[List[int]]) -> Tuple[List[int], bool]:
    """二分图检测 bfs染色"""

    def bfs(start: int) -> bool:
        colors[start] = 0
        queue = deque([start])
        while queue:
            cur = queue.popleft()
            for next in adjList[cur]:
                if colors[next] == -1:
                    colors[next] = colors[cur] ^ 1
                    queue.append(next)
                elif colors[next] == colors[cur]:
                    return False
        return True

    colors = [-1] * n
    for i in range(n):
        if colors[i] == -1 and not bfs(i):
            return [], False
    return colors, True



def solve(grid: List[List[int]]) -> int:
    ROW, COL = len(grid), len(grid[0])
    mp = defaultdict(list)
    for i in range(ROW):
        for j in range(COL):
            mp[grid[i][j]].append((i, j))

    res = 0
    for v, edges in mp.items():
        if v == 0:
            continue
        H = Hungarian()
        id1, id2 = dict(), dict()
        for u, v in edges:
            id1.setdefault(u, len(id1))
            id2.setdefault(v, len(id2))
            H.addEdge(id1[u], id2[v])
        res += len(H.work())

    return res
0