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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

const int num = 10;

int main() {
  while (1) {
    vector<vector<int>> oks(1024, vector<int>(10, 0));
    int N;
    cin >> N;
    if (!N)
      break;
    string st;
    cin >> st;
    vector<vector<int>> vs;
    vector<int> v;
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (st[i] == 'u') {
        vs.emplace_back(v);
        v.clear();
      } else {
        v.emplace_back(st[i] - '0');
      }
    }
    for (auto v : vs) {
      if (v.empty())
        continue;
      bitset<10> bs;
      bs[v[0]] = true;
      oks[0][v[0]] = true;
      for (int i = 1; i < v.size(); ++i) {
        oks[bs.to_ulong()][v[i]] = true;
        if (bs[v[i]]) {
          ok = false;
        } else {
          bs[v[i]] = true;
        }
      }
    }
    for (int j = 0; j < 1024; ++j) {
      for (int k = 0; k < 10; ++k) {
        if (!oks[j][k])
          continue;
        for (int l = 0; l < 1024; ++l) {
          for (int m = 0; m < 10; ++m) {
            if ((!oks[j][k]) || (!oks[l][m]))
              continue;
            if (j & l)
              continue;
            if (!(j & (1 << m)) && (l & (1 << k)))
              oks[j | l][m] = true;
          }
        }
      }
    }
    for (int j = 0; j < 1024; ++j) {
      for (int k = 0; k < 10; ++k) {
        if (!oks[j][k])
          continue;
        for (int l = 0; l < 1024; ++l) {
          for (int m = 0; m < 10; ++m) {
            if ((!oks[j][k]) || (!oks[l][m]))
              continue;
            if (j & l)
              continue;
            if ((j & (1 << m)) && l & (1 << k)) {
              ok = false;
              break;
            }
          }
        }
      }
    }
    if (ok)
      cout << "SAFE" << endl;
    else
      cout << "UNSAFE" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

const int num = 10;

int main() {
  while (1) {
    vector<vector<int>> oks(1024, vector<int>(10, 0));
    int N;
    cin >> N;
    if (!N)
      break;
    string st;
    cin >> st;
    vector<vector<int>> vs;
    vector<int> v;
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (st[i] == 'u') {
        vs.emplace_back(v);
        v.clear();
      } else {
        v.emplace_back(st[i] - '0');
      }
    }
    for (auto v : vs) {
      if (v.empty())
        continue;
      bitset<10> bs;
      bs[v[0]] = true;
      oks[0][v[0]] = true;
      for (int i = 1; i < v.size(); ++i) {
        oks[bs.to_ulong()][v[i]] = true;
        if (bs[v[i]]) {
          ok = false;
        } else {
          bs[v[i]] = true;
        }
      }
    }
    for (int j = 0; j < 1024; ++j) {
      for (int k = 0; k < 10; ++k) {
        for (int l = 0; l < 1024; ++l) {
          for (int m = 0; m < 10; ++m) {
            if ((!oks[j][k]) || (!oks[l][m]))
              continue;
            if (j & l)
              continue;
            if (!(j & (1 << m)) && (l & (1 << k)))
              oks[j | l][m] = true;
          }
        }
      }
    }
    for (int j = 0; j < 1024; ++j) {
      for (int k = 0; k < 10; ++k) {
        for (int l = 0; l < 1024; ++l) {
          for (int m = 0; m < 10; ++m) {
            if ((!oks[j][k]) || (!oks[l][m]))
              continue;
            if (j & l)
              continue;
            if ((j & (1 << m)) && l & (1 << k)) {
              ok = false;
              break;
            }
          }
        }
      }
    }
    if (ok)
      cout << "SAFE" << endl;
    else
      cout << "UNSAFE" << endl;
  }
  return 0;
}