Codeforces-985B - Switches and Lamps - 思维

91 篇文章 0 订阅

题解链接

https://www.lucien.ink/archives/233/


题目链接

http://codeforces.com/contest/985/problem/B


题目

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.


题意

  有n个开关和m盏灯,对于一盏灯,只要存在一个控制这个灯的开关是开着的,这个灯就会被点亮。然后给你 n×m n × m 01矩阵,如果 i i j列为1代表开关 i i 可以控制灯j,问你能否删掉一个开关,使得所有的灯仍旧能被点亮。


思路

  对于每一求和,如果对于某一行,如果去掉某一行后这一列的和会变成0,说明会有灯不受控制,这一行就不能删掉。


实现

#include <bits/stdc++.h>
const int maxn = 2007;
int map[maxn][maxn], sum[maxn], n, m;
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) {
        scanf("%1d", map[i] + j);
        sum[j] += map[i][j];
    }
    for (int i = 1, j; i <= n; i++) {
        for (j = 1; j <= m; j++) if (sum[j] - map[i][j] == 0) break;
        if (j > m) return 0 * puts("YES");
    }
    return 0 * puts("NO");
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值