B. Switches and Lamps
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 a consisting 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.
Examples
Input
Copy
4 5
10101
01000
00111
10000
Output
Copy
YES
Input
Copy
4 5
10100
01000
00110
00101
Output
Copy
NO
题目大意:
输入n,m,n代表开关的个数,m代表灯的个数
就是给你一个矩阵,然后a[i][j]代表第i个灯能够控制第j个灯,然后问题在去掉一个特定灯的条件下,能不能使剩下的灯还全部能够被控制,如果有这样的一盏灯,就输出yes,如果没有,就输出no。
正确代码:
#include<iostream>
#include<cstring>
using namespace std;
char a[2010][2010];//这个地方一开始定义的是int型,还各种傻逼debug。。。。
int main()
{
int b[2010];
memset(b,0,sizeof(b));
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>a[i][j];
b[j]+=a[i][j]-'0';//把每一列的值打进数组b中
}
}
int t=1;
int flag=0;
int j;
for(int i=1; i<=n; i++)
{
int k=1;
for(j=1; j<=m; j++)
{
if((b[j]-(a[i][j]-'0'))==0)break;//灵魂判断,最核心的地方,如果某一组列的总和减去某一行中该列对应的为0,就说明只有这一组灯能控制第j个灯
}
if(j==m+1) flag=1;//如果每一列都走完了还没有问题,就可以说明这个灯能被去掉
}
if(flag==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}