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

//???????????????
//???mnway=cnt???????????????cnt=mnway?????????????????????
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
const int MX = 100;
typedef bitset<MX> BS;
int H, W, N;
int ans;
bool used[MX];
vector<BS> cs;
bool incl(BS &a, BS &b) { return (a & b) == b; }
void dfs(BS now, int t) {
  if (ans <= t)
    return;
  if (now.none()) {
    chmin(ans, t);
    return;
  }
  int mnway = MX, arg = -1;
  rep(i, H * W) if (now[i]) {
    int cnt = 0;
    rep(j, N) if (!used[j]) {
      if (cs[j][i])
        cnt++;
    }
    if (cnt < mnway) {
      mnway = cnt;
      arg = i;
    }
  }
  assert(arg >= 0);
  rep(i, N) if (!used[i] && cs[i][arg]) {
    used[i] = 1;
    dfs(now & (~cs[i]), t + 1);
    used[i] = 0;
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    cs.clear();
    BS s;
    rep(i, H) rep(j, W) {
      bool x;
      cin >> x;
      s.set(i * W + j, x);
    }
    rep(i, H) rep(j, W) {
      for (int d = 10; d >= 1; d--) {
        bool can = 1;
        if (i + d - 1 >= H || j + d - 1 >= W)
          can = 0;
        if (can) {
          rep(x, d) rep(y, d) if (!s[(i + x) * W + j + y]) can = 0;
        }
        if (can) {
          BS b;
          rep(x, d) rep(y, d) b.set((i + x) * W + j + y);
          cs.pb(b);
          break;
        }
      }
    }
    N = cs.size();
    bool gomi[MX] = {};
    rep(i, N) rep(j, N) if (i != j) {
      if (incl(cs[i], cs[j]))
        gomi[j] = 1;
    }
    vector<BS> tmp;
    rep(i, N) if (!gomi[i]) tmp.pb(cs[i]);
    cs = tmp;
    N = cs.size();
    //		rep(i,N) show(cs[i]);
    ans = N;
    rep(i, N) used[i] = 0;
    dfs(s, 0);
    //		show(cnt);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

//???????????????
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
const int MX = 100;
typedef bitset<MX> BS;
int H, W, N;
int ans;
bool used[MX];
vector<BS> cs;
bool incl(BS &a, BS &b) { return (a & b) == b; }
int cnt;
void dfs(BS now, int t) {
  //	cnt++;
  //	printf("dfs");
  //	show(now);
  //	show(t);
  if (ans <= t)
    return;
  if (now.none()) {
    chmin(ans, t);
    return;
  }
  int mnway = MX, arg = -1;
  rep(i, H * W) if (now[i]) {
    int cnt = 0;
    rep(j, N) if (!used[j]) {
      if (cs[j][i])
        cnt++;
    }
    if (cnt < mnway) {
      cnt = mnway;
      arg = i;
    }
  }
  assert(arg >= 0);
  rep(i, N) if (!used[i] && cs[i][arg]) {
    used[i] = 1;
    dfs(now & (~cs[i]), t + 1);
    used[i] = 0;
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    cs.clear();
    BS s;
    rep(i, H) rep(j, W) {
      bool x;
      cin >> x;
      s.set(i * W + j, x);
    }
    rep(i, H) rep(j, W) {
      for (int d = 10; d >= 1; d--) {
        bool can = 1;
        if (i + d - 1 >= H || j + d - 1 >= W)
          can = 0;
        if (can) {
          rep(x, d) rep(y, d) if (!s[(i + x) * W + j + y]) can = 0;
        }
        if (can) {
          BS b;
          rep(x, d) rep(y, d) b.set((i + x) * W + j + y);
          cs.pb(b);
          break;
        }
      }
    }
    N = cs.size();
    bool gomi[MX] = {};
    rep(i, N) rep(j, N) if (i != j) {
      if (incl(cs[i], cs[j]))
        gomi[j] = 1;
    }
    vector<BS> tmp;
    rep(i, N) if (!gomi[i]) tmp.pb(cs[i]);
    cs = tmp;
    N = cs.size();
    //		rep(i,N) show(cs[i]);
    ans = N;
    rep(i, N) used[i] = 0;
    dfs(s, 0);
    //		show(cnt);
    cout << ans << endl;
  }
}