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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

vector<int> a;
int n, m;
vector<bool> d(300);

void saiki(vector<int> a, vector<bool> &u, int p) {
  if (u[p])
    return;
  u[p] = true;
  for (int i = 1; i <= m; i++) {
    int temp = min(n + 1, p + i);
    temp = max(min(temp + a[temp], n + 1), 0);
    saiki(a, u, temp);
  }
}

int main() {
  while (true) {
    fill(ALL(d), true);
    a = vector<int>(0);
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    a.PB(-300);
    REP(i, n) {
      int temp;
      cin >> temp;
      a.PB(temp);
    }
    a.PB(300);
    vector<bool> u(a.size(), false);
    saiki(a, u, 0);
    bool ans = true;
    if (!u[u.size() - 1])
      cout << "NG" << endl;
    else {
      u[u.size() - 1] = false;
      RREP(i, n + 2) {
        if (u[i]) {
          vector<bool> used(u.size(), false);
          queue<int> q;
          q.push(i);
          while (!q.empty()) {
            int x = q.front();
            q.pop();
            if (x == a.size() - 1) {
              u[x] = true;
              break;
            }
            for (int i = 1; i <= m; i++) {
              int nx = x + i;
              if (a.size() <= nx)
                nx = a.size() - 1;
              nx = min((int)(a.size() - 1), a[nx] + nx);
              nx = max(nx, 0);
              if (!used[nx]) {
                used[nx] = true;
                q.push(nx);
              }
            }
          }
          if (!used.back()) {
            ans = false;
            break;
          }
        }
      }
      if (ans)
        cout << "OK" << endl;
      else
        cout << "NG" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

vector<int> a;
int n, m;
vector<bool> d(300);

bool saiki(int p1, int pre, vector<int> u) {
  int p = p1;
  //  if(u[p] > 2) return true;
  u[p]++;
  bool ans = false;

  bool flg = false;
  for (int i = 1; i <= m; i++) {
    if (u[min(p + i, n + 1)] == 0)
      flg = true;
  }
  if (!flg)
    return true;
  for (int i = 1; i <= m; i++) {
    p = p1;
    p += i;
    p = min(p, n + 1);
    u[p]++;
    if (a[p] == 300)
      return d[p1] = false;
    p += a[p];
    p = min(p, n + 1);
    p = max(p, 0);
    if (u[p] < 50)
      ans = ans | saiki(p, p1, u);
  }
  return d[p] = ans;
}

int main() {
  while (true) {
    fill(ALL(d), true);
    a = vector<int>(0);
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    a.PB(-300);
    REP(i, n) {
      int temp;
      cin >> temp;
      a.PB(temp);
    }
    a.PB(300);
    if (!saiki(0, 0, vector<int>(n + 2, 0)))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}