alexjercan/bugnet huggingface dataset:
Problem p00881 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <climits>
#include <cstring>
#include <iostream>
using namespace std;

int w, h;
short dp[1 << 11][1 << 11];
int bit[200];
bool t[200][200];

int solve(int q, int a) {
  if (dp[q][a] != -1)
    return dp[q][a];

  int cnt = 0;

  for (int i = 0; i < h; i++) {
    if ((bit[i] & q) == a) {
      cnt++;
      if (cnt > 1)
        break;
    }
  }

  if (cnt == 1) {
    return 0;
  }

  int res = INT_MAX;

  for (int i = 0; i < w; i++) {
    if (!(q & (1 << i))) {
      int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
      res = min(res, tmp);
    }
  }

  return dp[q][a] = res + 1;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      bit[i] = 0;

      for (int j = 0; j < w; j++) {
        char ch;
        cin >> ch;
        t[i][j] = ch - '0';
        bit[i] = (bit[i] << 1) + t[i][j];
      }
    }

    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <climits>
#include <cstring>
#include <iostream>
using namespace std;

int w, h, dp[3000][3000];
int bit[200];
bool t[200][200];

int solve(int q, int a) {
  if (dp[q][a] != -1)
    return dp[q][a];

  int cnt = 0;

  for (int i = 0; i < h; i++) {
    if ((bit[i] & q) == a) {
      cnt++;
      if (cnt > 1)
        break;
    }
  }

  if (cnt == 1) {
    return 0;
  }

  int res = INT_MAX;

  for (int i = 0; i < w; i++) {
    if (!(q & (1 << i))) {
      int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
      res = min(res, tmp);
    }
  }

  return dp[q][a] = res + 1;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      bit[i] = 0;

      for (int j = 0; j < w; j++) {
        char ch;
        cin >> ch;
        t[i][j] = ch - '0';
        bit[i] = (bit[i] << 1) + t[i][j];
      }
    }

    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0) << endl;
  }
}
Problem p00881 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>

using namespace std;

int mem[1 << 22];

int rec(int b, int *f, int *l) {
  if (l - f == 1) {
    return 0;
  } else if (mem[b] >= 0) {
    return mem[b];
  } else {
    int m = 99;
    for (int i = 0; i < 11; i++) {
      if (!(b >> i * 2 & 1)) {
        auto it = partition(f, l, [&](int x) { return x >> i & 1; });
        if (it != f && it != l) {
          m = min(m, 1 + max(rec(b | 3 << i * 2, f, it),
                             rec(b | 1 << i * 2, it, l)));
        }
      }
    }
    return mem[b] = m;
  }
}

int main() {
  for (int n, m; cin >> m >> n, m;) {
    int b[128];
    for (int i = 0; i < n; i++) {
      char s[12];
      cin >> s;
      b[i] = strtol(s, nullptr, 2);
    }
    fill(begin(mem), end(mem), -1);
    cout << rec(0, b, b + n) << endl;
  }
}

Error: TLE

Fixed submission:

// 48
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int m, n;
set<vector<int>> s[1 << 11];

int dfs(vector<int> v, int u, int d) {
  if (v.size() < 2) {
    return d;
  } else if (d < 5 && !s[u].insert(v).second) {
    return 99;
  } else {
    int mn = 99;
    for (int i = 0; i < m; i++) {
      if (!(u >> i & 1)) {
        vector<int> n[2];
        for (int j = 0; j < v.size(); j++) {
          n[v[j] >> i & 1].push_back(v[j]);
        }
        mn = min(mn, max(dfs(n[0], u | 1 << i, d + 1),
                         dfs(n[1], u | 1 << i, d + 1)));
      }
    }
    return mn;
  }
}

