結果

問題 No.1479 Matrix Eraser
ユーザー 小野寺健小野寺健
提出日時 2021-04-29 08:47:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,083 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 106,308 KB
最終ジャッジ日時 2023-09-22 16:27:29
合計ジャッジ時間 37,216 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,116 KB
testcase_01 AC 17 ms
8,104 KB
testcase_02 AC 17 ms
8,072 KB
testcase_03 AC 17 ms
8,200 KB
testcase_04 AC 17 ms
8,172 KB
testcase_05 AC 17 ms
8,176 KB
testcase_06 AC 17 ms
8,064 KB
testcase_07 AC 234 ms
21,472 KB
testcase_08 AC 406 ms
30,864 KB
testcase_09 AC 981 ms
54,572 KB
testcase_10 AC 1,930 ms
87,580 KB
testcase_11 AC 1,153 ms
62,140 KB
testcase_12 AC 293 ms
25,072 KB
testcase_13 AC 422 ms
30,868 KB
testcase_14 AC 307 ms
25,720 KB
testcase_15 AC 78 ms
12,216 KB
testcase_16 AC 344 ms
26,916 KB
testcase_17 AC 2,353 ms
105,900 KB
testcase_18 AC 2,331 ms
106,308 KB
testcase_19 AC 2,342 ms
106,104 KB
testcase_20 AC 2,468 ms
106,120 KB
testcase_21 AC 2,403 ms
106,184 KB
testcase_22 AC 2,311 ms
106,028 KB
testcase_23 AC 2,323 ms
105,904 KB
testcase_24 AC 2,342 ms
105,948 KB
testcase_25 AC 2,297 ms
106,120 KB
testcase_26 AC 2,318 ms
106,204 KB
testcase_27 TLE -
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 #

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 matches(h, w, edges):
    global G, V, match
    V = h + w
    G = [[] for _ in range(V)]
    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

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

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