1. 程式人生 > >Codeforce 985B 思維題開關燈問題

Codeforce 985B 思維題開關燈問題

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 aconsisting of n rows and m columns where a

i, 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個開關,每次按開關只能將燈從燈滅的狀態轉變為燈亮,問是否存在不按所有開關就將所有燈開啟的方法。

思路:最暴力的方法就是每層都去掉一次,然後再暴力每一列,這樣的複雜度為O(n^3),顯然是過不了的,然後另闢蹊徑:

可以開一個一維陣列存放每一列,也就是每一盞燈被多少開關控制的數目,然後再去列舉每一行,也就是每一個開關控制的燈,如果說e[i][j]==1&&cnt[j]==1,就是說第i個開關是控制第j盞燈,並且這一盞燈只有一個開關i控制,因此這一個開關是不能去掉的,那就遍歷下一個開關,以此類推……

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
int e[2005][2005];
int cnt[2005];
int main()
{
    int n, m;
    scanf("%d%d",&n,&m);
    rep(i,1,n)
    rep(j,1,m){
   scanf("%1d",&e[i][j]);if(e[i][j]==1) cnt[j]++;}
   int f=0;
   rep(i,1,n)
   {
       f=0;
       rep(j,1,m)
       if(e[i][j]==1&&cnt[j]==1)
       {
           f=1;break;
       }
       if(!f) {
        printf("YES\n");return 0;
       }
   }
   printf("NO\n");
    return 0;
}