Matrix 二分图

来源:互联网 发布:中国体制 知乎 编辑:程序博客网 时间:2024/06/17 02:57

Matrix

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2234    Accepted Submission(s): 992
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 01 0 10 1 00 

Sample Output
2

题解:给你一个N*M的0/1矩阵,你每次可以选特定的某行或某列,然后删除该行/列的所有1,问你最少需要几次操作能删除矩阵的所有1.

最小点覆盖 = 最大匹配

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int n,m,ans=0;int link[101];int visit[101];int map[101][101];int dfs(int i){int j;for(j=1;j<=m;j++){if(map[i][j]==1 && visit[j]==0){visit[j]=1;if(link[j]==0 || dfs(link[j]==1)){ link[j]=1;//找到,加1 return 1;}}}return 0;}int main(){int x,y,i,j;scanf("%d%d",&n,&m);memset(map,0,sizeof(map));memset(link,0,sizeof(link));for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&map[i][j]);}}for(i=1;i<=n;i++){//从每个顶点出发,依次遍历 memset(visit,0,sizeof(visit));if(dfs(i)==1){//看是否能找到新的一对  ans++;}}printf("%d",ans);}