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

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

// #define DEBUG

int main() {
  ios::sync_with_stdio(false);
  typedef pair<bitset<10>, int> State;

  int N;
  while (cin >> N, N) {
#ifdef DEBUG
    cerr << "===============================" << endl;
#endif
    bool ans = true;
    vector<State> SS;
    bool memo[1 << 11][10] = {};

    string S;
    cin >> S;
    bitset<10> now(0);
    for (auto s : S) {
      if (s == 'u') {
        now = bitset<10>(0);
      } else {
        int l = s - '0';
        if (now.test(l)) {
          ans = false;
        }
        SS.emplace_back(now, l);
        memo[now.to_ulong()][l] = true;
        now.set(l);
      }
    }

    bool update = true;
    while (update && ans) {
#ifdef DEBUG
      cerr << "---------------------" << endl;
      for (auto s : SS)
        cerr << s.first << " " << s.second << endl;
#endif
      update = false;
      vector<State> nSS = SS;

      for (int i = 0; i < SS.size(); i++) {
        for (int j = i + 1; j < SS.size(); j++) {
          State s1 = SS[i], s2 = SS[j];
          if ((s1.first & s2.first).count() == 0) {
            if (s2.first.test(s1.second) && s1.first.test(s2.second)) {
              ans = false;
              goto END;
            } else if (s2.first.test(s1.second)) {
              State ns((s1.first | s2.first), s2.second);
              if (!memo[ns.first.to_ulong()][ns.second]) {
                nSS.push_back(ns);
                memo[ns.first.to_ulong()][ns.second] = true;
                update = true;
              }
            }
          }
        }
      }

      SS = nSS;
    }
  END:
    cout << (ans ? "SAFE" : "UNSAFE") << endl;
  }
}

Error: TLE

Fixed submission:

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

// #define DEBUG

int main() {
  ios::sync_with_stdio(false);
  typedef pair<bitset<10>, int> State;

  int N;
  while (cin >> N, N) {
#ifdef DEBUG
    cerr << "===============================" << endl;
#endif
    bool ans = true;
    vector<State> SS;

    string S;
    cin >> S;
    bitset<10> now(0);
    for (auto s : S) {
      if (s == 'u') {
        now = bitset<10>(0);
      } else {
        int l = s - '0';
        if (now.test(l)) {
          ans = false;
        }
        SS.emplace_back(now, l);
        now.set(l);
      }
    }

    bool update = true;
    while (update && ans) {
#ifdef DEBUG
      cerr << "---------------------" << endl;
      for (auto s : SS)
        cerr << s.first << " " << s.second << endl;
#endif
      update = false;
      vector<State> nSS = SS;

      for (int i = 0; i < SS.size(); i++) {
        for (int j = i + 1; j < SS.size(); j++) {
          State s1 = SS[i], s2 = SS[j];
          if ((s1.first & s2.first).count() == 0) {
            if (s2.first.test(s1.second) && s1.first.test(s2.second)) {
              ans = false;
              goto END;
            } else if (s2.first.test(s1.second)) {
              State ns((s1.first | s2.first), s2.second);
              if (find(SS.begin(), SS.end(), ns) == SS.end()) {
                nSS.push_back(ns);
                update = true;
              }
            }
          }
        }
      }

      SS = nSS;
    }
  END:
    cout << (ans ? "SAFE" : "UNSAFE") << endl;
  }
}