hdu 2119

Matrix

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2205    Accepted Submission(s): 975

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

//最小顶点覆盖=最大匹配

#include <stdio.h>
#include <string.h>

int ma[110][110];
int n,m;
bool vis[110];
int link[110];

bool Find(int x)
{
    for(int i=0;i<m;i++)
    {
        if(!vis[i]&&ma[x][i])
        {
            vis[i]=1;
            if(link[i]==-1||Find(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        memset(link,-1,sizeof(link));
        for(int i=0;i<n;i++)
            for(int k=0;k<m;k++)
                scanf("%d",&ma[i][k]);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-27 22:16:01

hdu 2119的相关文章

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

hdu 2119 Matrix

Matrix http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Give you a matrix(only contains 0 or 1),every time you can select a row or a column and dele

hdu 2119 Matrix(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2205    Accepted Submission(s): 975 Problem Description Give you a matrix(only contains

(匈牙利算法) hdu 2119

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> using namespace std; int n,m,a[110][110],g[110][110],mark[110],link[110],ans; bool dfs(i

HDU——2119 Matrix

    题目大意: 给你一个矩阵(只包含0或1),每次你可以选择一行或一列,并删除这一行或这个列中的所有“1”.您的任务是给出删除矩阵中所有“1”的最小时间. 思路: 我们要将所有的1都删去,那样我们对于1的位置的行与列连边,再把这两个(列和边)看作是两个集合.求它的最大匹配数. 一个很简单的二分图的板子.. 代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream>

poj 2226 Muddy Fields(合理建图+二分匹配)

1 /* 2 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 3 思路: 4 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1,然后让我们通过选择一行,或者 5 是一列将其所在行的或者所在列的 1全部删掉,求出最少需要几步? 6 7 这道题的思路就是:将行标 和 列标值为1的建立一条边!通过匈牙利算法可以得到这个二分图的最大匹配数 8 最大匹配数==最小顶点覆盖数!最小顶点覆盖就是用最少的点覆盖了这个二分图

ACM-二分搜索之Can you solve this equation?——hdu2199

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7507    Accepted Submission(s): 3490 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y

二分图相关题

HDU 1281 由于每行最多放一个,每列最多放一个(不能放置的位置不影响攻击,就是由于没注意这句话,把这题当做行列覆盖模型做了好久0.0) 所以把行列直接当做二分图X和Y集.能够放置的点的行列连边,求出的完备匹配就是第二个答案. 至于第一个答案求关键点,就枚举删除一条边是否能任然得到完备匹配,若不行,则是关键点. 我的代码c++会WA,不知道为什么.求教啊. #include<cstdio> #include<cstring> #include<algorithm>

HDU——T 2119 Matrix

http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3097    Accepted Submission(s): 1429 Problem Description Give you a matrix(only contains 0 or 1),ev