Switches and Lamps(cf 985B)

Description

You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix aconsisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp.

Initially all m lamps are turned off.

Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.

It is guaranteed that if you push all n switches then all m lamps will be turned on.

Your think that you have too many switches and you would like to ignore one of them.

Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps.

The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise.

It is guaranteed that if you press all n switches all m lamps will be turned on.

Output

Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.

Sample Input

4 5
10101
01000
00111
10000
Sample Output

YES
Sample Input

4 5
10100
01000
00110
00101
Sample  Output

NO

题解:有n个开关和m盏灯,对于一盏灯,只要存在一个控制这个灯的开关是开着的,这个灯就会被点亮。然后给你n×mn×m01矩阵,如果iijj列为1代表开关ii可以控制灯jj,问你能否删掉一个开关,使得所有的灯仍旧能被点亮。对于每一求和,如果对于某一行,如果去掉某一行后这一列的和会变成0,说明会有灯不受控制,这一行就不能删掉。

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define maxn 10007
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
using namespace std;
typedef long long ll;
stack<char>sk;
int a[2011][2011];
int main()
{
    int sum[2011],n,m;
    memset(sum,0,sizeof(sum));
    cin>>n>>m;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
             scanf("%1d",&a[i][j]);
            sum[j]+=a[i][j];
        }
	bool flag=0;
	for(int i=0,j; i<n; i++)
	{
		for(j=0; j<m; j++)
		{
			if(sum[j]-a[i][j]==0)
				break;
		}
		if(j>m-1)
			flag=1;
	}
	if(flag==1)
		printf("YES\n");
	else
		printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baiyi_destroyer/article/details/80803996
今日推荐