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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long unsigned llu;
typedef long double ld;
#define ll long long
#define PI 3.14159265358979323846
#define For(i, a, b) for (int i = a, sz = (int)b; i < sz; i++)
#define rFor(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fill(a, b) memset(a, b, sizeof(a))
#define IO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int mod = 1000000007;
const ll inf = 1000000000009;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdoutn

int n{}, ans{};
int vis[100005];
vector<int> v(n);
int solve(int size, int i) {
  if (i == 2)
    return ans;
  if (!vis[i] && i < size) {
    ans++;
    vis[i] = 1;
    solve(size, v[i]);
  } else
    return -1;
}

int main() {
  IO;
  cin >> n;
  v.resize(n + 1);
  For(i, 1, n + 1) cin >> v[i];
  cout << solve(n + 1, 1) << '\n';

  // system("pause");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long unsigned llu;
typedef long double ld;
#define ll long long
#define PI 3.14159265358979323846
#define For(i, a, b) for (int i = a, sz = (int)b; i < sz; i++)
#define rFor(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fill(a, b) memset(a, b, sizeof(a))
#define IO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int mod = 1000000007;
const ll inf = 1000000000009;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdoutn

int n{}, ans{};
int vis[100005];
vector<int> v(n);
int solve(int size, int i) {
  if (i == 2)
    return ans;
  if (!vis[i] && i < size) {
    ans++;
    vis[i] = 1;
    int a = solve(size, v[i]);
    return a;
  } else
    return -1;
}

int main() {
  IO;
  cin >> n;
  v.resize(n + 1);
  For(i, 1, n + 1) cin >> v[i];
  cout << solve(n + 1, 1) << '\n';

  // system("pause");
  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[i];
  vector<bool> visited(101);
  for (int i = 1; i <= N; i++)
    visited[i] = false;

  int ans = 0;
  int aa = 1;
  while (aa != 2) {
    if (visited[aa])
      break;
    visited[aa] = true;
    aa = a[aa - 1];
    ans++;
  }
  if (ans > 0 && aa == 2)
    cout << ans << endl;
  else
    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[i];
  vector<bool> visited(100001);
  for (int i = 1; i <= N; i++)
    visited[i] = false;

  int ans = 0;
  int aa = 1;
  while (aa != 2) {
    if (visited[aa])
      break;
    visited[aa] = true;
    aa = a[aa - 1];
    ans++;
  }
  if (ans > 0 && aa == 2)
    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++)

int main() {
  int n;
  cin >> n;
  vector<int> push(n, 0);
  vector<int> butt(n);
  rep(i, n) cin >> butt.at(i);
  int now = 0;
  int count = 1;
  while (true) {
    if (push.at(now) == 1) {
      cout << -1 << endl;
      return 0;
    }
    if (butt.at(now) == 2) {
      cout << count << endl;
      return 0;
    }
    now = butt.at(now) - 1;
    count++;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> push(n, 0);
  vector<int> butt(n);
  rep(i, n) cin >> butt.at(i);
  int now = 0;
  int count = 1;
  while (true) {
    if (push.at(now) == 1) {
      cout << -1 << endl;
      return 0;
    }
    if (butt.at(now) == 2) {
      cout << count << endl;
      return 0;
    }
    push.at(now)++;
    now = butt.at(now) - 1;
    count++;
  }

  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#define max 10000
int main() {
  int a[max], i, j, n, p = 0, sum = 0, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 2)
      p = i;
  }
  if (p == 0) {
    printf("-1");
    return 0;
  }
  x = 1;
  y = a[x];
  while (y != 2) {
    y = a[y];
    sum++;
    if (sum > n - 1) {
      printf("-1");
      return 0;
    }
  }
  sum++;
  printf("%d", sum);
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#define max 100005
int main() {
  int a[max], i, j, n, p = 0, sum = 0, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 2)
      p = i;
  }
  if (p == 0) {
    printf("-1");
    return 0;
  }
  x = 1;
  y = a[x];
  while (y != 2) {
    y = a[y];
    sum++;
    if (sum > n - 1) {
      printf("-1");
      return 0;
    }
  }
  sum++;
  printf("%d", sum);
  return 0;
}
Problem p03680 (C++)

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

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FUNCTION__ << endl;
#define int long long

const long long INF = __LONG_LONG_MAX__ / 2;

