题目链接:Switches and Lamps
题意
有 n n 个开关和 盏灯,每个开关可以控制几个灯,这些开关只能将所控制的灯变亮,问能否从中去掉一个开关,仍能使得所有灯都亮起来。
输入
第一行为两个整数 n,m (1≤n,m≤2000) n , m ( 1 ≤ n , m ≤ 2000 ) ,接下去为 n n 行长度为 的 01 01 串,第 i i 行第 列为 1 1 表示第 个开关能够让第 j j 盏灯亮起来,为 表示该开关无法控制第 j j 盏灯,数据保证如果打开 个开关,则 m m 盏等一定会全都亮起来。
输出
如果可以达到要求就输出 ,否则输出 NO N O 。
样例
输入 |
---|
4 510101 01000 00111 10000 |
输出 |
YES |
输入 |
---|
4 510100 01000 00110 00101 |
输出 |
NO |
题解
先统计每盏灯被控制的开关数,暴力枚举每一个开关,如果某个开关控制的灯对应的开关数量小于等于 1 1 ,说明这盏灯被这个开关唯一控制,这个开关就不符合条件。只要存在一个开关满足条件,则输出 ,否则输出 NO N O 。
过题代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
#define LL long long
const int maxn = 2000 + 100;
int n, m;
bool flag;
char str[maxn][maxn];
int num[maxn];
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("test1.out", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while(scanf("%d%d", &n, &m) != EOF) {
flag = false;
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; ++i) {
scanf("%s", str[i] + 1);
for(int j = 1; j <= m; ++j) {
if(str[i][j] == '1') {
++num[j];
}
}
}
for(int i = 1; i <= n; ++i) {
bool f = true;
for(int j = 1; j <= m; ++j) {
if(str[i][j] == '1') {
if(num[j] <= 1) {
f = false;
break;
}
}
}
if(f) {
flag = true;
break;
}
}
if(flag) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}