Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
#define YES(ans)                                                               \
  if (ans)                                                                     \
    cout << "YES" << endl;                                                     \
  else                                                                         \
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

// sは初めの点、vは頂点size、dは最短距離を求める(初期化はINF),Gは有向グラフ
void dijkdtra(int s, int v, vector<int> &d, whei(edge) & G) {
  priority_queue<Pint, vector<Pint>, greater<Pint>>
      que;  // firstが距離、secondが頂点
  d[s] = 0; // スタートの距離はもちろんzero
  que.push(Pint(0, s));
  while (!que.empty()) {
    Pint p = que.top();
    que.pop();
    int V = p.second;
    if (d[V] < p.first)
      continue;
    for (int i = 0; i < G[V].size(); i++) {
      edge e = G[V][i];
      if (d[e.to] > d[V] + e.cost) {
        d[e.to] = d[V] + e.cost;
        que.push(Pint(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  hei(int) a(n);
  llint ans = 0;
  rep(i, 0, n) { cin >> a[i]; }
  int i = 1;
  while (i != 2) {
    ans++;
    i = a[i - 1];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
#define YES(ans)                                                               \
  if (ans)                                                                     \
    cout << "YES" << endl;                                                     \
  else                                                                         \
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

// sは初めの点、vは頂点size、dは最短距離を求める(初期化はINF),Gは有向グラフ
void dijkdtra(int s, int v, vector<int> &d, whei(edge) & G) {
  priority_queue<Pint, vector<Pint>, greater<Pint>>
      que;  // firstが距離、secondが頂点
  d[s] = 0; // スタートの距離はもちろんzero
  que.push(Pint(0, s));
  while (!que.empty()) {
    Pint p = que.top();
    que.pop();
    int V = p.second;
    if (d[V] < p.first)
      continue;
    for (int i = 0; i < G[V].size(); i++) {
      edge e = G[V][i];
      if (d[e.to] > d[V] + e.cost) {
        d[e.to] = d[V] + e.cost;
        que.push(Pint(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  hei(int) a(n);
  llint ans = 0;
  rep(i, 0, n) { cin >> a[i]; }
  int i = 1;
  while (i != 2) {
    ans++;
    i = a[i - 1];
    if (ans == 1000000) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}