985B Switches and Lamps

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
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个灯,n行m列的01矩阵,1表示当前这个开关能够关联灯,可以通过开关,打开灯,0就不行。

可以确定的是所有开关打开灯都会亮。是否可以用n-1个开关,让所有灯亮。换句话说:去掉某一行,每一列至少有一个1就输出YES,否则就输出NO。

题解:模拟  对输入的所有列求和,如果某一行去掉,这一列和为0,就是不行,如果所有列的和都是大于0的就输出YES。

#include<bits/stdc++.h>
using namespace std;
int n,m,sum[2010],s[2010][2010];
char c;
int main()
{
    cin>>n>>m;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            cin>>c;
            s[i][j]=c-'0';
            if(s[i][j]) sum[j]+=1;
        }
    int j;
    for(int i=0; i<n; i++)
        for(j=0; j<m; j++)
        {
            if(sum[j]-s[i][j]==0) break;
            cout<<j<<" ";
            if(j==m-1)
            {
                puts("YES");
                return 0;
            }
        }
    puts("NO");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/80500747