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

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  int e[128];
  int dp[2048][2048];
  int n, m, l;
  string s;
  while (1) {
    cin >> m >> n;
    if (m == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      cin >> s;
      e[i] = 0;
      l = s.length();
      for (int j = 0; j < l; j++) {
        if (s[l - j - 1] == '1')
          e[i] += (1 << j);
      }
    }
    for (int i = 0; i < (1 << m); i++) {
      for (int j = 0; j < (1 << m); j++) {
        dp[i][j] = 0;
      }
    }
    for (int i = 0; i < (1 << m); i++) {
      map<int, int> mp;
      for (int j = 0; j < n; j++) {
        if (mp.find(i & e[j]) == mp.end())
          mp[i & e[j]] = 1;
        else
          mp[i & e[j]]++;
      }
      for (auto it : mp) {
        if (it.second > 1) {
          dp[i][it.first] = 100;
        }
      }
    }
    for (int i = (1 << m) - 1; i >= 0; i--) {
      for (int j = 0; j < (1 << m); j++) {
        if (i ^ (i | j) == 0) {
          // cout << i SP j << endl;
          if (dp[i][j] == 0)
            continue;
          for (int k = 0; k < m; k++) {
            if (!(i & (1 << k))) {
              dp[i][j] = min(
                  dp[i][j],
                  max(dp[i | (1 << k)][j], dp[i | (1 << k)][j | (1 << k)]) + 1);
            }
          }
        }
        // cout << dp[i][j] << " ";
      }
      // cout << endl;
    }
    cout << dp[0][0] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  int e[128];
  int dp[2048][2048];
  while (1) {
    int n, m;
    cin >> m >> n;
    string s;
    vector<int> e(n);
    for (int i = 0; i < n; i++) {
      cin >> s;
      e[i] = stoi(s, NULL, 2);
    }
    for (int i = 0; i < (1 << m); i++) {
      for (int j = 0; j < (1 << m); j++) {
        dp[i][j] = 0;
      }
    }
    for (int i = 0; i < (1 << m); i++) {
      map<int, int> mp;
      for (int j = 0; j < n; j++) {
        if (mp.find(i & e[j]) == mp.end())
          mp[i & e[j]] = 1;
        else
          mp[i & e[j]]++;
      }
      for (auto it : mp) {
        if (it.second > 1) {
          dp[i][it.first] = 100;
        }
      }
    }
    for (int i = (1 << m) - 1; i >= 0; i--) {
      for (int j = 0; j < (1 << m); j++) {
        if (i ^ (i | j) == 0) {
          // cout << i SP j << endl;
          if (dp[i][j] == 0)
            continue;
          for (int k = 0; k < m; k++) {
            if (!(i & (1 << k))) {
              dp[i][j] = min(
                  dp[i][j],
                  max(dp[i | (1 << k)][j], dp[i | (1 << k)][j | (1 << k)]) + 1);
            }
          }
        }
        // cout << dp[i][j] << " ";
      }
      // cout << endl;
    }
    cout << dp[0][0] << endl;
  }
  return 0;
}