B. Switches and Lamps
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
4 5
10101
01000
00111
10000
outputCopy
YES
inputCopy
4 5
10100
01000
00110
00101
outputCopy
NO
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
using namespace std;
int main()
{
char s[20001];
int i,j,n,m;
vector<int>w[20001];
map<int,int>p;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",s);
for(j=0;j<m;j++)
{
if(s[j]=='1')
{
w[i].push_back(j);
p[j]++;
}
}
}//找到每个开关控制几个电灯,每个电灯被几个开关控制
int flag=1;int flag2;
for(i=0;i<n;i++)
{
flag2=1;
for(j=0;j<w[i].size();j++)
{
if(p[w[i][j]]==1)
{
flag2=0;
break;
}
}
if(flag2==1)
{
flag=0;
break;
}
}
if(flag==0)
{
printf("YES\n");
}
else printf("NO\n");
return 0;
}
题意:给你n个开关,m个灯泡,再给你一个矩阵,矩阵上如果是1就表明这个开关控制这个灯泡,现在让我们判断去掉哪个开关后灯泡仍然可以全部点亮。
思路:既然这样的话,那就只能是先判断出开关控制哪几个灯泡,(就是利用vector把控制的灯泡的编号输进去),再对他控制的这几个灯泡进行判断,是不是只有这一个开关控制它,(那就需要一个map来记录每个灯泡被几个开关控制了),对每个开关进行判断,如果在这一行出现了灯泡是只有他控制的,那这一行就不行了,看下一行,如果这一行都遍历完了,都没有出现这样的灯泡就说明这一行是可以去掉的,用两个标记变量进行控制即可;