prithivMLmods/Coder-Stat huggingface dataset:
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  int a[N];
  for (int i; i < N; i++) {
    cin >> a[i];
  }
  int k = 1;
  for (int i = 1; i <= N; i++) {
    k = a[k];
    if (k == 2) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}


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

int main() {
  int N;
  cin >> N;
  int a[N];
  for (int i; i < N; i++) {
    cin >> a[i];
  }
  int k = 1;
  for (int i = 1; i <= N; i++) {
    k = a[k - 1];
    if (k == 2) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, i, j, cnt = 0;
  cin >> n;
  int *a = new int[n + 1];
  bool *c = new bool[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    c[i] = false;
  }
  c[1] = true;
  c[0] = false;
  i = 1;

  while (1) {
    i = a[i];
    if (c[i]) {
      cout << -1 << endl;
      delete[] a;
      return 0;
    }
    cnt++;
    if (i == 2) {
      cout << cnt << endl;
      delete[] a;
      return 0;
    }
  }
}


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

int main() {
  int n, i, j, cnt = 0;
  cin >> n;
  int *a = new int[n + 1];
  bool *c = new bool[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    c[i] = false;
  }
  c[1] = true;
  c[0] = false;
  i = 1;

  while (1) {
    i = a[i];
    if (c[i]) {
      cout << -1 << endl;
      delete[] a;
      return 0;
    }
    c[i] = true;
    cnt++;
    if (i == 2) {
      cout << cnt << endl;
      delete[] a;
      return 0;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

int main() {
  int n[10001];
  std::cin >> n[0];
  for (int i = 1; (i <= n[0]); ++i) {
    std::cin >> n[i];
  }
  int cur = 1;
  for (int c = 1; (c <= n[0]); ++c) {
    cur = n[cur];
    if (cur == 2) {
      std::cout << c << std::endl;
      return 0;
    }
  }
  std::cout << -1 << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>

int main() {
  int n[100001];
  std::cin >> n[0];
  for (int i = 1; (i <= n[0]); ++i) {
    std::cin >> n[i];
  }
  int cur = 1;
  for (int c = 1; (c <= n[0]); ++c) {
    cur = n[cur];
    if (cur == 2) {
      std::cout << c << std::endl;
      return 0;
    }
  }
  std::cout << -1 << std::endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, next = 1;
  int a[10000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    next = a[next - 1];
    if (next == 2) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, next = 1;
  int a[100000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    next = a[next - 1];
    if (next == 2) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int par[300003];
int N;
bool vis[300003];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &par[i]);
  vis[1] = true;
  int now = 1, cnt = 0;
  while (1) {
    now = par[now];
    if (vis[now])
      return !printf("-1");
    cnt++;
    if (now == 2)
      return !printf("%d", cnt);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int par[300003];
int N;
bool vis[300003];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &par[i]);
  vis[1] = true;
  int now = 1, cnt = 0;
  while (1) {
    now = par[now];
    if (vis[now])
      return !printf("-1");
    vis[now] = true;
    cnt++;
    if (now == 2)
      return !printf("%d", cnt);
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[100010];
bool f[100010];
int ans;
void dfs(int i, int c) {
  if (i == 2)
    ans = c;
  if (f[a[i]]) {
    return;
  } else {
    dfs(a[i], c + 1);
  }
}
int main() {
  int n;
  cin >> n;
  ans = 1000000;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1, 0);
  if (ans < 1000000) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100010];
bool f[100010];
int ans;
void dfs(int i, int c) {
  f[i] = true;
  if (i == 2)
    ans = c;
  if (f[a[i]]) {
    return;
  } else {
    dfs(a[i], c + 1);
  }
}
int main() {
  int n;
  cin >> n;
  ans = 1000000;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1, 0);
  if (ans < 1000000) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
// #define _GLIBCXX_DEBUG

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  vector<bool> b(N, false);
  b[0] = true;

  int ans = 0;
  int n = 0;

  while (b[a[n] - 1] == false) {
    n = a[n] - 1;
    ans++;
    if (n == 1) {
      cout << ans << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
// #define _GLIBCXX_DEBUG

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  vector<bool> b(N, false);
  b[0] = true;

  int ans = 0;
  int n = 0;

  while (b[a[n] - 1] == false) {
    b[a[n] - 1] = true;
    n = a[n] - 1;
    ans++;
    if (n == 1) {
      cout << ans << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__ ",", __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000
#define long long long // for codeforces

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) cin >> vec[i];
  rep(i, n) vec[i]--;

  int now = 0;
  vector<bool> visited(n, false);
  visited[0] = true;
  int cnt = 0;
  while (now != 1) {
    now = vec[now];
    if (visited[now]) {
      cout << -1 << endl;
      return 0;
    }
    cnt++;
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__ ",", __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000
#define long long long // for codeforces

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) cin >> vec[i];
  rep(i, n) vec[i]--;

  int now = 0;
  vector<bool> visited(n, false);
  visited[0] = true;
  int cnt = 0;
  while (now != 1) {
    now = vec[now];
    if (visited[now]) {
      cout << -1 << endl;
      return 0;
    }
    visited[now] = true;
    cnt++;
  }
  cout << cnt << endl;

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int N;
  int num[100001];
  char check[10001] = {0};
  int start;

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
  }

  int p = 1, cont = 0;
  while (1) {
    if (p == 2) {
      cout << cont << endl;
      ;
      break;
    }
    if (check[p] == 1) {
      cout << "-1" << endl;
      break;
    }

    check[p] = 1;
    p = num[p];
    cont++;
  }

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N;
  int num[100001];
  char check[100001] = {0};
  int start;

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
  }

  int p = 1, cont = 0;
  while (1) {
    if (p == 2) {
      cout << cont << endl;
      ;
      break;
    }
    if (check[p] == 1) {
      cout << "-1" << endl;
      break;
    }

    check[p] = 1;
    p = num[p];
    cont++;
  }

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int a = 0; a < N; a++) {
    cin >> A[a];
  }
  vector<bool> B(N, false);
  int i = 0;
  bool f = false;
  int count = 0;
  while (!B[i]) {
    i = A[i] - 1;
    count++;
    if (i == 1) {
      f = true;
      break;
    }
  }
  cout << (f ? count : -1) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int a = 0; a < N; a++) {
    cin >> A[a];
  }
  vector<bool> B(N, false);
  int i = 0;
  bool f = false;
  int count = 0;
  while (!B[i]) {
    B[i] = true;
    i = A[i] - 1;
    count++;
    if (i == 1) {
      f = true;
      break;
    }
  }
  cout << (f ? count : -1) << endl;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define N 10000
#define REP(i, n) for (int i = 0; i < n; i++)
int n;
int l[N];
int main(void) {
  cin >> n;
  REP(i, n) cin >> l[i];
  int now = 0;
  int c = 0;

  while (c <= n) {
    if (now == 1) {
      break;
    }
    c++;
    now = l[now] - 1;
  }
  if (c > n)
    c = -1;
  printf("%d", c);
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 1000000
#define REP(i, n) for (int i = 0; i < n; i++)
int n;
int l[N];
int main(void) {
  cin >> n;
  REP(i, n) cin >> l[i];
  int now = 0;
  int c = 0;

  while (c <= n) {
    if (now == 1) {
      break;
    }
    c++;
    now = l[now] - 1;
  }
  if (c > n)
    c = -1;
  printf("%d", c);
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main() {
  ll N, x = 0, t = 0;
  cin >> N;
  vec A(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--;
  }
  while (true) {
    x = A.at(x);
    t++;
    if (x == 1) {
      cout << t << endl;
      return 0;
    }
    if (x == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main() {
  ll N, x = 0, t = 0;
  cin >> N;
  vec A(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--;
  }
  while (true) {
    x = A.at(x);
    t++;
    if (x == 1) {
      cout << t << endl;
      return 0;
    }
    if (x == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (t == N - 1) {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main(void) {
  int n = 0;
  int a[100001];
  int abs_2 = 0;
  int ans = 0, now = 0;

  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 2) {
      abs_2 = 1;
    }
  }
  // どのボタンを押しても、２が光ることはない
  if (abs_2 == 0) {
    printf("-1\n");
    return 0;
  }

  // ボタンを２が光るまで押す
  now = 1;
  while (now != 2) {
    ans++;
    now = a[now];
  }

  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main(void) {
  int n = 0;
  int a[100001];
  int abs_2 = 0;
  int ans = 0, now = 0;

  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 2) {
      abs_2 = 1;
    }
  }
  // どのボタンを押しても、２が光ることはない
  if (abs_2 == 0) {
    printf("-1\n");
    return 0;
  }

  // ボタンを２が光るまで押す
  now = 1;
  while (now != 2) {
    ans++;
    now = a[now];
    if (ans > n) {
      printf("-1\n");
      return 0;
    }
  }

  printf("%d\n", ans);

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, o = 1;
  cin >> n;
  vector<int> v(n, 1);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  while (v.at(0) != 2 && v.at(0) != 1) {
    o++;
    v.at(0) = v.at(v.at(0) - 1);
  }
  if (v.at(0) == 2)
    cout << o << endl;
  else
    cout << -1 << endl;
}

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

int main() {
  int n, o = 1;
  cin >> n;
  vector<int> v(n, 1);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  while (v.at(0) != 2 && v.at(0) != 1 && o <= n + 1) {
    o++;
    v.at(0) = v.at(v.at(0) - 1);
  }
  if (v.at(0) == 2)
    cout << o << endl;
  else
    cout << -1 << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e15;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
int main() {
  int n, a[100005], b;
  cin >> n;
  rep(i, n) {
    cin >> b;
    --b;
    a[i] = b;
  }
  int now = 0, cnt = 0;
  bool out = false;
  while (now != 1) {
    now = a[now];
    if (now == 0) {
      out = 1;
      break;
    }
    cnt++;
  }
  if (out)
    PRINT(-1);
  else
    PRINT(cnt);
}

Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e15;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
int main() {
  int n, a[100005], b;
  cin >> n;
  rep(i, n) {
    cin >> b;
    --b;
    a[i] = b;
  }
  int now = 0, cnt = 0;
  bool out = false;
  while (now != 1) {
    now = a[now];
    if (now == 0 || cnt >= n) {
      out = 1;
      break;
    }
    cnt++;
  }
  if (out)
    PRINT(-1);
  else
    PRINT(cnt);
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {

  int N;
  cin >> N;

  int a[N];

  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  // ----------

  int lighted[N];

  int lighting = 1;
  int cnt = 0;

  int flg = 1;

  while (flg == 1) {

    int next_light = a[lighting - 1];

    lighted[cnt] = lighting;

    lighting = next_light;
    cnt++;

    if (lighting == 2) {
      flg = 2;
    }

    for (int k = 0; k < cnt; k++) {
      if (next_light == lighted[k]) {
        flg = 3;
      }
    }
  }

  if (flg == 2) {
    cout << cnt << endl;
  } else if (flg == 3) {
    cout << "-1" << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {

  int N;
  cin >> N;

  int a[N];

  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  // ----------

  int lighted[N];

  int lighting = 1;
  int cnt = 0;

  int flg = 1;

  while (flg == 1) {

    int next_light = a[lighting - 1];

    lighted[cnt] = lighting;

    lighting = next_light;
    cnt++;

    if (lighting == 2) {
      flg = 2;
    }

    if (cnt > N) {
      flg = 3;
    }
  }

  if (flg == 2) {
    cout << cnt << endl;
  } else if (flg == 3) {
    cout << "-1" << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> avec(N);
  for (int i = 0; i < N; i++) {
    cin >> avec.at(i);
  }

  int now = 1, cnt = 0;
  while (1) {
    if (now == 2) {
      cout << cnt << endl;
      break;
    }
    if (cnt >= N) {
      cout << -1 << endl;
      break;
    }
    now = avec.at(now);
    cnt++;
  }
  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> avec(N);
  for (int i = 0; i < N; i++) {
    cin >> avec.at(i);
  }

  int now = 1, cnt = 0;
  while (1) {
    if (now == 2) {
      cout << cnt << endl;
      break;
    }
    if (cnt >= N) {
      cout << -1 << endl;
      break;
    }
    now = avec.at(now - 1);
    cnt++;
  }
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
long tam, n, a[100001], s, t;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  t = 1;
  while (true) {
    s++;
    t = a[t];
    if (t == 2)
      break;
    if (s == 1000000001)
      return cout << -1, 0;
  }
  cout << s;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
long tam, n, a[100001], s, t;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  t = 1;
  while (true) {
    s++;
    t = a[t];
    if (t == 2)
      break;
    if (s == 100000001)
      return cout << -1, 0;
  }
  cout << s;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i, a, b) for (ll i = (a); i <= (ll)(b); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
}

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using Tuple = tuple<int, int, int>;

int main() {
  // ll n,a,b; cin >> n >> a >> b;
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  set<int> st;
  int now = 0;
  int ans = 0;
  st.insert(0);
  while (true) {
    now = a[now];
    ans++;
    if (now == 1) {
      cout << ans << endl;
      return 0;
    }
    if (st.find(now) != st.end()) {
      cout << -1 << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i, a, b) for (ll i = (a); i <= (ll)(b); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
}

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using Tuple = tuple<int, int, int>;

int main() {
  // ll n,a,b; cin >> n >> a >> b;
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  set<int> st;
  int now = 0;
  int ans = 0;
  st.insert(0);
  while (true) {
    now = a[now];
    ans++;
    if (now == 1) {
      cout << ans << endl;
      return 0;
    }
    if (st.find(now) != st.end()) {
      cout << -1 << endl;
      return 0;
    }
    st.insert(now);
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define MAX 1000
int main() {
  int n, num = 0;
  int a[MAX];
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  i = 1;
  while (1) {
    num++;
    if (a[i] == 2) {
      printf("%d", num);
      break;
    }
    i = a[i];
    if (num > n) {
      printf("-1");
      break;
    }
  }
}

Fixed submission:
#include <stdio.h>
#define MAX 100000
int main() {
  int n, num = 0;
  int a[MAX];
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  i = 1;
  while (1) {
    num++;
    if (a[i] == 2) {
      printf("%d", num);
      break;
    }
    i = a[i];
    if (num > n) {
      printf("-1");
      break;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  vector<int> checked(N, 0);
  checked.at(0) = 1;
  int current = 1;
  int count = 0;

  while (current != 2) {
    int next = a.at(current - 1);
    if (checked.at(next - 1) == 1) {
      count = -1;
      break;
    } else {
      current = next;
      count++;
    }
  }

  cout << count << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  vector<int> checked(N, 0);
  checked.at(0) = 1;
  int current = 1;
  int count = 0;

  while (current != 2) {
    int next = a.at(current - 1);
    if (checked.at(next - 1) == 1) {
      count = -1;
      break;
    } else {
      current = next;
      checked.at(current - 1) = 1;
      count++;
    }
  }

  cout << count << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  int next = 1, count = 0;
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (;;) {
    if (next == 2) {
      cout << count << endl;
      break;
    }
    next = a.at(next - 1);
    count += 1;
    if (next == 1 || count > N * N) {
      cout << "-1" << endl;
      break;
    }
  }
}

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

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  int next = 1, count = 0;
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (;;) {
    if (next == 2) {
      cout << count << endl;
      break;
    }
    next = a.at(next - 1);
    count += 1;
    if (next == 1 || count > N * 10) {
      cout << "-1" << endl;
      break;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k = 0, d = 0;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (d < n + 1) {
    d++;
    k = a[k];
    if (k == 2) {
      cout << d;
      return 0;
    }
  }
  cout << -1;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k = 1, d = 0;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (d < n + 1) {
    d++;
    k = a[k];
    if (k == 2) {
      cout << d;
      return 0;
    }
  }
  cout << -1;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
ll dp[2][4];
int now = 0;
int t[3];
ll p2[32];
bool use[100010];

int main() {
  int n;
  cin >> n;
  int a[100010];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  use[1] = true;
  int now = 1;
  int count = 0;
  while (true) {
    int nex = a[now];
    if (use[nex]) {
      cout << -1 << endl;
      return 0;
    }
    count++;

    if (nex == 2) {
      break;
    }
    now = nex;
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
ll dp[2][4];
int now = 0;
int t[3];
ll p2[32];
bool use[100010];

int main() {
  int n;
  cin >> n;
  int a[100010];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  use[1] = true;
  int now = 1;
  int count = 0;
  while (true) {
    int nex = a[now];
    if (use[nex]) {
      cout << -1 << endl;
      return 0;
    }
    use[nex] = true;
    count++;

    if (nex == 2) {
      break;
    }
    now = nex;
  }
  cout << count << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  vector<int> now(1);
  now.at(0) = 1;

  for (int i = 0; i < 100000; i++) {
    now.push_back(a.at(now.at(i) - 1));

    if (now.at(i + 1) == 2) {
      cout << now.size() - 1 << endl;
      break;
    }
  }
  if (now.at(100000) != 2) {
    cout << -1 << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  vector<int> now(1);
  now.at(0) = 1;

  for (int i = 0; i < 100000; i++) {
    now.push_back(a.at(now.at(i) - 1));

    if (now.at(i + 1) == 2) {
      cout << now.size() - 1 << endl;
      break;
    }
  }
  if (now.size() == 100001) {
    cout << -1 << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c = 1, i = 0;
  cin >> a;
  int b[a];
  for (int i = 0; i < a; i++)
    cin >> b[i];
  while (1) {
    c = b[c - 1];
    i++;
    if (c == 1) {
      cout << -1 << endl;
      return 0;
    } else if (c == 2) {
      cout << i << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c = 1, i = 0;
  cin >> a;
  int b[a];
  for (int i = 0; i < a; i++)
    cin >> b[i];
  while (1) {
    c = b[c - 1];
    i++;
    if (c == 1) {
      cout << -1 << endl;
      return 0;
    } else if (c == 2) {
      cout << i << endl;
      return 0;
    }
    if (i > a) {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  long long N, A, r;
  r = 0;
  cin >> N;
  vector<long long> vec(N), seen(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  seen.at(0) = 1;
  A = 1;
  long long count = 0;
  while (r == 0) {
    A = vec.at(A - 1);
    if (seen.at(A - 1) == 0) {
      seen.at(A - 1)++;
      if (A == 2) {
        r = 1;
      }
    } else {
      r == 1;
    }
    count++;
  }
  if (seen.at(1) == 1) {
    cout << count << endl;
  } else {
    cout << -1 << endl;
  }
}


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

int main() {

  long long N, A, r;
  r = 0;
  cin >> N;
  vector<long long> vec(N), seen(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  seen.at(0) = 1;
  A = 1;
  long long count = 0;
  while (r == 0) {
    A = vec.at(A - 1);
    if (seen.at(A - 1) == 0) {
      seen.at(A - 1)++;
      if (A == 2) {
        r = 1;
      }
    } else {
      r = 1;
    }
    count++;
  }
  if (seen.at(1) == 1) {
    cout << count << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
// #include <vector>

using namespace std;

int search(int *arr, int len) {
  int flag = 0;
  if (len > 0) {
    for (int i = 0; i < len; i++) {
      if (arr[len] == arr[i]) {
        flag = 1;
        break;
      }
    }
  }
  return flag;
}

int main() {
  int N;
  int a[110000];

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  // for(int i = 0; i < N; i++) cout << a[i];
  // cout << endl;

  int passed[110000] = {};

  int now = 1;
  int cnt = 0;

  passed[0] = now;
  while (now != 2) {
    // if(cnt > 10) break;
    if (now == 2)
      break;

    cnt++;
    now = a[now - 1];
    passed[cnt] = now;

    // cout << "cnt: " << cnt << " now: " << now << endl;

    if (search(passed, cnt) || cnt > N) {
      cnt = -1;
      break;
    }
  }

  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <iostream>
// #include <vector>

using namespace std;

int search(int *arr, int len) {
  int flag = 0;
  if (len > 0) {
    for (int i = 0; i < len; i++) {
      if (arr[len] == arr[i]) {
        flag = 1;
        break;
      }
    }
  }
  return flag;
}

int main() {
  int N;
  int a[110000];

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  // for(int i = 0; i < N; i++) cout << a[i];
  // cout << endl;

  int passed[110000] = {};

  int now = 1;
  int cnt = 0;

  passed[0] = now;
  while (now != 2) {
    // if(cnt > 10) break;
    if (now == 2)
      break;

    cnt++;
    now = a[now - 1];
    passed[cnt] = now;

    // cout << "cnt: " << cnt << " now: " << now << endl;

    if (cnt > N) { // if(search(passed, cnt)){
      cnt = -1;
      break;
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int a[10010];
bool b[10010];

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];

  int cou = 0;
  int i = 0;
  while (true) {
    if (b[a[i]] == true) {
      cout << -1 << endl;
      return 0;
    }
    if (a[i] == 2) {
      cou++;
      break;
    }
    b[a[i]] = true;
    cou++;
    i = a[i] - 1;
  }
  cout << cou << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int a[100100];
bool b[100100];

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];

  int cou = 0;
  int i = 0;
  while (true) {
    if (b[a[i]] == true) {
      cout << -1 << endl;
      return 0;
    }
    if (a[i] == 2) {
      cou++;
      break;
    }
    b[a[i]] = true;
    cou++;
    i = a[i] - 1;
  }
  cout << cou << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int i, ans = 1, count = 1;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  while (1) {
    ans = a[ans];
    if (ans == 2)
      break;
    count++;
    if (a[ans] == 1) {
      count = -1;
      break;
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int i, ans = 1, count = 1;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  while (1) {
    ans = a[ans];
    if (ans == 2)
      break;
    count++;
    if (a[ans] == 1 or count > 2 * n) {
      count = -1;
      break;
    }
  }
  cout << count << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

typedef unsigned long long LL;

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a;
  a.push_back(0);
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  int number = 0;
  bool flag = false;
  int current = 1;
  do {
    number++;
    current = a[current];
    if (current == 2) {
      flag = true;
      break;
    }
  } while (current != 1);
  if (!flag) {
    cout << -1 << endl;
  } else {
    cout << number << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

typedef unsigned long long LL;

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a;
  a.push_back(0);
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  int number = 0;
  bool flag = false;
  int current = 1;
  do {
    number++;
    current = a[current];
    if (current == 2) {
      flag = true;
      break;
    }
  } while (number < N + 10);
  if (!flag) {
    cout << -1 << endl;
  } else {
    cout << number << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i, n, ans = 1;
  int a[100005];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int chika = 0;
  while (ans != 2) {
    ans = a[ans];
    chika++;
  }
  cout << chika << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i, n, ans = 1;
  int a[100005];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int chika = 0;
  while (ans != 2) {
    ans = a[ans];
    chika++;
    if (chika > 200000) {
      break;
    }
  }
  if (chika > 200000) {
    chika = -1;
  }
  cout << chika << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue> // s.front()
#include <set>
#include <stack> // s.top()
#include <string>
#include <vector>
using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;
#define print(x) cout << (x) << endl

#define INF10 10000000000
#define INF5 100000
#define EPS 1e-10
#define MOD 1000000007

int main() {
  int N;
  vector<int> a;
  a.push_back(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    int pp;
    cin >> pp;
    a.push_back(pp);
  }
  int settle = 1;
  int count = 0;
  while (true) {
    if (settle == 2) {
      print(count);
      return 0;
    } else {
      settle = a[settle];
      count++;
      if (settle == 1)
        break;
    }
  }
  print(-1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue> // s.front()
#include <set>
#include <stack> // s.top()
#include <string>
#include <vector>
using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;
#define print(x) cout << (x) << endl

#define INF10 10000000000
#define INF5 100000
#define EPS 1e-10
#define MOD 1000000007

int main() {
  int N;
  vector<int> a;
  a.push_back(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    int pp;
    cin >> pp;
    a.push_back(pp);
  }
  int settle = 1;
  int count = 0;
  while (true) {
    if (settle == 2) {
      print(count);
      return 0;
    } else {
      settle = a[settle];
      count++;
      if (settle == 1 || count >= a.size())
        break;
    }
  }
  print(-1);

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 1; i < N + 1; i++) {
    cin >> a.at(i);
  }
  int b = 1;
  for (int j = 0; j < N; j++) {
    b = a.at(b);
    if (b == 2) {
      cout << j + 1 << endl;
      goto as;
    }
  }
  cout << -1 << endl;
as:
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> a(200000);
  for (int i = 1; i < N + 1; i++) {
    cin >> a.at(i);
  }
  int b = 1;
  for (int j = 0; j < N; j++) {
    b = a.at(b);
    if (b == 2) {
      cout << j + 1 << endl;
      goto as;
    }
  }
  cout << -1 << endl;
as:
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
// 多くてもN回
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int a[N];
  FOR(i, 0, N) cin >> a[i];
  int now = 1;
  FOR(i, 0, N + 1) {
    if (now == 2) {
      cout << i << '\n';
      return 0;
    }
    now = a[now];
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
// 多くてもN回
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int a[N + 1];
  FOR(i, 1, N + 1) cin >> a[i];
  int now = 1;
  FOR(i, 0, N + 1) {
    if (now == 2) {
      cout << i << '\n';
      return 0;
    }
    now = a[now];
  }
  cout << -1 << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, a[10000], ans = 0;
  bool check[10000] = {false};

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int i = 0;
  while (a[i] != 2) {
    if (check[a[i] - 1] == false) {
      check[a[i] - 1] = true;
      ans++;
      i = a[i] - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, a[100000], ans = 0;
  bool check[100000] = {false};

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int i = 0;
  while (a[i] != 2) {
    if (check[a[i] - 1] == false) {
      check[a[i] - 1] = true;
      ans++;
      i = a[i] - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, count;
  cin >> N;
  count = 0;
  int B = 1;
  vector<int> A(N);
  for (int i = 0; i <= N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i <= N; i++) {
    if (B == 2) {
      cout << count << endl;
      break;
    }

    B = A.at(B - 1);
    count++;
    if (i == N) {
      cout << -1 << endl;
    }
  }
}

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

int main() {
  int N, count;
  cin >> N;
  count = 0;
  int B = 1;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i <= N; i++) {
    if (B == 2) {
      cout << count << endl;
      break;
    }

    B = A.at(B - 1);
    count++;
    if (i == N) {
      cout << -1 << endl;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N; // ボタンの数

  cin >> N;
  vector<int> Botton(N); // 次に光るボタンのi(番号)
  for (int i = 0; i < N; i++) {
    cin >> Botton.at(i);
  }

  int Ok = -1; //-1は不可能　正の数はボタンを押した回数
  int I = 1;   // 光っているボタンの番号
  for (int i = 1; i <= N; i++) {
    if (Botton.at(I) == 2) {
      Ok = i;
      break;
    }
    I = Botton.at(I);
  }
  cout << Ok << endl;
}


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

int main() {
  int N; // ボタンの数

  cin >> N;
  vector<int> Botton(N + 1); // 次に光るボタンのi(番号)
  for (int i = 1; i <= N; i++) {
    cin >> Botton.at(i);
  }

  int Ok = -1; //-1は不可能　正の数はボタンを押した回数
  int I = 1;   // 光っているボタンの番号
  for (int i = 1; i <= N; i++) {
    if (Botton.at(I) == 2) {
      Ok = i;
      break;
    }
    I = Botton.at(I);
  }
  cout << Ok << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long int ll;

int main(int argc, char const *argv[]) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  ll next = a[0];
  ll cnt = 1;
  while (1) {
    if (next == 0) {
      cout << -1 << endl;
      break;
    }
    if (next == 1) {
      cout << cnt << endl;
      break;
    }
    next = a[next];
    cnt++;
  }
}

Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long int ll;

int main(int argc, char const *argv[]) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  ll next = a[0];
  ll cnt = 1;
  while (1) {
    if (next == 0 || cnt > n) {
      cout << -1 << endl;
      break;
    }
    if (next == 1) {
      cout << cnt << endl;
      break;
    }
    next = a[next];
    cnt++;
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int next = 1;
  int cnt = 0;
  while (next != 2) {
    next = a[next];
    ++cnt;
    if (next == 1) {
      cnt = -1;
      break;
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int next = 1;
  int cnt = 0;
  while (next != 2) {
    next = a[next];
    ++cnt;
    if (cnt > n) {
      cnt = -1;
      break;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  int i = 0, k = 1;
  while (k != 2) {
    k = a.at(k - 1);
    i++;
    if (k == 1) {
      cout << -1 << endl;
      break;
    }
  }

  if (k == 2) {
    cout << i << endl;
  }
}

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

int main() {

  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  int i = 0, k = 1;
  while (k != 2) {
    k = a.at(k - 1);
    i++;
    if (i > N) {
      cout << -1 << endl;
      break;
    }
  }

  if (k == 2) {
    cout << i << endl;
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
#define ALL(a) (a).begin(), (a).end()
#define p(a, b) printf(a, b);
#define c(s) cout << (s) << endl;
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define cyn(c) cout << (c ? "Yes" : "No") << endl;
#define cYN(c) cout << (c ? "YES" : "NO") << endl;
#define pyn(c) printf("%s\n", c ? "Yes" : "No");
#define pYN(c) printf("%s\n", c ? "YES" : "NO");
#define INF 1e18
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> P;
template <class T> bool contain(const std::string &s, const T &v) {
  return s.find(v) != std::string::npos;
}
ULL mod = 1e9 + 7;

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
#define ALL(a) (a).begin(), (a).end()
#define p(a, b) printf(a, b);
#define c(s) cout << (s) << endl;
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define cyn(c) cout << (c ? "Yes" : "No") << endl;
#define cYN(c) cout << (c ? "YES" : "NO") << endl;
#define pyn(c) printf("%s\n", c ? "Yes" : "No");
#define pYN(c) printf("%s\n", c ? "YES" : "NO");
#define INF 1e18
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> P;
template <class T> bool contain(const std::string &s, const T &v) {
  return s.find(v) != std::string::npos;
}
ULL mod = 1e9 + 7;

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