結果

問題 No.2263 Perms
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-04-07 23:46:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 3,299 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 82,000 KB
実行使用メモリ 81,356 KB
最終ジャッジ日時 2024-10-02 20:49:29
合計ジャッジ時間 6,015 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,084 KB
testcase_01 AC 43 ms
56,156 KB
testcase_02 AC 44 ms
54,720 KB
testcase_03 AC 44 ms
55,708 KB
testcase_04 AC 44 ms
54,672 KB
testcase_05 AC 62 ms
67,336 KB
testcase_06 AC 171 ms
79,720 KB
testcase_07 AC 159 ms
79,364 KB
testcase_08 AC 124 ms
78,432 KB
testcase_09 AC 175 ms
81,356 KB
testcase_10 AC 46 ms
61,112 KB
testcase_11 AC 148 ms
78,372 KB
testcase_12 AC 159 ms
78,960 KB
testcase_13 AC 177 ms
80,136 KB
testcase_14 AC 178 ms
79,944 KB
testcase_15 AC 48 ms
61,544 KB
testcase_16 AC 93 ms
76,924 KB
testcase_17 AC 106 ms
77,280 KB
testcase_18 AC 96 ms
76,940 KB
testcase_19 AC 105 ms
77,028 KB
testcase_20 AC 167 ms
79,760 KB
testcase_21 AC 165 ms
79,480 KB
testcase_22 AC 161 ms
79,680 KB
testcase_23 AC 127 ms
77,784 KB
testcase_24 AC 86 ms
77,332 KB
testcase_25 AC 181 ms
79,952 KB
testcase_26 AC 169 ms
79,772 KB
testcase_27 AC 129 ms
78,496 KB
testcase_28 AC 93 ms
77,088 KB
testcase_29 AC 117 ms
77,300 KB
testcase_30 AC 67 ms
69,884 KB
testcase_31 AC 62 ms
66,880 KB
testcase_32 AC 95 ms
77,184 KB
testcase_33 AC 92 ms
76,708 KB
testcase_34 AC 47 ms
56,728 KB
testcase_35 AC 43 ms
54,936 KB
testcase_36 AC 73 ms
73,020 KB
testcase_37 AC 51 ms
63,036 KB
testcase_38 AC 162 ms
79,928 KB
testcase_39 AC 43 ms
54,752 KB
testcase_40 AC 44 ms
54,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class MaxFlow:
    inf = 10**18

    class E:
        def __init__(self,to,cap):
            self.to = to
            self.cap = cap
            self.rev = None

    def __init__(self,n):
        self.n = n
        self.graph = [[] for _ in range(n)]

    def add_edge(self, fr, to, cap):
        graph = self.graph
        edge = self.E(to,cap)
        edge2 = self.E(fr,0)
        edge.rev = edge2
        edge2.rev = edge
        graph[fr].append(edge)
        graph[to].append(edge2)

    def bfs(self, s, t):
        level = self.level = [self.n]*self.n
        q = deque([s])
        level[s] = 0
        while q:
            now = q.popleft()
            lw = level[now]+1
            for e in self.graph[now]:
                if e.cap and level[e.to]> lw:
                    level[e.to] = lw
                    if e.to == t:
                        return True
                    q.append(e.to)
        return False

    def dfs(self, s, t, up):
        graph = self.graph
        it = self.it
        level = self.level

        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = graph[w][it[w]].rev
                    flow = min(flow, e.cap)
                for w in st:
                    e = graph[w][it[w]]
                    e.cap += flow
                    e.rev.cap -= flow
                return flow
            lv = level[v]-1
            while it[v] < len(graph[v]):
                e = graph[v][it[v]]
                re = e.rev
                if re.cap == 0 or lv != level[e.to]:
                    it[v] += 1
                    continue
                st.append(e.to)
                break
            if it[v] == len(graph[v]):
                st.pop()
                level[v] = self.n

        return 0

    def flow(self,s,t,flow_limit=inf):
        flow = 0
        while flow < flow_limit and self.bfs(s,t):
            self.it = [0]*self.n
            while flow < flow_limit:
                f = self.dfs(s,t,flow_limit-flow)
                if f == 0:
                    break
                flow += f
        return flow

    def min_cut(self,s):
        visited = [0]*self.n
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = 1
            for e in self.graph[v]:
                if e.cap and not visited[e.to]:
                    q.append(e.to)
        return visited

n,m = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]


for i in range(n):
    r = 0
    c = 0
    for j in range(n):
        r += A[i][j]
        c += A[j][i]
    if r != m or c != m:
        print(-1)
        exit()


ans = []

for _ in range(m):
    s = n**2
    t = s+1
    mf = MaxFlow(t+1)
    for i in range(n):
        for j in range(n):
            if A[i][j]:
                mf.add_edge(i,n+j,1)
    
    for i in range(n):
        mf.add_edge(s,i,1)
        mf.add_edge(i+n,t,1)

    mf.flow(s,t)

    l = [0]*n
    for i in range(n):
        for e in mf.graph[i]:
            if e.cap:
                continue
            to = e.to - n
            l[i] = to + 1
            A[i][to] -= 1
    ans.append(l)

for i in ans:
    print(*i)
0