結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-28 15:04:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,995 ms / 3,000 ms
コード長 1,374 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 10,864 KB
実行使用メモリ 188,404 KB
最終ジャッジ日時 2023-09-21 19:27:45
合計ジャッジ時間 68,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,244 KB
testcase_01 AC 16 ms
8,252 KB
testcase_02 AC 16 ms
8,232 KB
testcase_03 AC 15 ms
8,156 KB
testcase_04 AC 16 ms
8,284 KB
testcase_05 AC 15 ms
8,216 KB
testcase_06 AC 15 ms
8,288 KB
testcase_07 AC 213 ms
28,360 KB
testcase_08 AC 416 ms
42,928 KB
testcase_09 AC 986 ms
79,260 KB
testcase_10 AC 1,900 ms
130,712 KB
testcase_11 AC 1,117 ms
90,672 KB
testcase_12 AC 277 ms
33,996 KB
testcase_13 AC 409 ms
43,228 KB
testcase_14 AC 295 ms
35,228 KB
testcase_15 AC 72 ms
14,420 KB
testcase_16 AC 326 ms
37,624 KB
testcase_17 AC 2,464 ms
156,416 KB
testcase_18 AC 2,487 ms
156,404 KB
testcase_19 AC 2,468 ms
156,628 KB
testcase_20 AC 2,466 ms
156,568 KB
testcase_21 AC 2,475 ms
156,416 KB
testcase_22 AC 2,458 ms
156,332 KB
testcase_23 AC 2,472 ms
156,332 KB
testcase_24 AC 2,464 ms
156,440 KB
testcase_25 AC 2,477 ms
156,548 KB
testcase_26 AC 2,498 ms
156,552 KB
testcase_27 AC 2,869 ms
38,224 KB
testcase_28 AC 2,911 ms
38,328 KB
testcase_29 AC 2,929 ms
38,300 KB
testcase_30 AC 2,907 ms
38,264 KB
testcase_31 AC 2,919 ms
38,012 KB
testcase_32 AC 2,958 ms
37,548 KB
testcase_33 AC 2,969 ms
37,528 KB
testcase_34 AC 2,963 ms
37,420 KB
testcase_35 AC 2,995 ms
37,588 KB
testcase_36 AC 2,964 ms
37,592 KB
testcase_37 AC 80 ms
8,032 KB
testcase_38 AC 2,187 ms
62,772 KB
testcase_39 AC 2,848 ms
188,404 KB
testcase_40 AC 16 ms
8,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(v, visited):
    global edges, matched
    for u in edges[v]:
        if u in visited:
            continue
        visited.add(u)
        if matched[u] == -1 or dfs(matched[u], visited):
            matched[u] = v
            return True
    return False
    
def mpc(xn, yn):
    global matched, edges
    matched = [-1] * yn

    left = xn
    right = 0

    ledges = []
    redges = matched

    for v in range(xn):
        if not dfs(v, set()):
            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([i]), set([j])]
                S[a].append((i, j))
                S[a][0].add(i)
                S[a][1].add(j)
                    
    
    cnt = 0
    for pairs in S.values():
        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