結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-29 08:33:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,953 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 10,844 KB
実行使用メモリ 33,328 KB
最終ジャッジ日時 2023-09-22 16:12:13
合計ジャッジ時間 8,293 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
13,036 KB
testcase_01 AC 20 ms
8,496 KB
testcase_02 AC 20 ms
8,508 KB
testcase_03 AC 20 ms
8,536 KB
testcase_04 AC 19 ms
8,464 KB
testcase_05 AC 19 ms
8,452 KB
testcase_06 AC 20 ms
8,488 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 #

MAX_V = 500*2

def add_edge(u, v):
    global match
    G[u].append(v)
    G[v].append(u)
    
def dfs(v):
    global match, G, used
    used[v] = True
    for i in range(len(G[v])):
        u = G[v][i]
        w = match[u]
        if w < 0 or not used[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True
    return False

def bipartite_matching():
    global match, V, used
    res = 0
    match = [-1] * MAX_V
    for v in range(V):
        if match[v] < 0:
            used = [False] * MAX_V
            if dfs(v):
                res += 1
    return res

def matches(h, w, edges):
    global G, V, match
    G = [[] for _ in range(MAX_V)]
    V = h + w
    for v in range(h):
        for u in edges[v]:
            add_edge(v, u+h)
        
    res = bipartite_matching()
    return match[h:h+w]
    
def mpc(xn, yn):
    global edges
    matched = matches(xn, yn, edges)

    left = xn
    right = 0

    ledges = []
    redges = matched
    
    for v in range(xn):
        if v not in matched:
            left -= 1
            ledges.append(v)

    for v in ledges:
        for u in edges[v]:
            right += 1
            if redges[u] >= 0:
                left -= 1

    return left + right


if __name__ == '__main__':
    H, W = map(int, input().split())

    S = dict()

    for i in range(H):
        for j, a in enumerate(list(map(int, input().split()))):
            if a > 0:
                if a not in S:
                    S[a] = [set(), set()]
                S[a].append((i, j))
                S[a][0].add(i)
                S[a][1].add(j)
                    
    cnt = 0
    for a, pairs in S.items():
        hl = list(pairs[0])
        wl = list(pairs[1]) 
        h = len(hl)
        w = len(wl)
        edges = [set() for _ in range(h)]
        for i, j in pairs[2:]:
            edges[hl.index(i)].add(wl.index(j))
        n = mpc(h, w)
        cnt += n
    
    print(cnt)
      
0