HDU - 2119 Matrix

Matrix

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3788 Accepted Submission(s): 1803

Problem Description
Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the ‘1’ in this row or this column .

Your task is to give out the minimum times of deleting all the ‘1’ in the matrix.

Input
There are several test cases.

The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.

n=0 indicate the end of input.

Output
For each of the test cases, in the order given in the input, print one line containing the minimum times of deleting all the ‘1’ in the matrix.

Sample Input
3 3
0 0 0
1 0 1
0 1 0
0

Sample Output
2

Author
Wendell

Source
HDU 2007-10 Programming Contest_WarmUp


二分图的最小点覆盖。

我们把横,竖分别看成点,然后如果某个点为1,就将横竖连起来。

最后看最小点覆盖(也就是最大匹配数)。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1010,M=2e4+10;
int n,m,mat[N],vis[N],res;
int head[N],nex[M],to[M],tot;
inline void add(int a,int b){
	to[++tot]=b; nex[tot]=head[a]; head[a]=tot;
}
int find(int x){
	for(int i=head[x];i;i=nex[i]){
		if(vis[to[i]])	continue;
		vis[to[i]]=1;
		if(!mat[to[i]]||find(mat[to[i]])){
			mat[to[i]]=x;	return 1;
		}
	}
	return 0;
}
signed main(){
	while(cin>>n,n){
		cin>>m;	tot=0;	memset(head,0,sizeof head);
		memset(mat,0,sizeof mat);	res=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				int x;	scanf("%d",&x);	if(x)	add(i,j);
			}
		}
		for(int i=1;i<=n;i++){
			memset(vis,0,sizeof vis);	
			if(find(i))	res++;
		}
		cout<<res<<endl;
	}
	return 0;
}
全部评论

相关推荐

01-23 14:54
同济大学 Java
热爱敲代码的程序媛:给你提几点【专业技能】这个模块里面可优化的地方:1.【具备JVM调优经验】可以去b站上搜一下JVM调优的视频,估计一两个小时凭你的学习能力就能掌握JVM调优的实践方面的技能。2.【MySql优化】MySql这一栏,你去b站或者找个博客看看MySql优化,学一下,如果你本身比较熟悉MySql语句的话,那基本半天时间凭你的学习能力MySql语句优化方面的技能你也能掌握个差不多。以上1,2两点主要是因为我看你专业技能大部分都说的是偏理论,没有写应用。再就是最后,你结合你的项目,想一想你的项目中哪些sql语句是可以用MySql优化的,到时候你面试的时候也好结合着说一下。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务