[CF] B. Switches and Lamps - Educational Codeforces Round 44 (Rated for Div. 2)

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

题目大意

给出 $n$ 个开关和 $m$ 盏灯,和每个开关可以点亮哪些灯的 $n \times m$ 的矩阵。其中 $1 \le n, m \le 2000$。灯被多个开关操作时,如果任意开关可以点亮它,则点亮它。

最初所有灯都是熄灭的。问是否可以去掉某一个开关,用其他开关点亮所有灯。

简要题解

直接枚举去掉的,再看其他的可能会超时。

保存一个开关前缀可以开的灯的集合和后缀可以开的集合即可。

复杂度

$T$:$O(nm / W)$: W 为 $bitset$ 节约的常数。

$S$:$O(nm)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

const int M = 2000;
typedef bitset<M> BS;

void solve() {
  int n, m; cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  if (n == 1) {
    cout << "NO\n";
    return;
  }

  vector<BS> l(n), r(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '1') {
        l[i].set(j);
        r[i].set(j);
      }
    }
  }

  for (int i = 1; i < n; i++) {
    l[i] |= l[i - 1];
  }
  for (int i = n - 2; i >= 0; i--) {
    r[i] |= r[i + 1];
  }

  if (l[n - 2].count() == m || r[1].count() == m) {
    cout << "YES\n";
    return;
  }

  for (int i = 0; i + 2 < n; i++) {
    if ((l[i] | r[i + 2]).count() == m) {
      cout << "YES\n";
      return;
    }
  }

  cout << "NO\n";
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] D. Sand Fortress - Educational Codeforces Round 44 (Rated for Div. 2)
Next: [CF] A. Chess Placing - Educational Codeforces Round 44 (Rated for Div. 2)