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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  while (cin >> n, n) {
    string s;
    cin >> s;
    string ans = "SAFE";
    vector<set<int>> vsi(10);
    int memo = 0;
    REP(i, s.length()) {
      if (s[i] == 'u') {
        memo = 0;
      } else {
        if (((memo >> (s[i] - '0')) & 1)) {
          ans = "UNSAFE";
          break;
        }
        vsi[s[i] - '0'].insert(memo);
        memo |= 1 << (s[i] - '0');
      }
    }
    REP(k, 8)
    REP(i, 9)
    FOR(j, i + 1, 10)
    for (auto x1 : vsi[i])
      for (auto x2 : vsi[j]) {
        if (!(x1 & x2)) {
          if (((x1 >> j) & 1)) {
            vsi[i].insert(x1 | x2);
            if (((x1 | x2) >> i) & 1) {
              ans = "UNSAFE";
              goto finish;
            }
          }
          if ((x2 >> i) & 1) {
            vsi[j].insert(x1 | x2);
            if (((x1 | x2) >> j) & 1) {
              ans = "UNSAFE";
              goto finish;
            }
          }
        }
      }
    REP(i, 9)
    FOR(j, i + 1, 10)
    for (auto x1 : vsi[i])
      for (auto x2 : vsi[j]) {
        if (((x1 >> j) & 1) && ((x2 >> i) & 1) && !(x1 & x2)) {
          ans = "UNSAFE";
          break;
        }
      }
  finish:
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  while (cin >> n, n) {
    string s;
    cin >> s;
    string ans = "SAFE";
    vector<set<int>> vsi(10);
    int memo = 0;
    REP(i, s.length()) {
      if (s[i] == 'u') {
        memo = 0;
      } else {
        if (((memo >> (s[i] - '0')) & 1)) {
          ans = "UNSAFE";
          break;
        }
        vsi[s[i] - '0'].insert(memo);
        memo |= 1 << (s[i] - '0');
      }
    }
    REP(k, 8)
    REP(i, 9)
    FOR(j, i + 1, 10)
    for (auto x1 : vsi[i])
      for (auto x2 : vsi[j]) {
        if (!(x1 & x2)) {
          if (((x1 >> j) & 1))
            vsi[i].insert(x1 | x2);
          if ((x2 >> i) & 1)
            vsi[j].insert(x1 | x2);
        }
      }
    REP(i, 9)
    FOR(j, i + 1, 10)
    for (auto x1 : vsi[i])
      for (auto x2 : vsi[j]) {
        if (((x1 >> j) & 1) && ((x2 >> i) & 1) && !(x1 & x2)) {
          ans = "UNSAFE";
          break;
        }
      }
    cout << ans << endl;
  }
  return 0;
}