結果

問題 No.2263 Perms
ユーザー 👑 KazunKazun
提出日時 2023-04-07 22:28:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 2,498 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 77,392 KB
最終ジャッジ日時 2024-04-10 17:27:34
合計ジャッジ時間 4,367 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,336 KB
testcase_01 AC 38 ms
54,512 KB
testcase_02 AC 36 ms
53,488 KB
testcase_03 AC 36 ms
53,928 KB
testcase_04 AC 36 ms
52,660 KB
testcase_05 AC 47 ms
63,180 KB
testcase_06 AC 98 ms
77,352 KB
testcase_07 AC 94 ms
76,888 KB
testcase_08 AC 80 ms
76,844 KB
testcase_09 AC 93 ms
76,992 KB
testcase_10 AC 33 ms
53,204 KB
testcase_11 AC 89 ms
76,980 KB
testcase_12 AC 94 ms
76,864 KB
testcase_13 AC 94 ms
77,352 KB
testcase_14 AC 93 ms
77,028 KB
testcase_15 AC 34 ms
52,668 KB
testcase_16 AC 54 ms
69,944 KB
testcase_17 AC 60 ms
71,868 KB
testcase_18 AC 54 ms
69,332 KB
testcase_19 AC 67 ms
73,756 KB
testcase_20 AC 96 ms
77,392 KB
testcase_21 AC 91 ms
77,132 KB
testcase_22 AC 89 ms
76,884 KB
testcase_23 AC 81 ms
76,336 KB
testcase_24 AC 64 ms
71,512 KB
testcase_25 AC 96 ms
76,764 KB
testcase_26 AC 98 ms
77,204 KB
testcase_27 AC 72 ms
76,608 KB
testcase_28 AC 71 ms
76,992 KB
testcase_29 AC 66 ms
74,340 KB
testcase_30 AC 46 ms
64,272 KB
testcase_31 AC 41 ms
61,112 KB
testcase_32 AC 58 ms
71,748 KB
testcase_33 AC 57 ms
70,752 KB
testcase_34 AC 35 ms
54,208 KB
testcase_35 AC 33 ms
53,608 KB
testcase_36 AC 55 ms
65,428 KB
testcase_37 AC 38 ms
54,924 KB
testcase_38 AC 92 ms
76,740 KB
testcase_39 AC 36 ms
52,524 KB
testcase_40 AC 35 ms
53,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Hungarian(A, minimize=False, mode=False):
    """ 行列 A に対してハンガリアン法を適用し, 最大値を求める.

    A: 行列
    minimize: True にすると, 最小値を求める
    mode: True にすると, 最大値 (最小値) を達成する例も出力する.

    Reference: https://judge.yosupo.jp/submission/34963
    """
    if minimize:
        if mode:
            score, x=Hungarian([[-a_ij for a_ij in Ai] for Ai in A], False, True)
            return -score,x
        else:
            return -Hungarian([[-a_ij for a_ij in Ai] for Ai in A], False, False)

    inf=1<<31
    N=len(A)
    fx=[inf]*N
    fy=[0]*N
    x=[-1]*N
    y=[-1]*N
    i=0
    while i < N:
        t=[-1]*N
        s=[i]*(N + 1)
        p=q=0
        while p <= q and x[i] < 0:
            k, j = s[p], 0
            while j < N and x[i] < 0:
                if fx[k] + fy[j] == A[k][j] and t[j] < 0:
                    q += 1
                    s[q] = y[j]
                    t[j] = k
                    if s[q] < 0:
                        p = j
                        while p >= 0:
                            y[j] = k = t[j]
                            p = x[k]
                            x[k] = j
                            j = p
                j += 1
            p += 1
        if x[i] < 0:
            d = inf
            for k in range(q + 1):
                for j in range(N):
                    if t[j] < 0:
                        d = min(d, fx[s[k]] + fy[j] - A[s[k]][j])
            for j in range(N):
                if t[j] >= 0:
                    fy[j] += d
            for k in range(q + 1):
                fx[s[k]] -= d
        else:
            i += 1

    score=0
    for i,j in enumerate(x):
        score+=A[i][j]

    return (score,x) if mode else score

#==================================================
def solve():
    N,M=map(int,input().split())

    A=[None]*N
    S=[0]*N
    for i in range(N):
        A[i]=list(map(int,input().split()))

        if sum(A[i])!=M:
            print(-1)
            return

        for j in range(N):
            S[j]+=A[i][j]

    if not all(S[j]==M for j in range(N)):
        print(-1)
        return

    for _ in range(M):
        B=[[1 if A[i][j]>0 else 0 for j in range(N)] for i in range(N)]

        K,P=Hungarian(B,False,True)
        for i in range(N):
            A[i][P[i]]-=1

        print(*[P[i]+1 for i in range(N)])

    return



#==================================================
solve()
0