using namespace std;
int a[20000];
bool visited[20000];

signed main() {
  int N;
  cin >> N;
  for (int i = 1; i < N + 1; i++)
    visited[i] = false;
  for (int i = 1; i < N + 1; i++) {
    cin >> a[i];
  }

  int count = 0;
  int pos = 1;
  visited[1] = true;
  while (true) {
    if (pos == 2) {
      cout << count;
      return 0;
    }
    pos = a[pos];
    if (visited[pos]) {
      cout << -1;
      return 0;
    }
    visited[pos] = true;
    count++;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FUNCTION__ << endl;
#define int long long

const long long INF = __LONG_LONG_MAX__ / 2;

using namespace std;
int a[200000];
bool visited[200000];

signed main() {
  int N;
  cin >> N;
  for (int i = 1; i < N + 1; i++)
    visited[i] = false;
  for (int i = 1; i < N + 1; i++) {
    cin >> a[i];
  }

  int count = 0;
  int pos = 1;
  visited[1] = true;
  while (true) {
    if (pos == 2) {
      cout << count;
      return 0;
    }
    pos = a[pos];
    if (visited[pos]) {
      cout << -1;
      return 0;
    }
    visited[pos] = true;
    count++;
  }
  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);
  vector<int> b(N);

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

  if (b.at(0) == 0) {
    cout << -1 << endl;
  } else if (b.at(0) == 1) {
    cout << 1 << endl;
  } else {
    int counter = 2;
    int d = b.at(0);
    int e;

    while (d > 0) {
      e = d;
      d = b.at(e);

      if (d == 1) {
        break;
      }

      counter += 1;
    }
    if (d == 1) {
      cout << counter << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

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

int main() {

  int N;
  cin >> N;

  vector<int> a(N);
  vector<int> b(N);

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

  if (b.at(0) == 0) {
    cout << -1 << endl;
  } else if (b.at(0) == 1) {
    cout << 1 << endl;
  } else {
    int counter = 2;
    int d = b.at(0);
    int e;

    while (counter < N) {
      e = d;
      d = b.at(e);

      if (d == 1) {
        break;
      }

      counter += 1;
    }
    if (d == 1) {
      cout << counter << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
//  cout << char(i+48) <<
//  endl;//なぜかaは47と得る時がある。+48で出力もaにできる。 cout << char(97) <<
//  endl;//アスキーコードでaを出力
// sort(b.begin(), b.end());//bという配列を小さい方からソート
// reverse(b.begin(), b.end());//bという配列をリターン
// 10桁でdouble出力するパターン１
// printf("%.10f\n", ma);
// 10桁でdouble出力するパターン２
// cout << fixed << setprecision(10);
//  cout << ma << endl;

/*01 02 03 12 13 23　と６回見ていくパターン
for(int i=0;i<n-1;i++){
  for(int j=i+1;j<n;j++){
    }
  }
*///vector<vector<int>> data(3, vector<int>(4));//int型の2次元配列(3×4要素の)の宣言
// int64_t a;
// 10のi乗pow(10, i);
/*string s; stringでの文字列を数字型に変える方法
  cin >> s;
  rep(i,s.size()-2) {
  int a= (s.at(i)-'0')*100 + (s.at(i+1)-'0')*10+ s.at(i+2) -'0';
    */
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) cin >> a.at(i);
  rep(i, n) a.at(i)--;
  int x = -1;
  int y = -2;
  int z = -3;
  int v = -4;
  //  rep(i,n) cout << a.at(i) << endl;
  int count = 0;
  int num = 0;
  num = a.at(0);
  count++;
  b.at(num)++;
  for (;;) {
    if (num == 1)
      break;
    num = a.at(num);
    if (b.at(num) >= 2) {
      count = -1;
      break;
    }
    count++;
  }
  cout << count << endl;
}


Fixed submission:
//  cout << char(i+48) <<
//  endl;//なぜかaは47と得る時がある。+48で出力もaにできる。 cout << char(97) <<
//  endl;//アスキーコードでaを出力
// sort(b.begin(), b.end());//bという配列を小さい方からソート
// reverse(b.begin(), b.end());//bという配列をリターン
// 10桁でdouble出力するパターン１
// printf("%.10f\n", ma);
// 10桁でdouble出力するパターン２
// cout << fixed << setprecision(10);
//  cout << ma << endl;

/*01 02 03 12 13 23　と６回見ていくパターン
for(int i=0;i<n-1;i++){
  for(int j=i+1;j<n;j++){
    }
  }
*///vector<vector<int>> data(3, vector<int>(4));//int型の2次元配列(3×4要素の)の宣言
// int64_t a;
// 10のi乗pow(10, i);
/*string s; stringでの文字列を数字型に変える方法
  cin >> s;
  rep(i,s.size()-2) {
  int a= (s.at(i)-'0')*100 + (s.at(i+1)-'0')*10+ s.at(i+2) -'0';
    */
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) cin >> a.at(i);
  rep(i, n) a.at(i)--;
  int x = -1;
  int y = -2;
  int z = -3;
  int v = -4;
  //  rep(i,n) cout << a.at(i) << endl;
  int count = 0;
  int num = 0;
  num = a.at(0);
  count++;
  b.at(num)++;
  for (;;) {
    if (num == 1)
      break;
    num = a.at(num);
    b.at(num)++;
    if (b.at(num) >= 2) {
      count = -1;
      break;
    }
    count++;
  }
  cout << count << endl;
}
Problem p03680 (C++)

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

int main() {
  int N, count;
  cin >> N;
  count = 1;
  vector<int> vec(N);

  for (int i = 0; i < N; i++) { // 配列を代入。
    cin >> vec.at(i);
  }

  // テスト:配列の出力
  // for (int i=0; i<N ; i++){
  // cout << vec.at(i) << ' ';
  //}

  for (int i = vec.at(0); i != 2; i = vec.at(i - 1)) {
    count++;
    if (i == 1) {
      count = -1;
      break;
    }
  }
  cout << count << endl;
}


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

int main() {
  int N, count;
  cin >> N;
  count = 1;
  vector<int> vec(N);

  for (int i = 0; i < N; i++) { // 配列を代入。
    cin >> vec.at(i);
  }

  // テスト:配列の出力
  // for (int i=0; i<N ; i++){
  // cout << vec.at(i) << ' ';
  //}

  for (int i = vec.at(0); i != 2; i = vec.at(i - 1)) {
    count++;
    if (count > N + 1) {
      count = -1;
      break;
    }
  }
  cout << count << endl;
}
Problem p03680 (C++)

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

using namespace std;

int main() {
  int n, s[200] = {}, result = 0, lamp = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= 2 * n; i++) {
    lamp = s[lamp];
    result++;
    if (lamp == 2) {
      cout << result;
      return 0;
    }
  }
  cout << -1;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, s[200000] = {}, result = 0, lamp = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= 2 * n; i++) {
    lamp = s[lamp];
    result++;
    if (lamp == 2) {
      cout << result;
      return 0;
    }
  }
  cout << -1;
}
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);
  }

  int now = 0, c = 0;
  for (;;) {
    if (now == 2) {
      cout << c << endl;
      break;
    }
    if (c >= N) {
      cout << -1 << endl;
      break;
    }
    c++;
    now = a.at(now - 1);
  }
}

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 now = 1, c = 0;
  for (;;) {
    if (now == 2) {
      cout << c << endl;
      break;
    }
    if (c >= N) {
      cout << -1 << endl;
      break;
    }
    c++;
    now = a.at(now - 1);
  }
}
Problem p03680 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(100005);
int num = 0;

