結果

問題 No.2263 Perms
ユーザー 👑 KazunKazun
提出日時 2023-04-07 22:33:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,562 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 77,308 KB
最終ジャッジ日時 2024-04-10 17:30:52
合計ジャッジ時間 4,467 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,924 KB
testcase_01 AC 36 ms
53,684 KB
testcase_02 AC 38 ms
52,928 KB
testcase_03 AC 35 ms
53,996 KB
testcase_04 AC 36 ms
53,072 KB
testcase_05 AC 44 ms
59,784 KB
testcase_06 AC 91 ms
76,804 KB
testcase_07 AC 96 ms
77,000 KB
testcase_08 AC 66 ms
72,536 KB
testcase_09 AC 90 ms
76,864 KB
testcase_10 AC 37 ms
52,856 KB
testcase_11 AC 85 ms
77,180 KB
testcase_12 AC 84 ms
77,188 KB
testcase_13 AC 84 ms
77,200 KB
testcase_14 AC 85 ms
77,048 KB
testcase_15 AC 36 ms
53,672 KB
testcase_16 AC 45 ms
62,616 KB
testcase_17 AC 52 ms
65,260 KB
testcase_18 AC 47 ms
62,120 KB
testcase_19 AC 58 ms
66,076 KB
testcase_20 AC 87 ms
76,708 KB
testcase_21 AC 91 ms
77,308 KB
testcase_22 AC 90 ms
76,892 KB
testcase_23 AC 67 ms
71,976 KB
testcase_24 AC 51 ms
64,676 KB
testcase_25 AC 92 ms
77,296 KB
testcase_26 AC 90 ms
76,704 KB
testcase_27 AC 65 ms
73,612 KB
testcase_28 AC 58 ms
69,472 KB
testcase_29 AC 59 ms
69,756 KB
testcase_30 AC 42 ms
62,192 KB
testcase_31 AC 39 ms
54,468 KB
testcase_32 AC 50 ms
65,164 KB
testcase_33 AC 48 ms
62,976 KB
testcase_34 AC 35 ms
53,572 KB
testcase_35 AC 34 ms
53,160 KB
testcase_36 AC 42 ms
61,628 KB
testcase_37 AC 37 ms
54,004 KB
testcase_38 AC 83 ms
76,664 KB
testcase_39 AC 38 ms
53,512 KB
testcase_40 AC 36 ms
52,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 参考 URL
# https://snuke.hatenablog.com/entry/2019/05/07/013609

class Bipartite_Matching:
    def __init__(self,M,N):
        """ サイズが M,N からなる二部空グラフを作成する.
        """
        self.M=M; self.N=N
        self.edge=[[] for _ in range(M)]

    def add_edge(self, a, b):
        """ 辺 Aa Bb を加える.

        """
        assert 0<=a<self.M and 0<=b<self.N
        self.edge[a].append(b)

    def max_matching(self, mode):
        M=self.M; N=self.N; edge=self.edge
        pre=[-1]*M; root=[-1]*M
        p=[-1]*M; q=[-1]*N

        upd=True
        size=0
        while upd:
            upd=False
            S=[]
            Index=0

            for i in range(M):
                if p[i]==-1:
                    root[i]=i
                    S.append(i)

            while Index<len(S):
                v=S[Index]
                Index+=1

                if p[root[v]]!=-1:
                    continue

                for u in edge[v]:
                    if q[u]==-1:
                        while u!=-1:
                            q[u]=v
                            p[v],u=u,p[v]
                            v=pre[v]
                        upd=True
                        size+=1
                        break

                    u=q[u]
                    if pre[u]!=-1:
                        continue

                    pre[u]=v
                    root[u]=root[v]
                    S.append(u)

            if upd:
                pre=[-1]*M
                root=[-1]*M

        if mode==0:
            return size
        else:
            A=[-1]*M; B=[-1]*N
            for i in range(M):
                if p[i]!=-1:
                    A[i]=p[i]
                    B[p[i]]=i
            return size,(A,B)

#==================================================
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):
        G=Bipartite_Matching(N,N)
        for i in range(N):
            for j in range(N):
                if A[i][j]>=1:
                    G.add_edge(i,j)

        _,(P,_)=G.max_matching(True)

        for i in range(N):
            A[i][P[i]]-=1

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

    return

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