結果
| 問題 |
No.2263 Perms
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-04-07 22:01:51 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 2,000 ms |
| コード長 | 1,266 bytes |
| コンパイル時間 | 1,041 ms |
| コンパイル使用メモリ | 78,264 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-02 19:31:29 |
| 合計ジャッジ時間 | 2,558 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
ソースコード
#include<iostream>
#include<cassert>
using namespace std;
#include<algorithm>
#include<vector>
struct bimatch{
int n;
vector<vector<int> >G;
vector<int>match;
vector<bool>used;
bimatch(int _n=0):n(_n),G(n),match(n),used(n){}
void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v)
{
used[v]=true;
for(int u:G[v])
{
int w=match[u];
if(w<0||!used[w]&&dfs(w))
{
match[v]=u;
match[u]=v;
return true;
}
}
return false;
}
int count()
{
int ans=0;
bool flag=true;
fill(match.begin(),match.end(),-1);
while(flag)
{
flag=false;
fill(used.begin(),used.end(),false);
for(int v=0;v<n;v++)if(!used[v]&&match[v]<0&&dfs(v))ans++,flag=true;
}
return ans;
}
};
int N,M;
int A[50][50];
int dH[50],dW[50];
int main()
{
cin>>N>>M;
for(int i=0;i<N;i++)for(int j=0;j<N;j++)
{
cin>>A[i][j];
dH[i]+=A[i][j];
dW[j]+=A[i][j];
}
for(int i=0;i<N;i++)
{
if(dH[i]!=M||dW[i]!=M)
{
cout<<-1<<endl;
return 0;
}
}
for(int _=0;_<M;_++)
{
bimatch P(N+N);
for(int i=0;i<N;i++)for(int j=0;j<N;j++)if(A[i][j]>0)P.add_edge(i,N+j);
assert(P.count()==N);
for(int i=0;i<N;i++)
{
int j=P.match[i]-N;
assert(A[i][j]-->0);
cout<<j+1<<(i+1==N?"\n":" ");
}
}
}