int f(int x) {
  if (a.at(x) == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (a.at(x) == 2) {
    num++;
    cout << num << endl;
    return 0;
  }
  int y = a.at(x);
  a.at(x) = 0;
  num++;
  f(y);
}

int main() {
  int N;
  cin >> N;

  for (int i = 1; i <= N; i++)
    cin >> a.at(i);

  f(1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(100005);
int num = 0;

int f(int x) {
  if (a.at(x) == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (a.at(x) == 2) {
    num++;
    cout << num << endl;
    return 0;
  }
  int y = a.at(x);
  a.at(x) = 0;
  num++;
  f(y);
  return 0;
}

int main() {
  int N;
  cin >> N;

  for (int i = 1; i <= N; i++)
    cin >> a.at(i);

  f(1);
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mod 1000000007
int b[100];
int main() {
  int n;
  cin >> n;
  int a[n + 10];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int i = 1, re = 0;
  while (1) {
    if (b[i] == 0) {
      b[i] = 1;
      i = a[i];
      re++;
      if (i == 2) {
        cout << re << endl;
        return 0;
      }
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mod 1000000007
int b[1000000];
int main() {
  int n;
  cin >> n;
  int a[n + 10];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int i = 1, re = 0;
  while (1) {
    if (b[i] == 0) {
      b[i] = 1;
      i = a[i];
      re++;
      if (i == 2) {
        cout << re << endl;
        return 0;
      }
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
// CLMN
// Every cloud has a silver lining
// Hakuna matata

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> ii;

#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x) * (x))

const char *fin = "";
const char *fon = "";

#define oo (int)(1e9 + 7)
#define maxn (int)(1e5 + 5)

int a[maxn];
bool fr[maxn];
int n;

void trace(int u, int cnt) {
  if (u == 2) {
    cout << cnt;
    exit(0);
  }
  if (!fr[u]) {
    cout << -1;
    exit(0);
  }
  fr[u] = false;
  trace(a[u], cnt + 1);
}

void inp() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  memset(fr, true, sizeof(fr));
  trace(1, 0);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  freopen(fin, "r", stdin);
  freopen(fon, "w", stdout);
  inp();
  return 0;
}


Fixed submission:
// CLMN
// Every cloud has a silver lining
// Hakuna matata

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> ii;

#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x) * (x))

const char *fin = "";
const char *fon = "";

#define oo (int)(1e9 + 7)
#define maxn (int)(1e5 + 5)

int a[maxn];
bool fr[maxn];
int n;

void trace(int u, int cnt) {
  if (u == 2) {
    cout << cnt;
    exit(0);
  }
  if (!fr[u]) {
    cout << -1;
    exit(0);
  }
  fr[u] = false;
  trace(a[u], cnt + 1);
}

void inp() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  memset(fr, true, sizeof(fr));
  trace(1, 0);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen(fin, "r", stdin); freopen(fon, "w", stdout);
  inp();
  return 0;
}
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 _GLIBCXX_DEBUG

int main() {
  int n, p = 1, ans = 0;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) {
    int a;
    cin >> a;
    vec.at(i) = a;
  }
  while (p != 2) {
    p = vec.at(p - 1);
    ans += 1;
    if (p == 1) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int n, p = 1, ans = 0;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) {
    int a;
    cin >> a;
    vec.at(i) = a;
  }
  while (p != 2) {
    p = vec.at(p - 1);
    ans += 1;
    if (ans > n) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}
Problem p03680 (C++)

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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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


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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int ans = 1;
  int cnt = 0;
  while (ans != 2) {
    int x = ans - 1;
    ans = a[x];
    a[x] = 1;
    cnt++;
    if (ans == 1)
      break;
  }
  if (ans == 2)
    cout << cnt << endl;
  else
    cout << -1 << endl;
  cout << endl;
  return 0;
}
Problem p03680 (C++)

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

int main() {
  int nums;
  cin >> nums;
  vector<int> buttons(nums);

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

  bool flag = false;
  int count = 0;
  int current = 0;
  for (int i = 0; i < nums; i++) {
    count++;
    int tmp = buttons.at(current);
    if (tmp == 2) {
      flag = true;
      break;
    }
    current = tmp;
  }
  if (flag) {
    cout << count << endl;
  } else {
    cout << -1 << endl;
  }
}


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

int main() {
  int nums;
  cin >> nums;
  vector<int> buttons(nums);

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

  bool flag = false;
  int count = 0;
  int current = 0;
  for (int i = 0; i < nums; i++) {
    count++;
    int tmp = buttons.at(current) - 1;
    if (tmp == 1) {
      flag = true;
      break;
    }
    current = tmp;
  }
  if (flag) {
    cout << count << endl;
  } else {
    cout << -1 << 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> v(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    v.at(i) = a - 1;
  }
  bool X = false;
  int i = 0;
  vector<int> w;
  w.push_back(0);
  while (X == false) {
    if (v.at(i) != 1) {
      for (int j = 0; j < w.size(); j++) {
        if (w.at(j) == v.at(i)) {
          X = true;
          cout << -1 << endl;
          break;
        }
      }
      i = v.at(i);
      w.push_back(i);
    } else {
      cout << w.size() << endl;
      X = true;
    }
  }
}

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

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    v.at(i) = a - 1;
  }
  vector<int> w(N);
  int x = 0;
  for (int i = 0; i < N; i++) {
    w.at(i) = x;
    x = v.at(x);
  }
  for (int i = 0; i < N; i++) {
    if (w.at(i) == 1) {
      cout << i << endl;
      break;
    }
    if (i == N - 1) {
      cout << -1 << endl;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <map> //連想配列
#include <queue>
#include <set> //
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector> //動的配列

using namespace std;

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

  return -1;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map> //連想配列
#include <queue>
#include <set> //
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector> //動的配列

using namespace std;

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

  return 0;
}
Problem p03680 (C++)

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

using namespace std;

int main() {
  int a[10000];
  bool f[10000] = {};
  int n, l = 0, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  while (l != 1 && f[l] != true) {
    f[l] = true;
    l = a[l] - 1;
    c++;
  }
  if (l == 1)
    cout << c << endl;
  else
    cout << -1 << endl;

  return (0);
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int a[100000];
  bool f[100000] = {};
  int n, l = 0, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  while (l != 1 && f[l] != true) {
    f[l] = true;
    l = a[l] - 1;
    c++;
  }
  if (l == 1)
    cout << c << endl;
  else
    cout << -1 << 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);
  }
  vector<bool> visited(n, false);
  visited.at(0) = true;
  int now = 1;
  int res = 0;
  while (now != 2) {
    now = a.at(now - 1);
    res++;
    if (visited.at(now - 1)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << 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<bool> visited(n, false);
  visited.at(0) = true;
  int now = 1;
  int res = 0;
  while (now != 2) {
    now = a.at(now - 1);
    res++;
    if (visited.at(now - 1)) {
      cout << -1 << endl;
      return 0;
    }
    visited.at(now - 1) = true;
  }
  cout << res << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ull = unsigned long long;

int B[100001];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> B[i];
  }
  set<int> memo;
  int cur = 1;
  memo.insert(cur);
  int ans = 0;
  while (true) {
    cur = B[cur];
    ++ans;
    if (cur == 2) {
      cout << ans << endl;
      return 0;
    }
    if (memo.count(cur)) {
      cout << "-1" << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ull = unsigned long long;

int B[100001];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> B[i];
  }
  set<int> memo;
  int cur = 1;
  memo.insert(cur);
  int ans = 0;
  while (true) {
    cur = B[cur];
    ++ans;
    if (cur == 2) {
      cout << ans << endl;
      return 0;
    }
    if (memo.count(cur)) {
      cout << "-1" << endl;
      return 0;
    }
    memo.insert(cur);
  }
}
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()) - 1)

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1

print(ans)


Fixed submission:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()) - 1)

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1
        break

print(ans)
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = [int(input()) for _ in range(N)]
pushed = [False] * N
pushed[0] = True
button = 0
count = 0
while True:
    count += 1
    button = A[button] - 1
    if button == 1:
        print(count)
        exit()
    if pushed[button]:
        print(-1)
        exit()


Fixed submission:
N = int(input())
A = [int(input()) for _ in range(N)]
pushed = [False] * N
pushed[0] = True
button = 0
count = 0
while True:
    count += 1
    button = A[button] - 1
    if button == 1:
        print(count)
        exit()
    if pushed[button]:
        print(-1)
        exit()
    pushed[button] = True
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(w, n) FOR(w, 0, n)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int a[n];

  REP(i, n) { cin >> a[i]; }
  int on = 0;
  int count = 0;
  while (a[on] != 1) {
    on = a[on] - 1;
    count++;
    if (on == 1) {
      cout << count << '\n';
      return (0);
    }
  }
  cout << -1 << '\n';
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(w, n) FOR(w, 0, n)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int a[n];

  REP(i, n) { cin >> a[i]; }
  int on = 0;
  int count = 0;
  while (a[on] != 1 && count < n) {
    on = a[on] - 1;
    count++;
    if (on == 1) {
      cout << count << '\n';
      return (0);
    }
  }
  cout << -1 << '\n';
  return (0);
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  int a[n];
  int b = 0;
  int ans = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] == 2)
      b = 1;
  }
  map<int, int> mp;
  if (b == 0) {
    cout << -1 << endl;
  } else {
    int cnt = 0;
    int x = 1;
    b = 0;
    while (cnt < n) {
      ans++;
      if (a[x - 1] == 2) {
        b = 1;
        break;
      }
      x = a[x - 1];
    }
    cout << (b ? ans : -1) << endl;
  }

  // cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  int a[n];
  int b = 0;
  int ans = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] == 2)
      b = 1;
  }
  map<int, int> mp;
  if (b == 0) {
    cout << -1 << endl;
  } else {
    int cnt = 0;
    int x = 1;
    b = 0;
    while (cnt < n) {
      ans++;
      if (a[x - 1] == 2) {
        b = 1;
        break;
      }
      x = a[x - 1];
      cnt++;
    }
    cout << (b ? ans : -1) << endl;
  }

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

