Codeforces 985B (bitset)

传送门

题面:

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 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盏灯。对于每个开关,若第Cij==0,则当第i个开关开的时候,第j盏灯不开,反之Cij==1,则开。问你打开其中的n-1个开关,能否使m盏灯都打开。

题目分析:

    对于这个题目,因为n和m的数据范围均为2000,因此可以判断出n方的算法是可以过的。而因为我们要处理的串都是01串,因此我们可以用bitset进行比较简单的处理。

    对于bitset,它可以看作一个多位的二进制数,每8位占用1个字节,相当于采用了状态压缩的二进制数组,并支持基本的位运算。对于bitset的时间复杂度,一般以32位整数的位运算位基准,n位的bitset执行一次位运算的复杂度可视为n/32。

    对于整体上看,我们首先枚举需要删除哪个数,再进行n次操作,整体的时间复杂度大约在n^2/32左右,符合题意。

代码:

#include <bits/stdc++.h>
using namespace std;
bitset<2005>bit[2005];
char c[2005][2005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        bit[i].reset();
    }
    for(int i=0;i<n;i++){
        scanf("%s",c[i]);
        for(int j=0;j<m;j++){
            if(c[i][j]=='1'){
                bit[i].set(j,1);
            }
        }
    }
    bitset<2005>res;
    for(int i=0;i<n;i++){
        res.reset();
        for(int j=0;j<n;j++){
            if(i==j) continue;
            res=res|bit[j];
            if(res.count()==m){
                puts("YES");
                return 0;
            }
        }
    }
    puts("NO");
    return 0;
}

posted @ 2018-05-22 13:40  ChenJr  阅读(292)  评论(0编辑  收藏  举报