結果
問題 | No.2263 Perms |
ユーザー |
![]() |
提出日時 | 2023-04-07 21:17:14 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 151 ms / 2,000 ms |
コード長 | 1,385 bytes |
コンパイル時間 | 372 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 78,088 KB |
最終ジャッジ日時 | 2024-10-02 18:51:07 |
合計ジャッジ時間 | 5,552 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
import sys input = sys.stdin.readline class BipartiteMatching: def __init__(self, V): self.V = V self.G = [[] for _ in range(V)] def add_edge(self, u, v): self.G[u].append(v) self.G[v].append(u) def dfs(self, u): self.used[u] = True for v in self.G[u]: w = self.match[v] if w < 0 or (not self.used[w] and self.dfs(w)): self.match[u] = v self.match[v] = u return True return False def bipartite_matching(self): res = 0 self.match = [-1] * self.V for v in range(self.V): if self.match[v] == -1: self.used = [False] * self.V if self.dfs(v): res += 1 return res N, M = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] if any(sum(A[i][j] for j in range(N)) != M for i in range(N)): print(-1) exit() if any(sum(A[i][j] for i in range(N)) != M for j in range(N)): print(-1) exit() for _ in range(M): bm = BipartiteMatching(2*N) for i in range(N): for j in range(N): if A[i][j]: bm.add_edge(i, N+j) bm.bipartite_matching() ans = [0]*N for i in range(N): j = bm.match[i] - N A[i][j] -= 1 ans[i] = j+1 print(*ans)