int main() {
  while (cin >> m >> n, m | n) {
    for (int i = 0; i < m; i++) {
      s[i].clear();
    }
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      char c[12];
      cin >> c;
      for (int j = 0; c[j]; j++) {
        a[i] = a[i] * 2 + c[j] - '0';
      }
    }
    cout << dfs(a, 0, 0) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int dp[178000], N, M, power3[12];
char c[130][11];
vector<int> vec[178000];

void init() {
  for (int i = 0; i < 178000; i++) {
    dp[i] = -1;
    vec[i].clear();
  }
  for (int i = 0; i < 130; i++) {
    for (int j = 0; j < 11; j++)
      c[i][j] = 0;
  }
}

int solve(int mask) {
  if (vec[mask].size() <= 1)
    return 0;
  if (dp[mask] != -1)
    return dp[mask];

  int bit[11];
  for (int i = 0; i < M; i++)
    bit[i] = (mask / power3[i]) % 3;

  int minx = (1 << 30);
  for (int i = 0; i < M; i++) {
    if (bit[i] != 0)
      continue;

    int v1 = solve(mask + power3[i] * 1);
    int v2 = solve(mask + power3[i] * 2);
    minx = min(minx, max(v1, v2) + 1);
  }
  dp[mask] = minx;
  return minx;
}

int main() {
  power3[0] = 1;
  for (int i = 1; i <= 11; i++)
    power3[i] = (power3[i - 1] * 3);
  while (true) {
    init();
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++)
        cin >> c[i][j];
    }

    for (int i = 0; i < (1 << M); i++) {
      vector<int> V;
      for (int j = 0; j < M; j++) {
        if ((i / (1 << j)) % 2 == 1)
          V.push_back(j);
      }

      for (int j = 0; j < N; j++) {
        int bit[11];
        for (int k = 0; k < 11; k++)
          bit[k] = 0;
        for (int k = 0; k < V.size(); k++) {
          if (c[j][V[k]] == '0')
            bit[V[k]] = 1;
          else
            bit[V[k]] = 2;
        }
        int rem = 0;
        for (int k = 0; k < M; k++)
          rem += power3[k] * bit[k];
        vec[rem].push_back(j);
      }
    }
    cout << solve(0) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n, m, a[128][12];
map<vector<int>, int> M;
int dfs(int p, vector<int> x) {
  if (x.size() == 1)
    return 0;
  if (M[x] >= 1)
    return M[x];
  int maxn = 1000;
  for (int i = 0; i < n; i++) {
    vector<int> J;
    for (int j : x) {
      if (a[j][i] == a[p][i])
        J.push_back(j);
    }
    if (J.size() != x.size())
      maxn = min(maxn, dfs(p, J));
  }
  M[x] = maxn + 1;
  return maxn + 1;
}
int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        char c;
        cin >> c;
        if (c == '0')
          a[i][j] = 0;
        else
          a[i][j] = 1;
      }
    }
    int maxn = 0;
    for (int i = 0; i < m; i++) {
      M.clear();
      vector<int> O;
      for (int j = 0; j < m; j++)
        O.push_back(j);
      maxn = max(maxn, dfs(i, O));
    }
    cout << maxn << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
vi v;
int m, n;
short dp[1 << 12][1 << 12];

int solve(int asked, int resp) {
  if (dp[asked][resp] >= 0)
    return dp[asked][resp];
  int cnt = 0;
  REP(i, n) {
    if ((v[i] & asked) == resp)
      cnt++;
  }
  int res = m;
  if (cnt <= 1) {
    res = 0;
  } else {
    REP(i, m) {
      int nx = asked | (1 << i);
      if (nx == asked)
        continue;
      chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
    }
  }
  return dp[asked][resp] = res;
}

int tonum(string s) {
  int res = 0;
  REP(i, s.size()) {
    res *= 2;
    res += s[i] - '0';
  }
  return res;
}

