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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int MAXN = 1 << 11;
int dp[MAXN + 5][MAXN + 5];
int num[200];
int n, m;
/// dp[s1][s2]=min(dp[s1|1<<k][s2],dp[s1|1<<k][s2^(1<<k)])+1;
int dfs(int x, int y) {
  if (dp[x][y] != 1e9)
    return dp[x][y];
  int &ans = dp[x][y];
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    if ((num[i] & x) == y)
      cnt++;
  }
  if (cnt <= 1) {
    dp[x][y] = 0;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int z = (1 << i);
    if (x & z)
      continue;
    ans = min(ans, max(dfs(x | z, y), dfs(x | z, y ^ z)) + 1);
  }
  return ans;
}
int main() {
  // freopen("out.txt","w",stdout);
  while (scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      string str;
      cin >> str;
      num[i] = 0;
      for (int j = 0; j < n; j++) {
        if (str[j] == '1') {
          num[i] |= (1 << j);
        }
      }
      //  cout<<i<<" "<<num[i]<<endl;
    }
    // cout<<"!"<<endl;
    for (int i = 0; i < MAXN; i++) {
      for (int j = 0; j < MAXN; j++) {
        dp[i][j] = 1e9;
      }
    }
    cout << dfs(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define each(i, n) (int i = 1; i <= (n); ++i)
#define every(i, a, b) (int i = (a); i <= (b); ++i)
int n, nm;
// int ind[129];
int td[129];
// int nc1[2049*2];
int tdepth;
int search(int il, int ir, int in) {
  int tsd[129];
  /*cout<<"Search:"<<il<<","<<ir<<","<<in<<endl;
  for every(i,il,ir)
  {
          cout<<i<<":"<<td[i]<<endl;
  }*/
  if (il == ir)
    return 0;
  int zd, zn = 1 << in - 1;
  int res = 9999;
        for
          every(i, il + 1, ir) {
            if (td[i] == td[i - 1])
              return res;
          }
        for each (id, in) {
          zd = 1 << id - 1;
                for
                  every(i, il, ir) {
                    tsd[i] = td[i];
                    if ((bool(td[i] & zd)) xor bool(td[i] & zn)) {
                      td[i] ^= zd | zn;
                    }
                  }
                sort(td + il, td + ir + 1);
                if ((td[ir] & zn) && !(td[il] & zn))
                for
                  every(i, il, ir) {
                    if (td[i] & zn) {
                                for
                                  every(i, il, ir) td[i] &= (~zn);
                                /*cout<<"After Change:"<<endl;
                                for every(i,il,ir)
                                {
                                        cout<<i<<":"<<td[i]<<endl;
                                }*/
                                res =
                                    min(res, 1 + max(search(il, i - 1, in - 1),
                                                     search(i, ir, in - 1)));
                                break;
                    }
                  }
                for
                every(i, il, ir) { td[i] = tsd[i]; }
        }
        return res;
}
void task() {
        memset(td, 0, sizeof td);
        scanf("%d%d", &nm, &n);
        if (nm == 0 && n == 0)
                exit(0);
        int tc;
        for each (i, n) {
                for each (j, nm) {
                scanf("%1d", &tc);
                td[i] = td[i] * 2 + tc;
                }
                // cout<<ind[i]<<" ";
        }
        /*int nz=(1<<nm)-1;
        int minr=20;
        for every(iz,0,nz)
        {
                for each(i,n)
                {
                        td[i]=ind[i]&iz;
                }
                sort(td+1,td+n+1);
                for every(i,2,n)
                {
                        if(td[i]==td[i-1])goto die;
                }
                if(nc1[iz]<minr)cout<<iz<<endl;
                minr=min(minr,nc1[iz]);
                die:;
        }*/
        printf("%d\\n", search(1, n, nm));
}
int main() {
        /*#define lowbit(iv) ((iv)&-(iv))
        for each(i,1<<11)
        {
                nc1[i]=nc1[i^lowbit(i)]+1;
        }*/
        while (true)
                task();
}