結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-29 09:03:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,323 ms / 3,000 ms
コード長 1,850 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 187,164 KB
最終ジャッジ日時 2023-09-22 16:42:09
合計ジャッジ時間 52,081 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,056 KB
testcase_01 AC 17 ms
8,056 KB
testcase_02 AC 18 ms
7,976 KB
testcase_03 AC 18 ms
8,116 KB
testcase_04 AC 17 ms
8,048 KB
testcase_05 AC 17 ms
7,980 KB
testcase_06 AC 17 ms
8,056 KB
testcase_07 AC 211 ms
28,424 KB
testcase_08 AC 364 ms
42,896 KB
testcase_09 AC 909 ms
78,976 KB
testcase_10 AC 1,611 ms
129,864 KB
testcase_11 AC 1,015 ms
90,312 KB
testcase_12 AC 262 ms
33,996 KB
testcase_13 AC 363 ms
42,796 KB
testcase_14 AC 272 ms
35,148 KB
testcase_15 AC 71 ms
14,236 KB
testcase_16 AC 303 ms
37,780 KB
testcase_17 AC 1,988 ms
155,228 KB
testcase_18 AC 1,977 ms
155,644 KB
testcase_19 AC 1,987 ms
155,524 KB
testcase_20 AC 1,951 ms
155,456 KB
testcase_21 AC 1,947 ms
155,620 KB
testcase_22 AC 1,961 ms
155,352 KB
testcase_23 AC 2,010 ms
155,216 KB
testcase_24 AC 1,968 ms
155,280 KB
testcase_25 AC 1,940 ms
155,652 KB
testcase_26 AC 1,981 ms
155,552 KB
testcase_27 AC 2,067 ms
33,988 KB
testcase_28 AC 2,081 ms
34,168 KB
testcase_29 AC 2,104 ms
34,016 KB
testcase_30 AC 2,092 ms
33,876 KB
testcase_31 AC 2,074 ms
34,000 KB
testcase_32 AC 1,942 ms
34,472 KB
testcase_33 AC 1,948 ms
34,548 KB
testcase_34 AC 1,945 ms
34,468 KB
testcase_35 AC 1,947 ms
34,520 KB
testcase_36 AC 1,960 ms
34,524 KB
testcase_37 AC 80 ms
8,484 KB
testcase_38 AC 2,323 ms
56,400 KB
testcase_39 AC 2,014 ms
187,164 KB
testcase_40 AC 16 ms
8,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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] * V
    for v in range(V):
        if match[v] < 0:
            used = [False] * V
            if dfs(v):
                res += 1
    return res
    
def mpc(xn, yn):
    res = bipartite_matching()
    matched = match[h:h+w]

    left = xn
    right = 0

    redges = matched
    
    for v in range(xn):
        if v not in matched:
            left -= 1
            for u in G[v]:
                right += 1
                if redges[u-h] >= 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] = [dict(), dict()]
                if i in S[a][0]:
                    ai = S[a][0][i]
                else:
                    ai = len(S[a][0])
                    S[a][0][i] = ai
                if j in S[a][1]:
                    aj = S[a][1][j]
                else:
                    aj = len(S[a][1])
                    S[a][1][j] = aj                   
                S[a].append((ai, aj))

    cnt = 0
    for a, pairs in S.items():
        h = len(pairs[0])
        w = len(pairs[1])
        V = h + w
        G = [[] for _ in range(V)]
        for i, j in pairs[2:]:
            add_edge(i, j+h)
        n = mpc(h, w)
        cnt += n
    
    print(cnt)
0