結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-28 14:16:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,930 ms / 3,000 ms
コード長 1,349 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 69,644 KB
最終ジャッジ日時 2023-09-21 18:36:06
合計ジャッジ時間 46,462 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,700 KB
testcase_01 AC 19 ms
8,624 KB
testcase_02 AC 19 ms
8,668 KB
testcase_03 AC 20 ms
8,556 KB
testcase_04 AC 20 ms
8,660 KB
testcase_05 AC 18 ms
8,624 KB
testcase_06 AC 19 ms
8,536 KB
testcase_07 AC 166 ms
15,228 KB
testcase_08 AC 271 ms
20,820 KB
testcase_09 AC 567 ms
33,824 KB
testcase_10 AC 1,080 ms
50,608 KB
testcase_11 AC 655 ms
37,252 KB
testcase_12 AC 204 ms
17,232 KB
testcase_13 AC 270 ms
20,780 KB
testcase_14 AC 212 ms
17,652 KB
testcase_15 AC 62 ms
10,624 KB
testcase_16 AC 236 ms
17,904 KB
testcase_17 AC 1,290 ms
63,056 KB
testcase_18 AC 1,292 ms
63,040 KB
testcase_19 AC 1,301 ms
62,988 KB
testcase_20 AC 1,293 ms
62,988 KB
testcase_21 AC 1,291 ms
62,900 KB
testcase_22 AC 1,295 ms
63,044 KB
testcase_23 AC 1,282 ms
62,984 KB
testcase_24 AC 1,288 ms
63,040 KB
testcase_25 AC 1,294 ms
62,864 KB
testcase_26 AC 1,291 ms
63,192 KB
testcase_27 AC 2,861 ms
31,740 KB
testcase_28 AC 2,849 ms
31,800 KB
testcase_29 AC 2,849 ms
31,856 KB
testcase_30 AC 2,843 ms
31,772 KB
testcase_31 AC 2,851 ms
31,768 KB
testcase_32 AC 2,907 ms
36,524 KB
testcase_33 AC 2,917 ms
36,612 KB
testcase_34 AC 2,920 ms
36,632 KB
testcase_35 AC 2,930 ms
36,672 KB
testcase_36 AC 2,907 ms
36,544 KB
testcase_37 AC 84 ms
8,704 KB
testcase_38 AC 2,085 ms
30,880 KB
testcase_39 AC 1,386 ms
69,644 KB
testcase_40 AC 19 ms
8,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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 = defaultdict(list)

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