結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-28 14:54:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,952 ms / 3,000 ms
コード長 1,422 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 188,488 KB
最終ジャッジ日時 2023-09-21 19:17:59
合計ジャッジ時間 67,911 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,052 KB
testcase_01 AC 15 ms
8,024 KB
testcase_02 AC 16 ms
8,044 KB
testcase_03 AC 17 ms
8,040 KB
testcase_04 AC 16 ms
8,200 KB
testcase_05 AC 15 ms
8,140 KB
testcase_06 AC 15 ms
8,200 KB
testcase_07 AC 226 ms
28,408 KB
testcase_08 AC 418 ms
43,032 KB
testcase_09 AC 1,067 ms
79,192 KB
testcase_10 AC 2,019 ms
130,552 KB
testcase_11 AC 1,221 ms
90,672 KB
testcase_12 AC 302 ms
34,060 KB
testcase_13 AC 419 ms
43,236 KB
testcase_14 AC 302 ms
35,332 KB
testcase_15 AC 74 ms
14,536 KB
testcase_16 AC 333 ms
37,692 KB
testcase_17 AC 2,489 ms
156,452 KB
testcase_18 AC 2,514 ms
156,304 KB
testcase_19 AC 2,654 ms
156,688 KB
testcase_20 AC 2,492 ms
156,528 KB
testcase_21 AC 2,510 ms
156,340 KB
testcase_22 AC 2,535 ms
156,480 KB
testcase_23 AC 2,530 ms
156,420 KB
testcase_24 AC 2,531 ms
156,380 KB
testcase_25 AC 2,554 ms
156,572 KB
testcase_26 AC 2,565 ms
156,572 KB
testcase_27 AC 2,846 ms
38,276 KB
testcase_28 AC 2,837 ms
38,244 KB
testcase_29 AC 2,847 ms
38,276 KB
testcase_30 AC 2,841 ms
38,244 KB
testcase_31 AC 2,871 ms
38,280 KB
testcase_32 AC 2,948 ms
37,608 KB
testcase_33 AC 2,942 ms
37,508 KB
testcase_34 AC 2,941 ms
37,616 KB
testcase_35 AC 2,940 ms
37,484 KB
testcase_36 AC 2,952 ms
37,544 KB
testcase_37 AC 82 ms
8,172 KB
testcase_38 AC 2,184 ms
62,840 KB
testcase_39 AC 2,866 ms
188,488 KB
testcase_40 AC 15 ms
8,108 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 = [True] * xn
    right = [False] * yn

    ledges = []
    redges = matched

    for v in range(xn):
        if not dfs(v, set()):
            left[v] = False
            ledges.append(v)
    for v in ledges:
        for u in edges[v]:
            right[u] = True
            if redges[u] >= 0:
                left[redges[u]] = False

    return sum(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