Buggy submission (status: Time Limit Exceeded):
/*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
// 必要な宣言

void solve();

int n;
int a[100100];
int flag[100100] = {};

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

  solve();

  return 0;
}

void solve() {
  int cnt = 1;

  flag[0] = 1;

  int i = a[0] - 1;

  while (flag[i] != 1) {
    if (i == 1) {
      cout << cnt << endl;
      return;
    }

    i = a[i] - 1;
    cnt += 1;
  }

  cout << -1 << endl;
}


Fixed submission:
/*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
// 必要な宣言

void solve();

int n;
int a[100100];
int flag[100100] = {};

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

  solve();

  return 0;
}

void solve() {
  int cnt = 1;

  flag[0] = 1;

  int i = a[0] - 1;

  while (flag[i] != 1) {
    if (i == 1) {
      cout << cnt << endl;
      return;
    }
    flag[i] = 1;
    i = a[i] - 1;
    cnt += 1;
  }

  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++)

int main() {
  int N;
  cin >> N;
  vector<int> a(N + 1);
  rep(i, N) { cin >> a[i + 1]; }
  set<int> numset;
  numset.insert(1);
  int p = 1;
  int res = 0;
  bool can = false;
  while (1) {
    p = a[p];
    res++;
    if (numset.count(p))
      break;
    if (p == 2) {
      can = true;
      break;
    }
  }
  if (can) {
    cout << res << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N;
  cin >> N;
  vector<int> a(N + 1);
  rep(i, N) { cin >> a[i + 1]; }
  set<int> numset;
  numset.insert(1);
  int p = 1;
  int res = 0;
  bool can = false;
  while (1) {
    p = a[p];
    res++;
    if (numset.count(p))
      break;
    if (p == 2) {
      can = true;
      break;
    }
    numset.insert(p);
  }
  if (can) {
    cout << res << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define EVEL 1

#ifndef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
const int MOD = 1000000007;
const int INF = 2000000000;
ll ans = 0, id;
int N, M;
ll A, B, C, W;
int a[100010];
bool f = false;
ll sum = 0;
string S;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N;
  EREP(i, N) cin >> a[i];
  for (int i = 1, j = 1;; j++) {
    i = a[i];
    if (i == 2) {
      ans = j;
      break;
    }
    if (j > 600000000) {
      ans = -1;
      break;
    }
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define EVEL 1

#ifndef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
const int MOD = 1000000007;
const int INF = 2000000000;
ll ans = 0, id;
int N, M;
ll A, B, C, W;
int a[100010];
bool f = false;
ll sum = 0;
string S;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N;
  EREP(i, N) cin >> a[i];
  for (int i = 1, j = 1;; j++) {
    i = a[i];
    if (i == 2) {
      ans = j;
      break;
    }
    if (j > 6000000) {
      ans = -1;
      break;
    }
  }

  cout << ans;

  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> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(N);
  }

  int next = 1;
  int count = 0;
  while (next != 2) {
    next = vec.at(next - 1);
    count++;
    if (count > N) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << count << endl;
}

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

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

  int next = 1;
  int count = 0;
  while (next != 2) {
    next = vec.at(next - 1);
    count++;
    if (count > N) {
      cout << "-1" << endl;
      return 0;
    }
  }
  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> bottun(N);
  bool flag = true;
  for (int i = 0; i < N; i++) {
    cin >> bottun.at(i);
  }

  int count = 0;
  int next = 1;
  vector<int> history(1, 1);

  while (flag) {
    count++;
    next = bottun.at(next - 1);
    if (next == 2) {
      break;
    }

    for (int i = 0; i < history.size(); i++) {
      if (history.at(i) == next) {
        flag = false;
        break;
      }
    }
    history.push_back(next);
  }
  if (flag)
    cout << count << endl;
  else
    cout << -1 << endl;
}


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

int main() {
  int N;
  cin >> N;

  vector<int> bottun(N);
  bool flag = true;
  for (int i = 0; i < N; i++) {
    cin >> bottun.at(i);
  }

  int count = 0;
  int next = 1;
  vector<int> history(1, 1);

  while (flag) {
    count++;
    next = bottun.at(next - 1);
    if (next == 2) {
      break;
    }
    if (count > N) {
      flag = false;
      break;
    }
    history.push_back(next);
  }
  if (flag)
    cout << count << endl;
  else
    cout << -1 << endl;
}
Problem p03680 (C++)

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

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

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    state = a.at(state - 1);
    cnt++;
    if (state == 2) {
      cout << cnt << endl;
      break;
    }
  }
  if (state != 2)
    cout << -1 << endl;
}


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

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

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    state = a.at(state - 1);
    cnt++;
    if (state == 2) {
      cout << cnt << endl;
      break;
    }
  }
  if (state != 2)
    cout << -1 << 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);

  for (int i = 0; i < N; i++)
    cin >> a.at(i); // 入力終了

  int ans = 0, j = 0;

  while (1) {
    int x = a.at(j);
    j = x - 1;
    ans++;
    if (j == 0) {
      ans = -1;
      break;
    }
    if (j == 1)
      break;
  }

  cout << ans << endl;

  return 0;
}


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 ans = 0, j = 0;

  while (1) {
    int x = a.at(j);
    j = x - 1;
    ans++;
    if (ans >= N) {
      ans = -1;
      break;
    }
    if (j == 1)
      break;
  }

  cout << ans << 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;
  //  cout << N << endl;

  vector<int> a(N + 1);
  for (int i = 1; i < N + 1; ++i) {
    cin >> a.at(i);
    //    cout << a.at(i) << endl;
  }

  int tmp = 1, cnt = 1, result = -1;
  while (1) {
    tmp = a.at(tmp);
    if (tmp == 2) {
      result = cnt;
      break;
    } else if (tmp == 1) {
      result = -1;
      break;
    } else {
    }
    ++cnt;
  }

  cout << result << endl;

  return 0;
}

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

int main() {
  int N;
  cin >> N;
  //  cout << N << endl;

  vector<int> a(N + 1);
  for (int i = 1; i < N + 1; ++i) {
    cin >> a.at(i);
    //    cout << a.at(i) << endl;
  }

  int tmp = 1, cnt = 1, result = -1;
  while (1) {
    int tmp_cnt = tmp;
    tmp = a.at(tmp_cnt);
    a.at(tmp_cnt) = 1; // ループを検出
    if (tmp == 2) {
      result = cnt;
      break;
    } else if (tmp == 1) {
      result = -1;
      break;
    } else {
    }
    ++cnt;
  }

  cout << result << endl;

  return 0;
}
Problem p03680 (C++)

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

const long long INFLL = 1LL << 60;
const int INFI = 1000000000;

int main() {
  int N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  int count = 0;
  map<int, int> m{};

  int idx = 0;
  m[0]++;
  while (true) {
    idx = a[idx];
    if (m[idx] != 0) {
      count = -1;
      break;
    } else {
      m[idx]++;
      count++;
      if (idx == 1)
        break;
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;

const long long INFLL = 1LL << 60;
const int INFI = 1000000000;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  int count = 0;
  map<int, int> m{};

  int idx = 0;
  m[0]++;
  while (true) {
    idx = a[idx];
    if (m[idx] != 0) {
      count = -1;
      break;
    } else {
      m[idx]++;
      count++;
      if (idx == 1)
        break;
    }
  }
  cout << count << endl;
}
Problem p03680 (C++)

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

using namespace std;

int a[50000];

int main() {
  int n;
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int t = 1, tem, c = 0;
  while (1) {
    c++;
    if (a[t] == 2)
      break;
    if (a[t] == 0) {
      printf("-1\n");
      return 0;
    }
    tem = t;
    t = a[tem];
    a[tem] = 0;
  }
  printf("%d\n", c);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

int a[100001];

int main() {
  int n;
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int t = 1, tem, c = 0;
  while (1) {
    c++;
    if (a[t] == 2)
      break;
    if (a[t] == 0) {
      printf("-1\n");
      return 0;
    }
    tem = t;
    t = a[tem];
    a[tem] = 0;
  }
  printf("%d\n", c);
  return 0;
}
Problem p03680 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;

  int a[10010];
  // don't use a[0]
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int buttonIndex = 1;

  set<int> pushedButton;

  while (a[buttonIndex] != 2 && pushedButton.count(a[buttonIndex]) == 0) {
    pushedButton.insert(a[buttonIndex]);
    buttonIndex = a[buttonIndex];
  }

  if (a[buttonIndex] == 2) {
    cout << pushedButton.size() + 1 << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;

  int a[100010];
  // don't use a[0]
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int buttonIndex = 1;

  set<int> pushedButton;

  while (a[buttonIndex] != 2 && pushedButton.count(a[buttonIndex]) == 0) {
    pushedButton.insert(a[buttonIndex]);
    buttonIndex = a[buttonIndex];
  }

  if (a[buttonIndex] == 2) {
    cout << pushedButton.size() + 1 << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}
Problem p03680 (C++)

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

#define ll long long
#define maxn 10000

// FILE *fin = fopen("2.in","r");
// FILE *fout = fopen("2.out", "w");

int f[maxn + 1];
int main() {
  int n, v[maxn + 1], cnt = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  int t = 1;
  int p = 0;
  while (t != 2 && p == 0) {
    if (f[t] == 1)
      p = 1;
    f[t] = 1;
    t = v[t];
    cnt++;
  }
  if (p == 1)
    printf("-1");
  else
    printf("%d", cnt);
  return 0;
}


Fixed submission:
#include <cstdio>

#define ll long long
#define maxn 100000

// FILE *fin = fopen("2.in","r");
// FILE *fout = fopen("2.out", "w");

int f[maxn + 1];
int main() {
  int n, v[maxn + 1], cnt = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  int t = 1;
  int p = 0;
  while (t != 2 && p == 0) {
    if (f[t] == 1)
      p = 1;
    f[t] = 1;
    t = v[t];
    cnt++;
  }
  if (p == 1)
    printf("-1");
  else
    printf("%d", cnt);
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int light = 1;
  int ans = 0;
  while (1) {
    light = a[light];
    if (light == 1) {
      ans = -1;
      break;
    } else if (light == 2) {
      ans++;
      break;
    } else {
      ans++;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int light = 1;
  int ans = 0;
  while (1) {
    light = a[light];
    if (light == 1) {
      ans = -1;
      break;
    } else if (light == 2) {
      ans++;
      break;
    } else if (ans > n) {
      ans = -1;
      break;
    } else {
      ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p03680 (C++)

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

using namespace std;

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

template <typename T> int indexOf(vector<T> v, T elem) {
  auto iter = find(v.begin(), v.end(), elem);
  size_t idx = distance(v.begin(), iter);
  if (idx == v.size())
    return -1;
  return idx;
}

int n;
vector<int> a;
int result;

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

void calc() {
  vector<int> v; // 押したことがあるボタン
  result = 0;
  int i = 0;
  v.push_back(i);
  for (int j = 0; j < n; j++) {
    i = a[i] - 1;
    result++;
    if (i == 1) {
      return;
    }
    if (indexOf(v, i) != -1) {
      result = -1;
      return;
    }
    v.push_back(i);
  }
  result = -1;
}

void output() { cout << result << endl; }

int main(int argc, char const *argv[]) {
  input();
  calc();
  output();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>

using namespace std;

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

template <typename T> int indexOf(vector<T> v, T elem) {
  auto iter = find(v.begin(), v.end(), elem);
  size_t idx = distance(v.begin(), iter);
  if (idx == v.size())
    return -1;
  return idx;
}

int n;
vector<int> a;
int result;

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

void calc() {
  vector<int> v; // 押したことがあるボタン
  result = 0;
  int i = 0;
  v.push_back(i);
  for (int j = 0; j < n; j++) {
    i = a[i] - 1;
    result++;
    if (i == 1) {
      return;
    }
    // if(indexOf(v,i)!=-1){
    //   result=-1;
    //   return;
    // }
    v.push_back(i);
  }
  result = -1;
}

void output() { cout << result << endl; }

int main(int argc, char const *argv[]) {
  input();
  calc();
  output();
  return 0;
}