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

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

vector<vector<int>> A;
void Min(int &a, int b) { a = min(a, b); }

bool DP() {
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  int n = A.size();
  for (int i = 0; i < n; i++)
    for (int j = 0, used[10] = {}; j < A[i].size(); j++)
      if (used[A[i][j]]++)
        return 1;

  vector<vector<int>> dp(1 << 10, vector<int>(1 << 10, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = (1 << 10) - 1; j >= 0; j--)
      for (int k = (1 << 10) - 1; k >= 0; k--) {
        if (dp[j][k] == INF || dp[j][k] == 10)
          continue;
        if ((j && k) && __builtin_popcount(j & k) == dp[j][k])
          return 1;

        for (int l = 0, nj = j; l < (int)A[i].size(); l++) {
          int num = A[i][l];
          if (l)
            Min(dp[nj][k | (1 << num)], dp[j][k] + 1);
          if (j & (1 << num))
            break;
          nj |= (1 << num);
          Min(dp[nj][k], dp[j][k] + 1);
        }
      }
  }

  for (int i = 1; i < (1 << 10); i++)
    for (int j = 1; j < (1 << 10); j++) {
      if (__builtin_popcount(i & j) == dp[i][j])
        return 1;
    }

  return 0;
}

typedef pair<int, int> P;
bool bfs() {
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  int n = A.size();
  for (int i = 0; i < n; i++)
    for (int j = 0, used[10] = {}; j < A[i].size(); j++)
      if (used[A[i][j]]++)
        return 1;

  queue<P> Q;
  vector<vector<int>> D(1 << 10, vector<int>(1 << 10, INF));
  D[0][0] = 0;
  Q.push(P(0, 0));

  while (!Q.empty()) {
    P t = Q.front();
    Q.pop();
    int I = t.first, J = t.second;
    if (I && J && __builtin_popcount(I & J) == D[I][J])
      return 1;
    if (D[I][J] == 10)
      continue;

    for (int i = 0; i < n; i++) {
      for (int l = 0, ni = I; l < (int)A[i].size(); l++) {
        int num = A[i][l];
        if (l && D[ni][J | (1 << num)] == INF) {
          D[ni][J | (1 << num)] = D[I][J] + 1;
          Q.push(P(ni, J | (1 << num)));
        }
        if (I & (1 << num))
          break;
        ni |= (1 << num);
        if (D[ni][J] == INF)
          D[ni][J] = D[I][J] + 1, Q.push(P(ni, J));
      }
    }
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n, n) {
    string str;
    cin >> str;
    A.clear();
    for (int i = 0; i < str.size(); i++) {
      A.push_back(vector<int>());
      while (str[i] != 'u')
        A.back().push_back(str[i++] - '0');
    }
    cout << (bfs() ? "UN" : "") << "SAFE" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

vector<vector<int>> A;
void Min(int &a, int b) { a = min(a, b); }

bool DP() {
  int n = A.size();
  for (int i = 0; i < n; i++)
    for (int j = 0, used[10] = {}; j < A[i].size(); j++)
      if (used[A[i][j]]++)
        return 1;

  vector<vector<int>> dp(1 << 10, vector<int>(1 << 10, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++)
    for (int j = (1 << 10) - 1; j >= 0; j--)
      for (int k = (1 << 10) - 1; k >= 0; k--) {
        if (dp[j][k] == INF || dp[j][k] == 10)
          continue;

        for (int l = 0, nj = j; l < (int)A[i].size(); l++) {
          int num = A[i][l];
          if (l)
            Min(dp[nj][k | (1 << num)], dp[j][k] + 1);
          if (j & (1 << num))
            break;
          nj |= (1 << num);
          Min(dp[nj][k], dp[j][k] + 1);
        }
      }

  for (int i = 1; i < (1 << 10); i++)
    for (int j = 1; j < (1 << 10); j++) {
      int c = __builtin_popcount(i & j);
      if (c == dp[i][j])
        return 1;
    }
  return 0;
}

int main() {
  int n;
  while (cin >> n, n) {
    string str;
    cin >> str;
    A.clear();
    for (int i = 0; i < str.size(); i++) {
      A.push_back(vector<int>());
      while (str[i] != 'u')
        A.back().push_back(str[i++] - '0');
    }
    cout << (DP() ? "UN" : "") << "SAFE" << endl;
  }
  return 0;
}