int main() {
  while (cin >> m >> n, m) {
    MS(dp, -1);
    v.resize(n);
    REP(i, n) {
      string s;
      cin >> s;
      v[i] = tonum(s);
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
vi v;
int m, n;
int dp[1 << 12][1 << 12];

int solve(int asked, int resp) {
  if (dp[asked][resp] >= 0)
    return dp[asked][resp];
  int cnt = 0;
  REP(i, n) {
    if ((v[i] & asked) == resp)
      cnt++;
  }
  int res = m;
  if (cnt <= 1) {
    res = 0;
  } else {
    REP(i, m) {
      int nx = asked | (1 << i);
      if (nx == asked)
        continue;
      chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
    }
  }
  return dp[asked][resp] = res;
}

int tonum(string s) {
  int res = 0;
  REP(i, s.size()) {
    res *= 2;
    res += s[i] - '0';
  }
  return res;
}

int main() {
  while (cin >> m >> n, m) {
    MS(dp, -1);
    v.resize(n);
    REP(i, n) {
      string s;
      cin >> s;
      v[i] = tonum(s);
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Runtime Error

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

int m, n;
int ans[150];

int dp[2050][2050]; // S?????????????????£??????

using Set = bitset<12>;

int rec(int asked,
        int answer) { // ??????????????????aked???answer??¨??????????????????
  int &res = dp[asked][answer];
  if (res != -1)
    return res;
  res = m;

  // vector<int> a;
  {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((ans[i] & asked) == answer) {
        ++cnt;
      }
    }
    if (cnt <= 1)
      return res = 0;
  }

  // cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
  // cout << "B ";
  // for(int &x : a) cout << Set(ans[x]) << ' ';
  // cout << endl;

  for (int i = 0; i < m; ++i) {
    if (asked >> i & 1)
      continue;
    int cnt[2] = {};
    for (int j = 0; j < n; ++j) {
      if ((ans[j] & asked) == answer) {
        ++cnt[ans[i] >> j & 1];
      }
    }
    // if(cnt[0] == 0 || cnt[1] == 0) continue;
    int tmp = 0;
    for (int j = 0; j < 2; ++j) {
      tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
    }
    res = min(res, tmp);
  }

  // assert(res != 1e9);
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> m >> n && m) {
    for (int i = 0; i < n; ++i) {
      char buf[100];
      cin >> buf;
      ans[i] = 0;
      for (int j = 0; j < m; ++j) {
        if (buf[j] - '0')
          ans[i] |= 1 << j;
      }
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0) << endl;
    // cout << "=================" << endl;
  }
}

Error: 0

Fixed submission:

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

int m, n;
int ans[15];

int dp[2050][2050]; // S?????????????????£??????

using Set = bitset<12>;

int rec(int asked,
        int answer) { // ??????????????????aked???answer??¨??????????????????
  int &res = dp[asked][answer];
  if (res != -1)
    return res;
  res = m;

  // vector<int> a;
  {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((ans[i] & asked) == answer) {
        ++cnt;
      }
    }
    if (cnt <= 1)
      return res = 0;
  }

  // cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
  // cout << "B ";
  // for(int &x : a) cout << Set(ans[x]) << ' ';
  // cout << endl;

  for (int i = 0; i < m; ++i) {
    if (asked >> i & 1)
      continue;
    int cnt[2] = {};
    for (int j = 0; j < n; ++j) {
      if ((ans[j] & asked) == answer) {
        ++cnt[ans[i] >> j & 1];
      }
    }
    // if(cnt[0] == 0 || cnt[1] == 0) continue;
    int tmp = 0;
    for (int j = 0; j < 2; ++j) {
      tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
    }
    res = min(res, tmp);
  }

  // assert(res != 1e9);
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> m >> n && m) {
    for (int i = 0; i < n; ++i) {
      char buf[100];
      cin >> buf;
      ans[i] = 0;
      for (int j = 0; j < m; ++j) {
        if (buf[j] - '0')
          ans[i] |= 1 << j;
      }
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0) << endl;
    // cout << "=================" << endl;
  }
}
Problem p00881 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int maxm = 11;
const int maxn = 128 + 10;
const int maxBit = (1 << maxm);
const int inf = 1000000000;

int n, m;
int array[maxn];

int f[maxBit][maxBit];
bool ok(int t1, int t2) {
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if ((array[i] & t1) == t2)
      res++;
  }
  return (res <= 1);
}
int dp(int haveAsked, int answer) {
  int t1 = haveAsked, t2 = answer;
  if (f[t1][t2] != -1)
    return f[t1][t2];

  f[t1][t2] = inf;

  if (ok(t1, t2))
    return f[t1][t2] = 0;

  for (int i = 0; i < m; ++i) {
    if (!(t1 & (1 << i)))
      f[t1][t2] =
          min(f[t1][t2],
              max(dp(t1 | (1 << i), t2), dp(t1 | (1 << i), t2 ^ (1 << i))) + 1);
  }

  return f[t1][t2];
}

int main() {
  static char s[maxm];
  while (scanf("%d %d", &m, &n) != EOF && n && m) {
    memset(f, -1, sizeof f);

    for (int i = 1; i <= n; ++i) {
      int res = 0;
      scanf(" %s", s);
      for (int j = 0; j < m; ++j) {
        if (s[j] == '1')
          res |= (1 << j);
      }

      array[i] = res;
    }

    cout << dp(0, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int maxL = 12;
const int maxn = 128 + 10;
const int maxBit = (1 << maxL);
const int inf = 1000000000;

int n, m;
int array[maxn];

int f[maxBit][maxBit];
bool ok(int t1, int t2) {
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if ((array[i] & t1) == t2)
      res++;
  }
  return (res <= 1);
}
int dp(int haveAsked, int answer) {
  int t1 = haveAsked, t2 = answer;
  if (f[t1][t2] != -1)
    return f[t1][t2];

  f[t1][t2] = inf;

  if (ok(t1, t2))
    return f[t1][t2] = 0;

  for (int i = 0; i < m; ++i) {
    if (!(t1 & (1 << i)))
      f[t1][t2] =
          min(f[t1][t2],
              max(dp(t1 | (1 << i), t2), dp(t1 | (1 << i), t2 ^ (1 << i))) + 1);
  }

  return f[t1][t2];
}

int main() {
  while (scanf("%d %d", &m, &n) != EOF && n && m) {
    memset(f, -1, sizeof f);

    for (int i = 1; i <= n; ++i) {
      int res = 0;
      string s;
      cin >> s;
      for (int j = 0; j < m; ++j) {
        if (s[j] == '1')
          res |= (1 << j);
      }

      array[i] = res;
    }

    cout << dp(0, 0) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<vector<int>, int> mem;
int dfs(vector<int> &A) {
  int n = A.size();
  if (n <= 1)
    return 0;
  if (n == 2)
    return 1;
  if (n == 3)
    return 2;
  if (mem.count(A))
    return mem[A];

  int res = A.size() - 1;
  for (int i = 0; i < w; i++) {
    vector<int> T, F;
    for (int j = 0; j < n; j++)
      (A[j] >> i & 1) ? T.push_back(A[j]) : F.push_back(A[j]);
    if (T.empty() || F.empty())
      continue;
    if (T.size() == 1 || F.size() == 1)
      continue;
    res = min(res, max(dfs(T), dfs(F)) + 1);
  }
  return mem[A] = res;
}

int BtoI(string a) {
  int res = 0;
  for (int i = 0; i < (int)a.size(); i++)
    res = res * 2 + a[i] - '0';
  return res;
}

int main() {

  while (1) {
    int n;
    cin >> w >> n;
    if (w == 0 && n == 0)
      break;
    vector<int> A;
    A.resize(n);
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      A[i] = BtoI(s);
    }
    mem.clear();
    cout << dfs(A) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

map<vector<int>, int> mem;
int dfs(vector<int> A) {
  int n = A.size();
  if (n <= 1)
    return 0;
  if (n == 2)
    return 1;
  if (n == 3)
    return 2;
  if (mem.count(A))
    return mem[A];

  int res = A.size() - 1;
  for (int i = 0; i < w; i++) {
    vector<int> T, F;
    for (int j = 0; j < n; j++)
      (A[j] >> i & 1) ? T.push_back(A[j]) : F.push_back(A[j]);
    if (T.empty() || F.empty())
      continue;
    if (T.size() == 1 || F.size() == 1)
      continue;
    res = min(res, max(dfs(T), dfs(F)) + 1);
  }
  return res;
}

int BtoI(string a) {
  int res = 0;
  for (int i = 0; i < (int)a.size(); i++)
    res = res * 2 + a[i] - '0';
  return res;
}

int main() {

  while (1) {
    int n;
    cin >> w >> n;
    if (w == 0 && n == 0)
      break;
    vector<int> A;
    A.resize(n);
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      A[i] = BtoI(s);
    }
    mem.clear();
    cout << dfs(A) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 130;
const int MAXB = 11;
const int MAXS = (1 << MAXB) + 5;
int m, n;
struct data {
  int ans;
} obj[MAXN];
short dp[MAXS][MAXS];
int dfs(int state, int now) {
  short &ret = dp[state][now];
  if (ret != -1)
    return ret;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if ((obj[i].ans & state) == now) {
      cnt++;
    }
  }
  if (cnt < 2)
    return ret = 0;
  ret = m;
  for (int i = 0; i < m; i++) {
    if (state >> i & 1)
      continue;
    ret = min((int)ret, 1 + max(dfs(state | (1 << i), now),
                                dfs(state | (1 << i), now | (1 << i))));
  }
  return ret;
}
int main() {
  while (cin >> m >> n && (n || m)) {
    string str;
    for (int i = 0; i < n; i++) {
      cin >> str;
      int ans = 0;
      for (int j = 0; j < m; j++) {
        ans = (ans << 1) | (str[j] == '1');
      }
      obj[i].ans = ans;
    }
    memset(dp, -1, sizeof dp);
    cout << dfs(0, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 130;
const int MAXB = 12;
const int MAXS = (1 << MAXB) + 5;
int m, n;
struct data {
  bool val[MAXB];
  int ans;
} obj[MAXN];
int dp[MAXS][MAXS];
int dfs(int state, int now) {
  int &ret = dp[state][now];
  if (ret != -1)
    return ret;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if ((obj[i].ans & state) == now) {
      cnt++;
    }
  }
  if (cnt < 2)
    return ret = 0;
  ret = m;
  for (int i = 0; i < m; i++) {
    if (state >> i & 1)
      continue;
    ret = min(ret, 1 + max(dfs(state | (1 << i), now),
                           dfs(state | (1 << i), now | (1 << i))));
  }
  return ret;
}
int main() {
  while (cin >> m >> n && (n || m)) {
    string str;
    for (int i = 0; i < n; i++) {
      cin >> str;
      int ans = 0;
      for (int j = 0; j < m; j++) {
        obj[i].val[j] = (str[j] == '1');
        ans = (ans << 1) | obj[i].val[j];
      }
      obj[i].ans = ans;
    }
    memset(dp, -1, sizeof dp);
    cout << dfs(0, 0) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int s[130];
int dp[4000][4000];
bool v[4000][4000];
int how[4000];
int n, m;
int dfs(int x, int y) {
  if (v[x][y])
    return dp[x][y];
  v[x][y] = 1;
  int ty = 0, u = -1;
  for (int i = 0; i < m; i++)
    if ((s[i] & x) == y) {
      int z = s[i] ^ y;
      how[z]++;
      if (z != u)
        ty++, u = z;
    }
  for (int i = 0; i < m; i++)
    if ((s[i] & x) == y)
      how[s[i] ^ y] = 0;
  if (ty == 1)
    return dp[x][y] = 0;
  else {
    int sum = 1000000;
    for (int i = 0; i < n; i++)
      if (((1 << i) & x) == 0)
        sum = min(sum, 1 + max(dfs((1 << i) | x, y),
                               dfs((1 << i) | x, (1 << i) | y)));
    return dp[x][y] = sum;
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n) {
    char f[130];
    for (int i = 0; i < m; i++) {
      int z = 0;
      scanf("%s", f);
      for (int j = 0; j < n; j++)
        z |= f[j] - '0' << j;
      s[i] = z;
    }
    int lim = 1 << n;
    for (int i = 0; i < lim; i++)
      for (int j = 0; j < lim; j++)
        v[i][j] = 0;
    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#define MAXN 111
using namespace std;
int n, m;
int dp[1 << 12][1 << 12];
int que[222];
int dfs(int state, int ans) {
  if (dp[state][ans] != -1)
    return dp[state][ans];
  int vis = 0;
  for (int i = 0; i < n; i++) {
    if ((state & que[i]) == ans)
      vis++;
  }
  if (vis <= 1)
    return dp[state][ans] = 0;
  int minn = 999999999;
  for (int i = 0; i < m; i++) {
    if (state & (1 << i))
      continue;
    minn = min(minn, max(dfs(state | (1 << i), ans),
                         dfs(state | (1 << i), ans | (1 << i))) +
                         1);
  }
  return dp[state][ans] = minn;
}

int main() {

  char s[22];
  while (scanf("%d%d", &m, &n) != EOF) {
    if (n == 0 && m == 0)
      break;
    memset(dp, -1, sizeof dp);
    memset(que, 0, sizeof que);
    for (int i = 0; i < n; i++) {
      scanf("%s", s);
      for (int j = 0; j < m; j++) {
        que[i] += (s[j] - '0') << (m - 1 - j);
      }
    }
    printf("%d\\n", dfs(0, 0));
  }
}