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

#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 10001

struct Info {
  Info(int arg_have_lock, int arg_next) {
    have_lock = arg_have_lock;
    next = arg_next;
  }
  int have_lock, next;
};

int POW[11];
int length;
bool check[1024][10];
char buf[NUM];
vector<Info> V;

void func() {

  for (int i = 0; i < POW[10]; i++) {
    for (int k = 0; k < 10; k++)
      check[i][k] = false;
  }

  scanf("%s", buf);

  int have_lock = 0, next;
  V.clear();

  for (int i = 0; i < length; i++) {

    if (buf[i] == 'u') {

      have_lock = 0;

    } else {

      next = buf[i] - '0';

      if (have_lock & (POW[next])) {
        printf("UNSAFE\\n");
        return;
      }
      if (!check[have_lock][next]) {
        V.push_back(Info(have_lock, next));
        check[have_lock][next] = true;
      }
      have_lock += POW[next];
    }
  }

  vector<Info> ADD;
  int new_have_lock, new_next;
  bool FLG;

  while (true) {

    for (int i = 0; i < V.size(); i++) {
      for (int k = 0; k < V.size(); k++) {

        if (i == k || (V[i].have_lock & V[k].have_lock) != 0)
          continue;

        if ((POW[V[i].next] & V[k].have_lock) != 0 &&
            (POW[V[k].next] & V[i].have_lock) != 0) {
          printf("UNSAFE\\n");
          return;
        }

        FLG = false;

        if ((POW[V[i].next] & V[k].have_lock) != 0 &&
            (POW[V[k].next] & V[i].have_lock) == 0) {

          new_next = V[k].next;
          FLG = true;

        } else if ((POW[V[k].next] & V[i].have_lock) != 0 &&
                   (POW[V[i].next] & V[k].have_lock) == 0) {

          new_next = V[i].next;
          FLG = true;
        }

        if (FLG) {
          new_have_lock = V[i].have_lock | V[k].have_lock;
          if (!check[new_have_lock][new_next]) {
            ADD.push_back(Info(new_have_lock, new_next));
            check[new_have_lock][new_next] = true;
          }
        }
      }
    }

    if (ADD.size() == 0)
      break;

    for (int i = 0; i < ADD.size(); i++) {
      V.push_back(ADD[i]);
    }
    ADD.clear();
  }

  printf("SAFE\\n");
}

int main() {

  for (int i = 0; i < 11; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d", &length);
    if (length == 0)
      break;

    func();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_have_lock, int arg_next_lock) {
    have_lock = arg_have_lock;
    next_lock = arg_next_lock;
  }
  int have_lock, next_lock;
};

int length, POW[11];
bool check[1 << 10][10];
char buf[10001];

void func() {

  for (int state = 0; state < POW[10]; state++) {
    for (int next = 0; next < 10; next++)
      check[state][next] = false;
  }

  vector<Info> V, NEW;

  scanf("%s", buf);

  int state, next, k;

  // あり得る状態を列挙
  for (int i = 0; i < length;) {
    if (buf[i] != 'u') {
      state = 0;
      for (k = i; buf[k] != 'u'; k++) {
        next = buf[k] - '0';
        if (state & (1 << next)) {
          printf("UNSAFE\\n"); // 既に持っているデータをロックしようとしたらunsafe
          return;
        }

        if (!check[state][next]) { // 初出の状態
          check[state][next] = true;
          V.push_back(Info(state, next));
        }
        state += POW[next];
      }
      i = k + 1;
    }
  }

  int next_state;
  bool in_i, in_k;

  while (true) {

    // 状態同士で演算
    for (int i = 0; i < V.size() - 1; i++) {
      for (int k = i + 1; k < V.size(); k++) {
        if (V[i].have_lock & V[k].have_lock)
          continue; // 共通資源があったらSKIP

        // 次の資源の共有関係を見る
        if (V[k].have_lock & (1 << V[i].next_lock)) {
          in_k = true;
        } else {
          in_k = false;
        }

        if (V[i].have_lock & (1 << V[k].next_lock)) {
          in_i = true;
        } else {
          in_i = false;
        }

        if (in_i == true && in_k == true) {
          printf("UNSAFE\\n");
          return;
        }

        if (in_i == false && in_k == false)
          continue;

        next_state = V[i].have_lock | V[k].have_lock;
        if (in_i) {
          next = V[i].next_lock;
        } else {
          next = V[k].next_lock;
        }

        if (!check[next_state][next]) {
          check[next_state][next] = true;
          NEW.push_back(Info(next_state, next));
        }
      }
    }

    if (NEW.size() == 0)
      break;

    for (int i = 0; i < NEW.size(); i++)
      V.push_back(NEW[i]);
    NEW.clear();
  }

  printf("SAFE\\n");
}

int main() {

  for (int i = 0; i < 11; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d", &length);
    if (length == 0)
      break;

    func();
  }

  return 0;
}