Problem p00881 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

int M, N;
vector<string> sts;

int memo[1 << 12][128];

int getans(const vector<int> vs, bitset<11> used) {

  if (memo[used.to_ulong()][vs[0]] != -1)
    return memo[used.to_ulong()][vs[0]];
  if (vs.size() == 1) {
    return memo[used.to_ulong()][vs[0]] = 0;
  } else {
    int ans = 11;
    for (int ask = 0; ask < M; ++ask) {
      if (!used[ask]) {
        vector<int> yess, nos;
        for (const auto v : vs) {
          if (sts[v][ask] == '1')
            yess.push_back(v);
          else
            nos.push_back(v);
        }
        if (nos.empty() || yess.empty())
          continue;
        used[ask] = true;
        ans = min(ans, 1 + max(getans(yess, used), getans(nos, used)));
        used[ask] = false;
      }
    }
    return memo[used.to_ulong()][vs[0]] = ans;
  }
}
int main() {
  while (1) {
    for (int i = 0; i < (1 << 11); ++i) {
      for (int j = 0; j < 128; ++j) {
        memo[i][j] = -1;
      }
    }
    cin >> M >> N;
    if (!M)
      break;
    sts.clear();
    for (int i = 0; i < N; ++i) {
      string st;
      cin >> st;
      sts.push_back(st);
    }
    vector<int> a(N);
    iota(a.begin(), a.end(), 0);
    int aans = getans(a, bitset<11>(0));
    cout << aans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

int M, N;
vector<string> sts;
int ans = 11;

int getans(const vector<int> vs, bitset<11> used) {
  if (vs.size() == 1)
    return 0;
  else {
    int ans = 11;
    for (int ask = 0; ask < M; ++ask) {
      if (!used[ask]) {
        vector<int> yess, nos;
        for (const auto v : vs) {
          if (sts[v][ask] == '1')
            yess.push_back(v);
          else
            nos.push_back(v);
        }
        if (yess.size() == vs.size() || yess.empty())
          continue;
        used[ask] = true;
        ans = min(ans, 1 + max(getans(yess, used), getans(nos, used)));
        used[ask] = false;
      }
    }
    return ans;
  }
}
int main() {
  while (1) {
    ans = 11;
    /*for (int i = 0; i < (1 << 11); ++i) {
            for (int j = 0; j < 11; ++j) {
                    memo[i][j] = -1;
            }
    }*/
    cin >> M >> N;
    if (!M)
      break;
    sts.clear();
    for (int i = 0; i < N; ++i) {
      string st;
      cin >> st;
      sts.push_back(st);
    }
    /*for (int i = 0; i < (1 << M); ++i) {
            bitset<11>bs(i);
            set<int>sums;
            for (int j = 0; j < N; ++j) {
                    int sum = 0;
                    int now = 1;
                    for (int l = 0; l < M; ++l) {
                            if (bs[l]) {
                                    sum += now*(sts[j][l] == '1');
                                    now *= 2;
                            }
                    }
                    sums.emplace(sum);
            }
            if (sums.size() == N) {
                    ans = min(ans, int(bs.count()));
            }
    }*/
    vector<int> a(N);
    iota(a.begin(), a.end(), 0);
    ans = getans(a, bitset<11>(0));
    cout << ans << endl;
  }
  return 0;
}