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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vpii = vector<pii>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
int cell[100001];
bool used[100001];
int moveto[100001];
int main() {
  int n;
  cin >> n;
  rep1(i, n) cin >> cell[i];
  rep1(i, n) {
    if (!cell[i]) {
      moveto[i] = i;
      continue;
    }
    if (moveto[i])
      continue;
    unordered_set<int> buf;
    int next = i;
    bool f;
    while (f = buf.insert(next).second) {
      next += cell[next];
      if (!cell[next])
        break;
      if (moveto[next]) {
        next = moveto[next];
        break;
      }
    }
    for (int a : buf)
      moveto[a] = f ? next : -1;
  }
  queue<pii> que;
  que.push(pii(1, 0));
  while (que.front().first != n) {
    pii a = que.front();
    que.pop();
    for (int i = 1; i < 7; i++) {
      int b = moveto[a.first + i];
      if (b != -1 && !used[b]) {
        que.push(pii(b, a.second + 1));
        used[b] = 1;
      }
    }
  }
  cout << que.front().second << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vpii = vector<pii>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
int cell[100000];
bool used[100000];
int moveto[100000];
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> cell[i];
  rep(i, n) {
    if (!cell[i]) {
      moveto[i] = i;
      continue;
    }
    if (moveto[i])
      continue;
    unordered_set<int> buf;
    int next = i;
    bool f;
    while (f = buf.insert(next).second) {
      next += cell[next];
      if (!cell[next])
        break;
      if (moveto[next]) {
        next = moveto[next];
        break;
      }
    }
    for (int a : buf)
      moveto[a] = f ? next : -1;
  }
  queue<pii> que;
  que.push(pii(0, 0));
  while (que.front().first != n - 1) {
    pii a = que.front();
    que.pop();
    for (int i = 1; i < 7; i++) {
      int b = moveto[a.first + i];
      if (b != -1 && !used[b]) {
        que.push(pii(b, a.second + 1));
        used[b] = 1;
      }
    }
  }
  cout << que.front().second << endl;
}