結果

問題 No.2263 Perms
ユーザー kotatsugamekotatsugame
提出日時 2023-04-07 22:01:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,266 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 78,912 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 17:05:10
合計ジャッジ時間 2,632 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 5 ms
6,940 KB
testcase_22 AC 4 ms
6,944 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 4 ms
6,944 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 5 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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":" ");
		}
	}
}
0