結果

問題 No.2263 Perms
ユーザー chineristACchineristAC
提出日時 2023-04-07 21:43:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 2,639 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 82,876 KB
最終ジャッジ日時 2024-04-10 16:50:51
合計ジャッジ時間 6,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
56,704 KB
testcase_01 AC 48 ms
56,576 KB
testcase_02 AC 48 ms
56,576 KB
testcase_03 AC 49 ms
56,320 KB
testcase_04 AC 44 ms
56,320 KB
testcase_05 AC 68 ms
72,448 KB
testcase_06 AC 168 ms
81,840 KB
testcase_07 AC 163 ms
82,360 KB
testcase_08 AC 134 ms
81,216 KB
testcase_09 AC 171 ms
82,520 KB
testcase_10 AC 47 ms
62,720 KB
testcase_11 AC 182 ms
82,876 KB
testcase_12 AC 168 ms
82,260 KB
testcase_13 AC 177 ms
82,068 KB
testcase_14 AC 174 ms
82,092 KB
testcase_15 AC 50 ms
62,464 KB
testcase_16 AC 92 ms
77,940 KB
testcase_17 AC 97 ms
77,824 KB
testcase_18 AC 83 ms
77,940 KB
testcase_19 AC 99 ms
77,952 KB
testcase_20 AC 159 ms
81,620 KB
testcase_21 AC 154 ms
80,500 KB
testcase_22 AC 151 ms
80,604 KB
testcase_23 AC 117 ms
78,992 KB
testcase_24 AC 99 ms
78,552 KB
testcase_25 AC 169 ms
81,320 KB
testcase_26 AC 174 ms
82,248 KB
testcase_27 AC 121 ms
79,072 KB
testcase_28 AC 98 ms
78,896 KB
testcase_29 AC 104 ms
78,192 KB
testcase_30 AC 68 ms
73,600 KB
testcase_31 AC 55 ms
66,176 KB
testcase_32 AC 89 ms
78,072 KB
testcase_33 AC 87 ms
77,824 KB
testcase_34 AC 50 ms
62,976 KB
testcase_35 AC 42 ms
56,320 KB
testcase_36 AC 68 ms
73,856 KB
testcase_37 AC 52 ms
63,616 KB
testcase_38 AC 153 ms
81,956 KB
testcase_39 AC 45 ms
56,448 KB
testcase_40 AC 52 ms
56,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None

    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow

N,M = mi()
A = [li() for i in range(N)]

if any(sum(A[i])!=M for i in range(N)):
    exit(print(-1))
if any(sum(A[i][j] for i in range(N))!=M for j in range(N)):
    exit(print(-1))
    
res = []
for _ in range(M):
    G = Dinic(2+2*N)
    s,t = 2*N,2*N+1
    for i in range(N):
        G.add_edge(s,i,1)
        for j in range(N):
            G.add_edge(i,N+j,A[i][j])
    for j in range(N):
        G.add_edge(N+j,t,1)
    
    F = G.flow(s,t)
    if F!=N:
        exit(print(-1))
    
    P = [-1] * N
    for i in range(N):
        for e in G.G[i]:
            to,cap,_ = e
            if N <= to < 2*N:
                j = to - N
                if cap == A[i][j]-1:
                    P[i] = j+1
                    A[i][j] -= 1
    res.append(P)

for P in res:
    print(*P)

0