結果

問題 No.2263 Perms
ユーザー とりゐとりゐ
提出日時 2023-04-04 17:32:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 3,855 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 82,084 KB
最終ジャッジ日時 2024-10-01 09:08:52
合計ジャッジ時間 6,492 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,424 KB
testcase_01 AC 43 ms
54,784 KB
testcase_02 AC 43 ms
54,528 KB
testcase_03 AC 42 ms
54,784 KB
testcase_04 AC 45 ms
54,784 KB
testcase_05 AC 69 ms
68,992 KB
testcase_06 AC 190 ms
80,600 KB
testcase_07 AC 179 ms
79,528 KB
testcase_08 AC 116 ms
78,184 KB
testcase_09 AC 201 ms
82,084 KB
testcase_10 AC 48 ms
61,828 KB
testcase_11 AC 182 ms
79,592 KB
testcase_12 AC 178 ms
79,612 KB
testcase_13 AC 188 ms
80,776 KB
testcase_14 AC 193 ms
80,332 KB
testcase_15 AC 45 ms
61,824 KB
testcase_16 AC 92 ms
76,800 KB
testcase_17 AC 112 ms
76,856 KB
testcase_18 AC 92 ms
76,880 KB
testcase_19 AC 113 ms
77,060 KB
testcase_20 AC 177 ms
79,936 KB
testcase_21 AC 189 ms
80,360 KB
testcase_22 AC 182 ms
80,264 KB
testcase_23 AC 138 ms
78,116 KB
testcase_24 AC 95 ms
77,312 KB
testcase_25 AC 184 ms
80,504 KB
testcase_26 AC 198 ms
80,592 KB
testcase_27 AC 142 ms
78,552 KB
testcase_28 AC 101 ms
77,328 KB
testcase_29 AC 124 ms
77,812 KB
testcase_30 AC 62 ms
66,816 KB
testcase_31 AC 57 ms
63,616 KB
testcase_32 AC 109 ms
76,928 KB
testcase_33 AC 97 ms
76,928 KB
testcase_34 AC 47 ms
55,952 KB
testcase_35 AC 43 ms
55,296 KB
testcase_36 AC 93 ms
76,848 KB
testcase_37 AC 48 ms
56,872 KB
testcase_38 AC 213 ms
80,636 KB
testcase_39 AC 44 ms
54,912 KB
testcase_40 AC 42 ms
54,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class mf_graph:
    n=1
    g=[[] for i in range(1)]
    pos=[]
    def __init__(self,N):
        self.n=N
        self.g=[[] for i in range(N)]
        self.pos=[]
    def add_edge(self,From,To,cap):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        assert 0<=cap
        m=len(self.pos)
        from_id=len(self.g[From])
        self.pos.append([From,from_id])
        to_id=len(self.g[To])
        if From==To:to_id+=1
        self.g[From].append([To,to_id,cap])
        self.g[To].append([From,from_id,0])
        return m
    def get_edge(self,i):
        m=len(self.pos)
        assert 0<=i and i<m
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e[0]][_e[1]]
        return [self.pos[i][0],_e[0],_e[2]+_re[2],_re[2]]
    def edges(self):
        m=len(self.pos)
        result=[]
        for i in range(m):
            a,b,c,d=self.get_edge(i)
            result.append((a,b,c,d))
        return result

    def change_edge(self,i,new_cap,new_flow):
        m=len(self.pos)
        assert 0<=i and i<m
        assert 0<=new_flow and new_flow<=new_cap
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e[0]][_e[1]]
        _e[2]=new_cap-new_flow
        _re[2]=new_flow
    def flow(self,s,t,flow_limit=(1<<63)-1):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        assert s!=t
        def bfs():
            level=[-1 for i in range(self.n)]
            level[s]=0
            que=deque([])
            que.append(s)
            while(que):
                v=que.popleft()
                for to,rev,cap in self.g[v]:
                    if cap==0 or level[to]>=0:continue
                    level[to]=level[v]+1
                    if to==t:return level
                    que.append(to)
            return level
        def dfs(v,up):
            if (v==s):return up
            res=0
            level_v=level[v]
            for i in range(Iter[v],len(self.g[v])):
                to,rev,cap=self.g[v][i]
                if (level_v<=level[to] or self.g[to][rev][2]==0):continue
                d=dfs(to,min(up-res,self.g[to][rev][2]))
                if d<=0:continue
                self.g[v][i][2]+=d
                self.g[to][rev][2]-=d
                res+=d
                if res==up:return res
            level[v]=self.n
            return res

        flow=0
        while(flow<flow_limit):
            level=bfs()
            if level[t]==-1:break
            Iter=[0 for i in range(self.n)]
            f=dfs(t,flow_limit-flow)
            if not(f):break
            flow+=f
        return flow
    def min_cut(self,s):
        visited=[False for i in range(self.n)]
        que=deque([])
        que.append(s)
        while(len(que)>0):
            p=que.popleft()
            visited[p]=True
            for to,rev,cap in self.g[p]:
                if cap and not(visited[to]):
                    visited[to]=True
                    que.append(to)
        return visited
      
n,m=map(int,input().split())
a=[]
for i in range(n):
  a.append(list(map(int,input().split())))

# check
assert 2<=n<=50 and 2<=m<=50
for i in range(n):
  for j in range(n):
    assert 0<=a[i][j]<=m


for i in range(n):
  sm=0
  for j in range(n):
    sm+=a[i][j]
  if sm!=m:
    exit(print(-1))
    
for j in range(n):
  sm=0
  for i in range(n):
    sm+=a[i][j]
  if sm!=m:
    exit(print(-1))

for _ in range(m):
  g=mf_graph(2*n+2)
  S=2*n
  T=2*n+1
  for i in range(n):
    g.add_edge(S,i,1)
    g.add_edge(i+n,T,1)
    for j in range(n):
      if a[i][j]>0:
        g.add_edge(i,n+j,1)
  g.flow(S,T,n)
  p=[-1]*n
  for e in g.edges():
    frm=e[0]
    to=e[1]-n
    flow=e[3]
    if 0<=frm<n and 0<=to<n and flow==1:
      p[frm]=to
      a[frm][to]-=1
  assert sorted(p)==list(range(n))
  print(*[i+1 for i in p])
0