alexjercan/bugnet huggingface dataset:
Problem p01448 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

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

int main() {
  int N;
  cin >> N;
  vector<int> c(100000 + 2, 0);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    c[a - 1]++;
    c[b]--;
  }
  int sum = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    sum += c[i];
    if (i <= sum)
      ans = i;
  }
  cout << ans << endl;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

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

int main() {
  int N;
  cin >> N;
  vector<int> c(N + 1, 0);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    c[a - 1]++;
    c[b]--;
  }
  int sum = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    sum += c[i];
    if (i <= sum)
      ans = i;
  }
  cout << ans << endl;
}
Problem p01448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int dp[100010] = {};

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (int t = a; t < b + 1; t++) {
      dp[t]++;
    }
  }

  int mx = 0;
  for (int i = n + 1; i > 1; i--) {
    if (dp[i] >= i - 1) {
      mx = i - 1;
      break;
    }
  }
  cout << mx << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int dp[100010] = {};

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    if (n < b) {
      b = n;
    }
    for (int t = a; t <= b; i++) {
      dp[t]++;
    }
  }

  int mx = 0;
  for (int i = n; i > 0; i--) {
    if (dp[i] >= i) {
      mx = i;
      break;
    }
  }
  cout << mx << endl;

  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> que;

int p[100500];
char tbl[100500];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &p[i]);
  priority_queue<que> q;
  q.push(que(-0, 0));
  while (!q.empty()) {
    que t = q.top();
    q.pop();
    int cost = -t.fst;
    int i = t.snd;

    tbl[i] = 1;
    if (i == n - 1) {
      printf("%d\\n", cost);
      break;
    }
    if (p[i]) {
      if (0 <= i + p[i] && i + p[i] < n && !tbl[i + p[i]])
        q.push(que(-cost, i + p[i]));
    } else
      for (int dice = 1; dice <= 6; dice++)
        if (i + dice < n && !tbl[i + dice])
          q.push(que(-(cost + 1), i + dice));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> que;

int p[100500];
char tbl[100500];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &p[i]);
  priority_queue<que> q;
  q.push(que(-0, 0));
  while (!q.empty()) {
    que t = q.top();
    q.pop();
    int cost = -t.fst;
    int i = t.snd;

    tbl[i] = 1;
    if (i == n - 1) {
      printf("%d\\n", cost);
      break;
    }
    if (p[i]) {
      if (0 <= i + p[i] && i + p[i] < n && !tbl[i + p[i]])
        q.push(que(-cost, i + p[i]));
    } else
      for (int dice = 1; dice <= 6; dice++)
        if (i + dice < n)
          q.push(que(-(cost + 1), i + dice));
  }
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int INF = 1 << 28;
vector<int> road;
vector<int> dp;
int solve(int p, set<int> &same) {
  if (dp[p] != -2)
    return dp[p];
  if (same.count(p))
    return -1;
  same.insert(p);
  if (road[p] == 0) {
    dp[p] = p;
    return p;
  }
  return dp[p] = solve(p + road[p], same);
}
int main() {
  int n;
  cin >> n;
  road = vector<int>(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> road[i];
  }
  dp = vector<int>(n, -2);
  for (int i = 0; i < n; i++) {
    set<int> same;
    solve(i, same);
  }

  int res = 0;
  queue<pair<int, int>> que;
  que.push(pair<int, int>(dp[0], 0));
  vector<int> used(n, INF);
  while (!que.empty()) {
    pair<int, int> tmp = que.front();
    que.pop();
    int pos = tmp.first;
    int cnt = tmp.second;

    if (pos == n - 1) {
      cout << cnt << endl;
      return 0;
    }

    for (int i = 1; i <= 6; i++) {
      if (pos + i == n - 1) {
        cout << cnt + 1 << endl;
        return 0;
      }
      if (dp[pos + i] != -1 && used[pos + i] > cnt + 1) {
        used[pos + i] = cnt + 1;
        que.push(pair<int, int>(dp[pos + i], cnt + 1));
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
vector<int> road;
vector<int> dp;
int solve(int p, set<int> &same) {
  if (dp[p] != -2)
    return dp[p];
  if (same.count(p))
    return -1;
  same.insert(p);
  if (road[p] == 0) {
    dp[p] = p;
    return p;
  }
  return dp[p] = solve(p + road[p], same);
}
int main() {
  int n;
  cin >> n;
  road = vector<int>(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> road[i];
  }
  dp = vector<int>(n, -2);
  for (int i = 0; i < n; i++) {
    set<int> same;
    solve(i, same);
  }

  int res = 0;
  queue<pair<int, int>> que;
  que.push(pair<int, int>(dp[0], 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front();
    que.pop();
    int pos = tmp.first;
    int cnt = tmp.second;
    if (pos == n - 1) {
      cout << cnt << endl;
      return 0;
    }

    for (int i = 1; i <= 6; i++) {
      if (pos + i == n - 1) {
        cout << cnt + 1 << endl;
        return 0;
      }
      if (dp[pos + i] != -1) {
        que.push(pair<int, int>(dp[pos + i], cnt + 1));
      }
    }
  }

  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  int n;
  cin >> n;
  VI p(n);
  REP(i, n) cin >> p[i];
  VI dist(n, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (dist[n - 1] == -1) {
    int x = que.front();
    que.pop();
    REP(i, 6) {
      int next = min(n - 1, x + i + 1);
      while (1) {
        if (dist[next] >= 0)
          break;
        dist[next] = dist[x] + 1;
        if (p[next] == 0) {
          que.push(next);
          break;
        }
        next += p[next];
      }
    }
  }

  cout << dist[n - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

VI p, e;

int dfs(int i) {
  e[i] = -1;
  if (p[i] == 0) {
    return e[i] = i;
  }
  if (e[i + p[i]] == -1) {
    return -1;
  }
  return e[i] = dfs(i + p[i]);
}

int main() {
  int n;
  cin >> n;
  p.resize(n);
  e.resize(n);
  REP(i, n) cin >> p[i];
  REP(i, n) {
    if (e[i] != 0)
      continue;
    if (p[i] == 0)
      e[i] = i;
    else
      dfs(i);
  }
  VI dist(n, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (dist[n - 1] == -1) {
    int x = que.front();
    que.pop();
    REP(i, 6) {
      int next;
      if (x + i + 1 >= n)
        next = n;
      else
        next = e[x + i + 1];
      if (next == -1 || dist[next] >= 0)
        continue;
      dist[next] = dist[x] + 1;
      que.push(next);
    }
  }

  cout << dist[n - 1] << endl;

  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+9
using namespace std;

typedef pair<int, int> P;

int main() {
  int n, p[100010] = {}, d[100010];
  bool used[100010] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n + 6; i++)
    d[i] = INF;
  d[0] = 0;
  used[0] = true;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P pp = que.top();
    que.pop();
    int v = pp.second;
    if (v >= n)
      continue;
    if (d[v] < pp.first)
      continue;
    for (int i = 1; i <= 6; i++) {
      if (d[v + i] > d[v] + 1) {
        if (p[v + i]) {
          int pos = v + i;
          bool f = false;
          while (p[pos]) {
            pos += p[pos];
            if (used[pos]) {
              f = true;
              break;
            }
            used[pos] = true;
          }
          if (f)
            continue;
          if (d[pos] > d[v] + 1) {
            d[pos] = d[v] + 1;
            que.push(P(d[pos], pos));
          }
        } else {
          d[v + i] = d[v] + 1;
          used[v + i] = true;
          que.push(P(d[v + i], v + i));
        }
      }
    }
  }
  // for(int i = 0;i < n;i++) cout << d[i] << endl;
  cout << d[n - 1] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+9
using namespace std;

typedef pair<int, int> P;

int main() {
  int n, p[100010] = {}, d[100010];
  bool used[100010] = {};
  cin >> n;
  for (int i = 0; i < 100010; i++) {
    cin >> p[i];
    d[i] = INF;
  }
  d[0] = 0;
  used[0] = true;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P pp = que.top();
    que.pop();
    int v = pp.second;
    if (d[v] < pp.first)
      continue;
    for (int i = 1; i <= 6; i++) {
      if (d[v + i] > d[v] + 1) {
        if (p[v + i]) {
          int pos = v + i;
          bool f = false;
          while (p[pos]) {
            pos += p[pos];
            if (used[pos]) {
              f = true;
              break;
            }
            used[pos] = true;
          }
          if (f)
            continue;
          if (d[pos] > d[v] + 1) {
            d[pos] = d[v] + 1;
            que.push(P(d[pos], pos));
          }
        } else {
          d[v + i] = d[v] + 1;
          used[v + i] = true;
          que.push(P(d[v + i], v + i));
        }
      }
    }
  }
  // for(int i = 0;i < n;i++) cout << d[i] << endl;
  cout << d[n - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <iostream>
using namespace std;

// for "to" table
#define INF -1
#define LOOP -2

int n;
int t[100010];
int to[100010];
int dp[100010];

int dfs(int idx) {
  if (to[idx] != INF) {
    return to[idx];
  }
  if (to[idx] == LOOP) {
    return LOOP;
  }
  to[idx] = LOOP;

  int next = idx + t[idx];
  return to[idx] = dfs(next);
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> t[i];

      if (t[i] == 0) {
        to[i] = i;
      } else {
        to[i] = INF;
      }

      dp[i] = INT_MAX;
    }

    for (int i = 0; i < n; i++) {
      if (to[i] == INF) {
        to[i] = dfs(i + t[i]);
      }
    }

    dp[0] = 0;

    for (int i = 0; i < 10; i++) {
      for (int idx = 0; idx < n; idx++) {
        if (dp[idx] == INT_MAX) {
          continue;
        }

        for (int plus = 1; plus <= 6; plus++) {
          if (idx + plus >= n) {
            continue;
          }

          int next = to[idx + plus];

          if (next == LOOP) {
            continue;
          }

          dp[next] = min(dp[next], dp[idx] + 1);
        }
      }
    }

    cout << dp[n - 1] << endl;
  }
}

Error: 0

Fixed submission:


#include <climits>
#include <iostream>
using namespace std;

// for "to" table
#define INF -1
#define LOOP -2

int n;
int t[100010];
int to[100010];
int dp[100010];

int dfs(int idx, int start) {
  if (to[idx] != INF) {
    return to[idx];
  }
  if (idx == start) {
    return LOOP;
  }

  int next = idx + t[idx];
  return to[idx] = dfs(next, start);
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> t[i];

      if (t[i] == 0) {
        to[i] = i;
      } else {
        to[i] = INF;
      }

      dp[i] = INT_MAX;
    }

    for (int i = 0; i < n; i++) {
      if (to[i] == INF) {
        to[i] = dfs(i + t[i], i);
      }
    }

    dp[0] = 0;

    for (int i = 0; i < 2; i++) {
      for (int idx = 0; idx < n; idx++) {
        if (dp[idx] == INT_MAX) {
          continue;
        }

        for (int plus = 1; plus <= 6; plus++) {
          if (idx + plus > n) {
            continue;
          }

          int next = to[idx + plus];

          if (next == LOOP) {
            continue;
          }

          dp[next] = min(dp[next], dp[idx] + 1);
        }
      }
    }

    cout << dp[n - 1] << endl;
  }
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define fi first
#define sc second
using namespace std;
typedef pair<int, int> P;

int n;
int a[100000];
int to[100000];
int d[100000];
vector<int> e[100000];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, INF);
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.fi > d[p.sc])
      continue;
    rep(i, e[p.sc].size()) {
      if (d[e[p.sc][i]] > d[p.sc] + 1) {
        d[e[p.sc][i]] = d[p.sc] + 1;
        que.push(P(d[e[p.sc][i]], e[p.sc][i]));
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", a + i);
  memset(to, -1, sizeof(to));
  rep(i, n) {
    if (to[i] != -1)
      continue;
    int j = i;
    set<int> s;
    vector<int> v;
    s.insert(j);
    v.push_back(j);
    while (a[j] != 0) {
      j = j + a[j];
      if (to[j] != -1) {
        j = to[j];
        break;
      }
      if (s.count(j)) {
        j = -2;
        break;
      }
      s.insert(j);
      v.push_back(j);
    }
    rep(k, v.size()) to[v[k]] = j;
  }
  rep(i, n) {
    for (int j = 1; j <= 6; j++) {
      if (i + j >= n)
        break;
      if (to[i + j] == -2)
        continue;
      e[i].push_back(to[i + j]);
    }
  }
  dijkstra();
  cout << d[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define fi first
#define sc second
using namespace std;
typedef pair<int, int> P;

int n;
int a[100000];
int to[100000];
int d[100000];
vector<int> e[100000];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, INF);
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.fi > d[p.sc])
      continue;
    rep(i, e[p.sc].size()) {
      if (d[e[p.sc][i]] > d[p.sc] + 1) {
        d[e[p.sc][i]] = d[p.sc] + 1;
        que.push(P(d[e[p.sc][i]], e[p.sc][i]));
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", a + i);
  memset(to, -1, sizeof(to));
  rep(i, n) {
    if (to[i] != -1)
      continue;
    int j = i;
    set<int> s;
    vector<int> v;
    s.insert(j);
    v.push_back(j);
    while (a[j] != 0) {
      j = j + a[j];
      if (to[j] != -1) {
        j = to[j];
        break;
      }
      if (s.count(j)) {
        j = -1;
        break;
      }
      s.insert(j);
      v.push_back(j);
    }
    rep(k, v.size()) to[v[k]] = j;
  }
  rep(i, n) {
    for (int j = 1; j <= 6; j++) {
      if (i + j >= n)
        break;
      if (to[i + j] == -1)
        continue;
      e[i].push_back(to[i + j]);
    }
  }
  dijkstra();
  cout << d[n - 1] << endl;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int BAN = 1 << 25;
typedef struct P {
  int p, t, f;
  bool operator<(const P &a) const { return t == a.t ? p < a.p : t > a.t; };
} P;
int N, M[100010], f[100010], np;
int main() {
  memset(f, 0x77, sizeof(f));
  f[0] = 0;
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &M[i]);
  priority_queue<P> Q;
  P t = {0, 0, 0};
  Q.push(t);
  for (; !Q.empty();) {
    t = Q.top();
    Q.pop();
    if (t.p >= N - 1)
      break;
    if (t.t > f[t.p])
      continue;
    if (!t.f)
      for (int i = 1; i <= 6; i++) {
        np = t.p + i;
        if (f[np] > t.t + 1) {
          f[np] = t.t + 1;
          P ins = {np, t.t + 1, M[np] ? 1 : 0};
          Q.push(ins);
        }
      }
    else {
      np = t.p + M[t.p];
      if (f[np] > t.t) {
        f[np] = t.t;
        P ins = {np, t.t, M[np] ? 1 : 0};
        Q.push(ins);
      }
    }
  }
  printf("%d\\n", t.t);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int BAN = 1 << 25;
typedef struct P {
  int p, t;
  bool operator<(const P &a) const { return t == a.t ? p < a.p : t > a.t; };
} P;
int N, M[100010], f[100010];
int main() {
  memset(f, 0x77, sizeof(f));
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &M[i]);
  for (int i = 0; i < N - 1; i++)
    for (int j = 1, np, c; i + j < N && j <= 6; j++)
      if (M[i + j] != BAN) {
        for (c = 0, np = i + j + M[i + j];
             c < 100005 && M[np] && M[np] != BAN && np != i + j;
             np += M[np], c++)
          ;
        if (c == 100005 || M[np] && (np == i + j || M[np] == BAN))
          M[i + j] = BAN;
      }
  priority_queue<P> Q;
  P t = {0, 0};
  Q.push(t);
  for (; !Q.empty();) {
    t = Q.top();
    Q.pop();
    if (t.p >= N - 1)
      break;
    if (t.t > f[t.p] || M[t.p] == BAN)
      continue;
    for (int i = 1, np; i <= 6; i++) {
      for (np = t.p + i; M[np] && M[np] != BAN; np += M[np])
        ;
      if (M[np] != BAN && np > t.p && f[np] > t.t + 1) {
        f[np] = t.t + 1;
        P ins = {np, t.t + 1};
        Q.push(ins);
      }
    }
  }
  printf("%d\\n", t.t);
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100010;
const int inf = (1 << 29);
int p[N], memo[N], visited[N], res[N], n;
int func(int x) {
  if (x >= n)
    return n - 1;
  if (memo[x] < inf)
    return memo[x];
  if (p[x] == 0)
    return memo[x] = x;
  if (visited[x])
    return memo[x] = -1;
  visited[x] = 1;
  int nx = x + p[x];
  nx = max(0, min(nx, n - 1));
  return memo[x] = func(nx);
}
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i)
      scanf("%d", p + i);
    for (int i = 0; i <= n; ++i)
      memo[i] = res[i] = inf;
    memset(visited, 0, sizeof(memo));
    queue<int> q;
    q.push(0);
    res[0] = 0;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      if (x >= n - 1)
        break;
      for (int i = 1; i <= 6; ++i) {
        int nx = func(x + i);
        if (res[nx] < inf)
          continue;
        q.push(nx);
        res[nx] = res[x] + 1;
      }
    }
    printf("%d\\n", res[n - 1]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, p[100010], r[100010];
const int inf = (1 << 29);
int main() {
  while (~scanf("%d", &n)) {
    memset(r, -1, sizeof(r));
    for (int i = 0; i < n; ++i)
      scanf("%d", p + i);
    queue<int> q;
    q.push(0);
    r[0] = 0;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      if (x >= n - 1)
        break;
      for (int i = 1; i <= 6; ++i)
        if (0 < x + i && x + i < n && r[x + i] < 0) {
          int k = x + i;
          if (p[k] == 0 && r[k] < 0) {
            q.push(k);
            r[k] = r[x] + 1;
            continue;
          }
          queue<int> t;
          t.push(k);
          bool loop = false;
          while (p[k] != 0) {
            k = max(0, min(k + p[k], n - 1));
            if (k == x + i || r[k] == inf) {
              loop = true;
              break;
            }
            t.push(k);
          }
          if (p[k] == 0 && r[k] < 0)
            q.push(k);
          while (!t.empty()) {
            if (r[t.front()] < 0)
              r[t.front()] = loop ? inf : r[x] + 1;
            t.pop();
          }
        }
    }
    // for(int i=0;i<n;++i)printf("%d ",r[i]);puts("");
    printf("%d\\n", r[n - 1]);
  }
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define inf 10101010
typedef pair<int, int> pii;

int main(void) {
  int n, ans;
  cin >> n;
  vector<int> g(n);
  vector<int> g2(n, -1);
  for (int i = 0; i < n; i++)
    cin >> g[i];
  set<int> se;
  for (int i = 0; i < n; i++) {
    if (g[i] == 0)
      continue;
    if (se.count(i))
      continue;
    set<int> s;
    int j = i;
    while (g[j] != 0) {
      if (g2[j] != -1) {
        j = g2[j];
        break;
      }
      if (s.count(j) || se.count(j)) {
        se.insert(s.begin(), s.end());
        break;
      }
      s.insert(j);
      j += g[j];
    }
    if (!se.count(j)) {
      for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        g2[*it] = j;
      }
    }
  }
  vector<int> dp(n, inf);
  dp[0] = 0;

  priority_queue<pii, vector<pii>, greater<pii>> que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int cnt = p.first, pos = p.second;
    if (pos >= n - 1) {
      ans = cnt;
      break;
    }
    for (int i = 1; i < 7; i++) {
      if (se.count(pos + i))
        continue;
      int t = pos + i;
      if (g2[t] != -1)
        t = g2[t];
      if (dp[t] > cnt + 1) {
        dp[t] = cnt + 1;
        que.push(make_pair(cnt + 1, t));
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
#define inf 10101010

int main(void) {
  int n;
  cin >> n;
  vector<int> g(n);
  vector<int> g2(n, -1);
  for (int i = 0; i < n; i++)
    cin >> g[i];
  set<int> se;
  for (int i = 0; i < n; i++) {
    if (g[i] == 0)
      continue;
    if (se.count(i))
      continue;
    set<int> s;
    int j = i;
    while (g[j] != 0) {
      if (s.count(j) || se.count(j)) {
        se.insert(s.begin(), s.end());
        break;
      }
      s.insert(j);
      j += g[j];
    }
    if (!se.count(j)) {
      for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        g2[*it] = j;
      }
    }
  }
  vector<int> dp(n, inf);
  dp[0] = 0;
  int ans = inf;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < 7; j++) {
      if (se.count(i + j))
        continue;
      int t = i + j;
      if (g2[t] != -1)
        t = g2[t];
      if (t >= n - 1)
        ans = min(ans, dp[i] + 1);
      else
        dp[t] = min(dp[t], dp[i] + 1);
    }
  }
  cout << ans << endl;
}