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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
long long s1[100010], s2[100010];
int n, m;
long long all;
int nw;
long long ans = 1e18;
int main() {
  scanf("%d%d%d", &n, &m, &nw);
  nw--;
  for (int i = 1; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    nw %= m;
    if (x < nw)
      x += m;
    all += x - nw;
    s1[nw + 1]++;
    s1[x + 1]--;
    s2[nw + 1] += nw;
    s2[x + 1] -= nw;
    nw = x;
  }
  for (int i = 1; i < 2 * m; i++) {
    s1[i] += s1[i - 1];
    s2[i] += s2[i - 1];
  }
  for (int i = 0; i < m; i++) {
    long long a = s1[i], b = s2[i];
    long long t = all;
    t -= (long long)i * a - b - a;
    a = s1[i + m], b = s2[i + m];
    t -= (long long)(i + m) * a - b - a;
    ans = min(ans, t);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
long long s1[200010], s2[200010];
int n, m;
long long all;
int nw;
long long ans = 1e18;
int main() {
  scanf("%d%d%d", &n, &m, &nw);
  nw--;
  for (int i = 1; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    nw %= m;
    if (x < nw)
      x += m;
    all += x - nw;
    s1[nw + 1]++;
    s1[x + 1]--;
    s2[nw + 1] += nw;
    s2[x + 1] -= nw;
    nw = x;
  }
  for (int i = 1; i < 2 * m; i++) {
    s1[i] += s1[i - 1];
    s2[i] += s2[i - 1];
  }
  for (int i = 0; i < m; i++) {
    long long a = s1[i], b = s2[i];
    long long t = all;
    t -= (long long)i * a - b - a;
    a = s1[i + m], b = s2[i + m];
    t -= (long long)(i + m) * a - b - a;
    ans = min(ans, t);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned ll
int n, m;
ll a1[100005], a2[100005];
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cin >> n >> m;
  int from;
  ll tot = 0ll;
  cin >> from;
  from--;
  for (int i = 1; i < n; i++) {
    from %= m;
    int to;
    cin >> to;
    to--;
    if (from > to)
      to += m;
    tot += to - from;
    a1[from + 1]++;
    a1[to + 1]--;
    a2[from + 1] += from;
    a2[to + 1] -= from;
    from = to;
  }
  for (int i = 1; i < 2 * m; i++) {
    a1[i] += a1[i - 1];
    a2[i] += a2[i - 1];
  }
  ll ans = tot;
  for (int i = 0; i < m; i++) {
    ll now = tot;
    ll x = a1[i], y = a2[i];
    now -= (x * i - x - y);
    x = a1[i + m], y = a2[i + m];
    now -= (x * (i + m) - x - y);
    ans = min(ans, now);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned ll
int n, m;
ll a1[200005], a2[200005];
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cin >> n >> m;
  int from;
  ll tot = 0ll;
  cin >> from;
  from--;
  for (int i = 1; i < n; i++) {
    from %= m;
    int to;
    cin >> to;
    to--;
    if (from > to)
      to += m;
    tot += to - from;
    a1[from + 1]++;
    a1[to + 1]--;
    a2[from + 1] += from;
    a2[to + 1] -= from;
    from = to;
  }
  for (int i = 1; i < 2 * m; i++) {
    a1[i] += a1[i - 1];
    a2[i] += a2[i - 1];
  }
  ll ans = tot;
  for (int i = 0; i < m; i++) {
    ll now = tot;
    ll x = a1[i], y = a2[i];
    now -= (x * i - x - y);
    x = a1[i + m], y = a2[i + m];
    now -= (x * (i + m) - x - y);
    ans = min(ans, now);
  }
  cout << ans << endl;
  return 0;
}
Problem p03677 (C++)

Buggy submission (status: Runtime Error):
// ARC 077E
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
int a[111111], b[111111], e[111111], f[111111];
LL s[222222];
vector<int> B[111111], E[111111];
int main() {
  int n, m, i, j, x, sf;
  LL ss, ans, k;
  cin >> n >> m;
  for (i = 1; i <= n; i = i + 1)
    cin >> a[i];
  k = 0;
  for (i = 1; i < n; i = i + 1) {
    b[i] = a[i] + 1;
    e[i] = a[i + 1] + 1;
    if (b[i] > e[i])
      e[i] += m;
    k += (LL)e[i] - b[i];
    B[b[i]].push_back(i);
    E[e[i]].push_back(i);
  }
  s[1] = 0;
  ss = 0;
  sf = 0;
  for (i = 2; i <= m + m + 1; i = i + 1) {
    for (j = 0; j < B[i].size(); j = j + 1) {
      x = B[i][j];
      f[x] = 1;
      sf++;
      ss += (LL)b[x] - 1;
    }
    for (j = 0; j < E[i].size(); j = j + 1) {
      x = E[i][j];
      f[x] = 0;
      sf--;
      ss -= (LL)b[x] - 1;
    }
    s[i] = (LL)sf * (i - 1) - ss;
  }
  ans = 0;
  for (i = 1; i <= m; i = i + 1)
    ans = max(ans, s[i] + s[m + i]);
  cout << (LL)k - ans << endl;
  return 0;
}

Fixed submission:
// ARC 077E
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
int a[111111], b[111111], e[111111], f[111111];
LL s[222222];
vector<int> B[222222], E[222222];
int main() {
  int n, m, i, j, x, sf;
  LL ss, ans, k;
  cin >> n >> m;
  for (i = 1; i <= n; i = i + 1)
    cin >> a[i];
  k = 0;
  for (i = 1; i < n; i = i + 1) {
    b[i] = a[i] + 1;
    e[i] = a[i + 1] + 1;
    if (b[i] > e[i])
      e[i] += m;
    k += (LL)e[i] - b[i];
    B[b[i]].push_back(i);
    E[e[i]].push_back(i);
  }
  s[1] = 0;
  ss = 0;
  sf = 0;
  for (i = 2; i <= m + m + 1; i = i + 1) {
    for (j = 0; j < B[i].size(); j = j + 1) {
      x = B[i][j];
      f[x] = 1;
      sf++;
      ss += (LL)b[x] - 1;
    }
    for (j = 0; j < E[i].size(); j = j + 1) {
      x = E[i][j];
      f[x] = 0;
      sf--;
      ss -= (LL)b[x] - 1;
    }
    s[i] = (LL)sf * (i - 1) - ss;
  }
  ans = 0;
  for (i = 1; i <= m; i = i + 1)
    ans = max(ans, s[i] + s[m + i]);
  cout << (LL)k - ans << endl;
  return 0;
}
Problem p03677 (C++)

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

typedef long long LL;

const int N = 1e5 + 10;

int n, m, a[N];
LL p[N], k[N];

void Init() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
}

void add(int l, int r) {
  k[l]++;
  k[r + 1]--;
  p[r + 1] -= r - l + 1;
}

void Solve() {
  LL tot = 0ll;
  for (int i = 1; i < n; ++i) {
    int l = a[i], r = a[i + 1];
    if (r < l)
      r += m;
    tot += r - l;
    add(l + 2, r);
  }
  for (int i = 1; i <= m * 2; ++i)
    k[i] += k[i - 1];
  for (int i = 1; i <= m * 2; ++i)
    p[i] += p[i - 1] + k[i];
  LL ans = 1e18;
  for (int i = 1; i <= m; ++i)
    ans = std::min(ans, tot - p[i] - p[i + m]);
  printf("%lld\n", ans);
}

int main() {
  Init();
  Solve();
  return 0;
}

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

typedef long long LL;

const int N = 2e5 + 10;

int n, m, a[N];
LL p[N], k[N];

void Init() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
}

void add(int l, int r) {
  k[l]++;
  k[r + 1]--;
  p[r + 1] -= r - l + 1;
}

void Solve() {
  LL tot = 0ll;
  for (int i = 1; i < n; ++i) {
    int l = a[i], r = a[i + 1];
    if (r < l)
      r += m;
    tot += r - l;
    add(l + 2, r);
  }
  for (int i = 1; i <= m * 2; ++i)
    k[i] += k[i - 1];
  for (int i = 1; i <= m * 2; ++i)
    p[i] += p[i - 1] + k[i];
  LL ans = 1e18;
  for (int i = 1; i <= m; ++i)
    ans = std::min(ans, tot - p[i] - p[i + m]);
  printf("%lld\n", ans);
}

int main() {
  Init();
  Solve();
  return 0;
}
Problem p03678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
int i, j, c, ne[maxn];
char S[maxn];
ll num[maxn][26], l, r, T, n;
ll f(ll len) {
  if (len <= n)
    return num[len][c];
  if (len <= n * 2)
    return num[n][c] + num[len - n][c];
  ll f1 = num[n][c], f2 = num[n][c] + num[T][c], l1 = n, l2 = n + T;
  for (; len > l1 + l2;)
    swap(f1, f2), f2 += f1, swap(l1, l2), l2 += l1;
  return f2 + f(len - l2);
}
ll work(ll len) {
  if (n % T == 0)
    return len / T * num[T][c] + num[len % T][c];
  return f(len);
}
int main() {
  scanf("%s", S + 1);
  n = strlen(S + 1), n >>= 1;
  fo(i, 1, n) {
    fo(j, 0, 25) num[i][j] = num[i - 1][j];
    num[i][S[i] - 'a']++;
  }
  fo(i, 2, n) {
    for (; j && S[j + 1] != S[i]; j = ne[j])
      ;
    if (S[j + 1] == S[i])
      j++;
    ne[i] = j;
  }
  T = n - j;
  scanf("%lld%lld", &l, &r);
  fo(c, 0, 25) printf("%lld ", work(r) - work(l - 1));
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
int i, j, c, ne[maxn];
char S[maxn];
ll num[maxn][26], l, r, T, n;
ll f(ll len) {
  if (len <= n)
    return num[len][c];
  if (len <= n * 2)
    return num[n][c] + num[len - n][c];
  ll f1 = num[n][c], f2 = num[n][c] + num[T][c], l1 = n, l2 = n + T;
  for (; len > l1 + l2;)
    swap(f1, f2), f2 += f1, swap(l1, l2), l2 += l1;
  return f2 + f(len - l2);
}
ll work(ll len) {
  if (n % T == 0)
    return len / T * num[T][c] + num[len % T][c];
  return f(len);
}
int main() {
  scanf("%s", S + 1);
  n = strlen(S + 1), n >>= 1;
  fo(i, 1, n) {
    fo(j, 0, 25) num[i][j] = num[i - 1][j];
    num[i][S[i] - 'a']++;
  }
  j = 0;
  fo(i, 2, n) {
    for (; j && S[j + 1] != S[i]; j = ne[j])
      ;
    if (S[j + 1] == S[i])
      j++;
    ne[i] = j;
  }
  T = n - j;
  scanf("%lld%lld", &l, &r);
  fo(c, 0, 25) printf("%lld ", work(r) - work(l - 1));
}
Problem p03678 (C++)

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

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 1e5 + 7;

char s[N];
int len, n, m;
ll L, R;
int app[N][26], p[N];

ll count(ll a, ll b) {
  if (a <= (n << 1))
    return a <= n ? app[a][b] : app[n][b] + app[a - n][b];
  ll f1 = n, f2 = m, k1 = app[n][b], k2 = app[m][b], k3, f3;
  while (f1 * 2 < a) {
    f3 = f1 + f2;
    f2 = f1;
    f1 = f3;
    k3 = k1 + k2;
    k2 = k1;
    k1 = k3;
  }
  return k3 + count(a - f1, b);
}

int main() {
  scanf("%s", s + 1);
  len = strlen(s + 1);
  n = len >> 1;
  cin >> L >> R;
  --L;
  fo(i, 1, n) {
    fo(l, 0, 25) app[i][l] = app[i - 1][l];
    ++app[i][s[i] - 97];
  }
  for (int j = 0, i = 2; i <= n; ++i) {
    while (j != 0 && s[j + 1] != s[i])
      j = p[j];
    if (s[j + 1] == s[i])
      ++j;
    p[i] = j;
  }
  m = n - p[n];
  fo(i, 0, 25) printf("%lld ", count(R, i) - count(L, i));
}

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

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 22e4;

char s[N];
int len, n, m;
ll L, R;
int app[N][26], p[N];

ll count(ll a, ll b) {
  if (a <= (n << 1))
    return a <= n ? app[a][b] : app[n][b] + app[a - n][b];
  ll f1 = n, f2 = m, k1 = app[n][b], k2 = app[m][b], k3, f3;
  while (f1 * 2 < a) {
    f3 = f1 + f2;
    f2 = f1;
    f1 = f3;
    k3 = k1 + k2;
    k2 = k1;
    k1 = k3;
  }
  return k3 + count(a - f1, b);
}

int main() {
  scanf("%s", s + 1);
  len = strlen(s + 1);
  n = len >> 1;
  cin >> L >> R;
  --L;
  fo(i, 1, n) {
    fo(l, 0, 25) app[i][l] = app[i - 1][l];
    ++app[i][s[i] - 97];
  }
  for (int j = 0, i = 2; i <= n; ++i) {
    while (j != 0 && s[j + 1] != s[i])
      j = p[j];
    if (s[j + 1] == s[i])
      ++j;
    p[i] = j;
  }
  m = n - p[n];
  fo(i, 0, 25) printf("%lld ", count(R, i) - count(L, i));
}
Problem p03679 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

#define ll long long

int main() {
  ll X, A, B, N, K, sum = 0, l = 0, r = 0, tmp = 1000000000000000000LL;
  ;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  if (B - A <= 0) {
    cout << "delicious" << endl;
  } else if (B - A <= X) {
    cout << "safe" << endl;
  } else {
    cout << "dangerous" << endl;
  }
}

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

#define ll long long

int main() {
  ll X, A, B, N, K, sum = 0, l = 0, r = 0, tmp = 1000000000000000000LL;
  ;
  cin >> X >> A >> B;
  if (B - A <= 0) {
    cout << "delicious" << endl;
  } else if (B - A <= X) {
    cout << "safe" << endl;
  } else {
    cout << "dangerous" << endl;
  }
}
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 here = 1;

  for (int i = 0; i < N + 10; i++) {
    if (here == 2) {
      cout << i << endl;
      break;
    } else {
      here = A.at(i - 1);
    }

    if (i > N) {
      cout << -1 << endl;
      break;
    }
  }
}


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 here = 1;

  for (int i = 0; i < N + 10; i++) {
    if (here == 2) {
      cout << i << endl;
      break;
    } else {
      here = A.at(here - 1);
    }

    if (i > N) {
      cout << -1 << endl;
      break;
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

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

  int button = 0;
  int cnt = 0;
  rep(i, n) {
    button = a[button];
    cnt++;
    if (button == 1) {
      cout << cnt << endl;
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

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

  int button = 0;
  int cnt = 0;
  rep(i, n) {
    button = a[button];
    cnt++;
    if (button == 1) {
      cout << cnt << endl;
      return 0;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int cunt;
int main() {
  int N;
  cin >> N;

  a.push_back(-1);

  int ac;
  for (int i = 1; i <= N; i++) {
    cin >> ac;
    a.push_back(ac);
  }
  int c = 1;
  while (c != 2) {
    c = a[c];
    cunt++;
    if (c == 1) {
      cout << -1 << "\n";
      return 0;
    }
  }
  cout << cunt << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int cunt;
int main() {
  int N;
  cin >> N;

  a.push_back(-1);

  int ac;
  for (int i = 1; i <= N; i++) {
    cin >> ac;
    a.push_back(ac);
  }
  int c = 1;
  while (c != 2) {
    c = a[c];
    cunt++;
    if (c == 1 || cunt > N) {
      cout << -1 << "\n";
      return 0;
    }
  }
  cout << cunt << "\n";

  return 0;
}
Problem p03680 (C++)

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

int main() {
  int N, i, k = 0;
  cin >> N;
  vector<int> a(N);
  for (i = 0; i < N; ++i)
    cin >> a.at(i);
  i = 0;
  do {
    ++k;
    i = a.at(i - 1);
    if (i == 2)
      break;
  } while (--N);
  if (N == 0)
    cout << -1 << endl;
  else
    cout << k << endl;
}


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

int main() {
  int N, i, k = 0;
  cin >> N;
  vector<int> a(N);
  for (i = 0; i < N; ++i)
    cin >> a.at(i);
  i = 0;
  do {
    ++k;
    i = a.at(i) - 1;
    if (i == 1)
      break;
  } while (--N);
  if (N == 0)
    cout << -1 << endl;
  else
    cout << k << endl;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, n, a[10001], flash, flg;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  flash = a[1];
  flg = 0;
  for (i = 1; i <= n; i++) {
    if (flash == 2) {
      flg = 1;
      break;
    }
    flash = a[flash];
  }
  if (flg == 1)
    cout << i << endl;
  else
    cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, n, a[100001], flash, flg;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  flash = a[1];
  flg = 0;
  for (i = 1; i <= n; i++) {
    if (flash == 2) {
      flg = 1;
      break;
    }
    flash = a[flash];
  }
  if (flg == 1)
    cout << i << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iostream>
using namespace std;

int main() {
  int n, a[100000], b[100000], cnt = 1;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int i = a[0] - 1;
  while (1) {
    // cout<<"___"<<i<<"___"<<cnt<<endl;
    if (i == 1) {
      cout << cnt << endl;
      break;
    } else {
      i = a[i] - 1;
      b[cnt] = a[i] - 1;
      cnt++;
      for (int j = 0; j < cnt - 2; j++)
        if (a[i] - 1 == b[j]) {
          cout << -1 << endl;
          goto fin;
        }
    }
  }
fin:
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iostream>
using namespace std;

int main() {
  int n, a[100000], b[100000], cnt = 1;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int i = a[0] - 1;
  while (1) {
    // cout<<"___"<<i<<"___"<<cnt<<endl;
    if (i == 1) {
      cout << cnt << endl;
      break;
    } else {
      i = a[i] - 1;
      b[cnt] = a[i] - 1;
      cnt++;
      if (cnt > n) {
        cout << -1 << endl;
        goto fin;
      }
    }
  }
fin:
  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> vec(n);
  int count = 0;

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

  int next = 0;
  while (1) {
    if (next == 1) {
      cout << count << endl;
      break;
    }
    if (count > n) {
      cout << -1 << endl;
    }
    next = vec[next];
    count++;
  }
}


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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  int count = 0;

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

  int next = 0;
  while (1) {
    if (next == 1) {
      cout << count << endl;
      break;
    }
    if (count > n) {
      cout << -1 << endl;
      break;
    }
    next = vec[next];
    count++;
  }
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  vll a(n + 1);
  re(i, n) std::cin >> a[i + 1];
  vll use(n, 0);
  use[1] = 1;
  ll now = 1;
  for (int i = 0; i < n; i++) {
    if (use[a[now]] == 1) {
      std::cout << -1 << '\n';
      return 0;
    } else {
      now = a[now];
      use[now] = 1;
      if (now == 2) {
        std::cout << i + 1 << '\n';
        return 0;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  vll a(n + 1);
  re(i, n) std::cin >> a[i + 1];
  vll use(n + 1, 0);
  use[1] = 1;
  ll now = 1;
  for (int i = 0; i < n; i++) {
    if (use[a[now]] == 1) {
      std::cout << -1 << '\n';
      return 0;
    } else {
      now = a[now];
      use[now] = 1;
      if (now == 2) {
        std::cout << i + 1 << '\n';
        return 0;
      }
    }
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef long double ld;
int n, a[100001];
bool b[100001];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int sum = 0, x = 1;
  b[1] = true;
  while (x != 2) {
    x = a[x];
    if (b[x]) {
      cout << -1 << endl;
      return 0;
    }
    sum++;
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef long double ld;
int n, a[100001];
bool b[100001];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int sum = 0, x = 1;
  b[1] = true;
  while (x != 2) {
    x = a[x];
    if (b[x]) {
      cout << -1 << endl;
      return 0;
    }
    b[x] = 1;
    sum++;
  }
  cout << sum << 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 < n; i++)
#define puts(i) cout << i << endl
#define pi 3.14159265358979
using ll = long long;
using P = pair<int, int>;

ll divisor = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  int now = 1;
  now = a[now - 1];
  int count = 1;
  while (now != 2) {
    if (now == 1) {
      puts(-1);
      return 0;
    }
    now = a[now - 1];
    count++;
  }
  puts(count);
}

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

ll divisor = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  int now = 1;
  now = a[now - 1];
  int count = 1;
  while (now != 2) {
    if (now == 1 || count > n) {
      puts(-1);
      return 0;
    }
    now = a[now - 1];
    count++;
  }
  puts(count);
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, cnt = 1;
  cin >> N;
  vector<int> a(N);

  for (int nj = 0; nj < N; nj++) {
    cin >> a.at(nj);
  }
  int ni = 0;
  while (2 != a.at(ni)) {
    cnt++;
    ni = a.at(ni);
    if (N == cnt) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << cnt << endl;
  return 0;
}

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

int main() {
  int N, cnt = 1;
  cin >> N;
  vector<int> a(N);

  for (int nj = 0; nj < N; nj++) {
    cin >> a.at(nj);
  }
  int ni = 0;
  while (2 != a.at(ni)) {
    cnt++;
    ni = a.at(ni) - 1;
    if (N == cnt) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int box[100001];
  int flg1[100001];
  int x;
  int i;
  int flg;
  int cnt;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> box[i];
    flg1[i] = 0;
  }
  cnt = 1;
  x = box[1];
  flg1[1] = 1;
  flg = 0;
  while (1) {
    if (flg1[x] == 1) {
      flg = 1;
      break;
    }
    if (x == 2)
      break;
    x = box[x];
    cnt++;
  }
  if (flg == 1)
    cout << -1 << endl;
  else
    cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int box[100001];
  int flg1[100001];
  int x;
  int i;
  int flg;
  int cnt;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> box[i];
    flg1[i] = 0;
  }
  cnt = 1;
  x = box[1];
  flg1[1] = 1;
  flg = 0;
  while (1) {
    if (flg1[x] == 1) {
      flg = 1;
      break;
    }
    if (x == 2)
      break;
    flg1[x] = 1;
    x = box[x];
    cnt++;
  }
  if (flg == 1)
    cout << -1 << endl;
  else
    cout << cnt << endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):

// B - Trained?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int a[100000];
bool pressed[100000];

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

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

  int i = 0;
  int cnt = 0;
  pressed[0] = true;
  int ans;
  while (1) {
    cnt++;
    i = a[i];
    if (pressed[i]) {
      ans = -1;
      break;
    }

    if (i == 1) {
      ans = cnt;
      break;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:

// B - Trained?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int a[100000];
bool pressed[100000];

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

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

  int i = 0;
  int cnt = 0;
  pressed[0] = true;
  int ans;
  while (1) {
    cnt++;
    i = a[i];
    if (pressed[i]) {
      ans = -1;
      break;
    }

    if (i == 1) {
      ans = cnt;
      break;
    }

    pressed[i] = true;
  }

  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;

  vector<int> a(N), b(N + 1);
  b.at(0) = 1;

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

  bool flag = false;
  for (int i = 0; i < N; i++) {
    b.at(i + 1) = a.at(b.at(i) - 1);
    if (b.at(i + 1) == 2) {
      cout << i + 1 << endl;
      break;
    }
    for (int j = 0; j < i; j++) {
      if (b.at(i + 1) == b.at(j)) {
        cout << -1 << endl;
        flag = true;
        if (flag)
          break;
      }
    }
    if (flag)
      break;
  }
}

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

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

  vector<int> a(N), b(N + 1);
  b.at(0) = 1;

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

  bool flag = false;
  for (int i = 0; i < N; i++) {
    b.at(i + 1) = a.at(b.at(i) - 1);
    if (b.at(i + 1) == 2) {
      cout << i + 1 << endl;
      break;
    } else if (i == N - 1)
      cout << -1 << endl;
  }
}
Problem p03680 (C++)

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

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

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

  int i = 0;
  int state[100005] = {0};
  state[0] = 1;

  while (state[i] != 2) {

    // cout << "while " << i << " " << state[i] << endl;

    ++i;

    state[i] = a[state[i - 1] - 1];

    // cout << i << " " << a[state[i-1]-1] << " " << state[i] << endl;

    if (state[i] == 2)
      break;

    if (state[i] == 1) {
      cout << -1 << endl;
      return 0;
    }

    for (int j = 0; j < i; j++) {
      // cout << "for" << endl;
      if (state[i] == state[j]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  cout << i << endl;
  return 0;
}

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

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

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

  int i = 0;
  int state[100005] = {0};
  state[0] = 1;

  while (state[i] != 2) {

    // cout << "while " << i << " " << state[i] << endl;

    ++i;

    state[i] = a[state[i - 1] - 1];

    // cout << i << " " << a[state[i-1]-1] << " " << state[i] << endl;

    if (state[i] == 2)
      break;

    if (state[i] == 1) {
      cout << -1 << endl;
      return 0;
    }

    /*
    for(int j=0; j<i; j++){
        //cout << "for" << endl;
        if(state[i] == state[j]) {
            cout << -1 << endl;
            return 0;
        }
    }
    */
    if (i > n) {
      cout << -1 << endl;
      return 0;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll n;
  cin >> n;
  ll a[n], pushed[n];
  ll i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    pushed[i] = 0;
  }
  i = 0;
  ll cnt = 0;
  while (i != 1) {
    if (pushed[i] == 1) {
      cout << -1;
      return 0;
    }
    i = a[i] - 1;
    cnt++;
  }
  cout << cnt;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll n;
  cin >> n;
  ll a[n], pushed[n];
  ll i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    pushed[i] = 0;
  }
  i = 0;
  ll cnt = 0;
  while (i != 1) {
    if (pushed[i] == 1) {
      cout << -1;
      return 0;
    }
    pushed[i] = 1;
    i = a[i] - 1;
    cnt++;
  }
  cout << cnt;
  return 0;
}
Problem p03680 (C++)

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

#define MAX 10000

using namespace std;

int main() {
  int N;
  vector<int> a(MAX);
  int now;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  int flag = 0;
  int count = 0;
  now = 1;
  for (int i = 1; i <= N; i++) {
    if (now == 2) {
      flag = 1;
      break;
    }
    now = a[now]; // オス
    count++;
  }

  if (flag)
    cout << count << endl;
  else
    cout << -1 << endl;

  return 0;
}

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

#define MAX 100005

using namespace std;

int main() {
  int N;
  vector<int> a(MAX);
  int now;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  int flag = 0;
  int count = 0;
  now = 1;
  for (int i = 1; i <= N; i++) {
    if (now == 2) {
      flag = 1;
      break;
    }
    now = a[now]; // オス
    count++;
  }

  if (flag)
    cout << count << 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, a;
  cin >> N;

  vector<int> button;
  for (int i = 1; i <= N; i++) {
    cin >> a;
    button.push_back(a);
  }

  int ite = 1, count = 0;
  bool flg = false;
  for (int i = 1; i < 1000000000000000; i++) {
    ite = button[ite - 1];
    count++;
    if (ite == 2) {
      flg = true;
      break;
    }
  }
  if (flg) {
    cout << count << endl;
  } else {
    cout << "-1" << endl;
  }
}


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

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

  vector<int> button;
  for (int i = 1; i <= N; i++) {
    cin >> a;
    button.push_back(a);
  }

  int ite = 1, count = 0;
  bool flg = false;
  for (int i = 1; i < N; i++) {
    ite = button[ite - 1];
    count++;
    if (ite == 2) {
      flg = true;
      break;
    }
  }
  if (flg) {
    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;
  int count = 0;
  int temp0 = 1;
  int temp1 = 1;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  while (temp1 != -1 || temp1 != 2) {
    temp1 = a.at(temp0 - 1);
    a.at(temp0 - 1) = -1;
    temp0 = temp1;
    count++;
    if (temp1 == -1)
      count = -1;
  }
  cout << count << endl;
}

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

int main() {
  int N;
  int count = 0;
  int temp0 = 1;
  int temp1 = 1;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  while (temp1 != -1 && temp1 != 2) {
    temp1 = a.at(temp0 - 1);
    a.at(temp0 - 1) = -1;
    temp0 = temp1;
    count++;
    if (temp1 == -1)
      count = -1;
  }
  cout << count << endl;
}
Problem p03680 (C++)

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

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

  vector<int> W(N, 0);
  W.at(0) = 1;
  int t = 0;

  for (int i = 0; i < N; i++) {
    W.at(i + 1) = V.at(W.at(i) - 1);
    if (W.at(i) == 2) {
      t = 1;
      cout << i << endl;
      break;
    }
  }
  if (t == 0)
    cout << "-1" << endl;
}


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++) {
    cin >> V.at(i);
  }

  vector<int> W(N + 1, 0);
  W.at(0) = 1;
  int t = 0;

  for (int i = 0; i < N; i++) {
    W.at(i + 1) = V.at(W.at(i) - 1);
    if (W.at(i) == 2) {
      t = 1;
      cout << i << endl;
      break;
    }
  }
  if (t == 0)
    cout << "-1" << endl;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

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

  ever.push_back(0);
  int num = 0, count = 0;
  while (find(ever.begin(), ever.end(), a[num]) == ever.end() &&
         count <= N + 1) {
    if (a[num] == 1) {
      cout << ever.size();
      return 0;
    } else {
      ever.push_back(a[num]);
      num = a[num];
      count++;
    }
  }
  cout << -1;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

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

  ever.push_back(0);
  int num = 0, count = 0;
  while (count <= N + 1) {
    if (a[num] == 1) {
      cout << ever.size();
      return 0;
    } else {
      ever.push_back(a[num]);
      num = a[num];
      count++;
    }
  }
  cout << -1;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main(void) {
  bool history[100001];
  int list[100001];
  int n;

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", list + i);

  for (int i = 0; i <= n; i++)
    history[i] = false;
  history[1] = true;

  int i = 1;
  int count = 0;
  while (1) {
    i = list[i];
    count++;
    if (i == 2 || history[i])
      break;
  }
  if (i == 2)
    printf("%d\n", count);
  else
    printf("-1");
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  bool history[100001];
  int list[100001];
  int n;

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", list + i);

  for (int i = 0; i <= n; i++)
    history[i] = false;
  history[1] = true;

  int i = 1;
  int count = 0;
  while (1) {
    i = list[i];
    count++;
    if (i == 2 || history[i])
      break;
    history[i] = true;
  }
  if (i == 2)
    printf("%d\n", count);
  else
    printf("-1");
  return 0;
}
Problem p03680 (C++)

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

int main() {
  // 入力
  int n;
  int a[10010];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  // ２のボタンが光っている状態をtrue、光っていない状態をfalseとする関数flagを定義する。
  bool flag = false;
  // １しか光っていない状態では１しか押せない。次には１を押して光るボタンを押すしかできない。
  int temp = 1; // 光っているボタンの数字
  int ans = 0;  // ボタンを押した回数
  for (int i = 1; i <= n; i++) {
    if (a[temp] == 2) { // ２が光れば終わり
      flag = true;
      ans++;
      break;
    } else if (a[temp] == 1 || a[temp] == temp)
      break;
    else { // 次に光るボタンを押していく
      temp = a[temp];
      ans++;
    }
  }
  if (flag)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}

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

int main() {
  // 入力
  int n;
  int a[100010];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  // ２のボタンが光っている状態をtrue、光っていない状態をfalseとする関数flagを定義する。
  bool flag = false;
  // １しか光っていない状態では１しか押せない。次には１を押して光るボタンを押すしかできない。
  int temp = 1; // 光っているボタンの数字
  int ans = 0;  // ボタンを押した回数
  for (int i = 1; i <= n; i++) {
    if (a[temp] == 2) { // ２が光れば終わり
      flag = true;
      ans++;
      break;
    } else if (a[temp] == 1 || a[temp] == temp)
      break;
    else { // 次に光るボタンを押していく
      temp = a[temp];
      ans++;
    }
  }
  if (flag)
    cout << ans << 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> dir(n);
  for (int i = 0; i < n; i++) {
    int d;
    cin >> d;
    dir.at(i) = d - 1;
  }
  vector<bool> passed(n, false);
  int idx = 0;
  int counter = 0;
  passed.at(0) = true;
  while (idx != 1) {
    idx = dir.at(idx);
    counter++;
    if (passed.at(idx)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << counter << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> dir(n);
  for (int i = 0; i < n; i++) {
    int d;
    cin >> d;
    dir.at(i) = d - 1;
  }
  vector<bool> passed(n, false);
  int idx = 0;
  int counter = 0;
  passed.at(0) = true;
  while (idx != 1) {
    idx = dir.at(idx);
    counter++;
    if (passed.at(idx)) {
      cout << -1 << endl;
      return 0;
    } else {
      passed.at(idx) = true;
    }
  }
  cout << counter << endl;
  return 0;
}
Problem p03680 (C++)

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

const int N = 100010;

int n;
int a[N];
bool vis[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  int now = 1, cnt = 1;
  for (;; ++cnt) {
    vis[now] = true;
    now = a[now];
    if (now == 2) {
      return printf("%d\n", cnt);
    }
    if (vis[now]) {
      break;
    }
  }
  return printf("-1\n"), 0;
}

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

const int N = 100010;

int n;
int a[N];
bool vis[N];

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

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

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

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = [int(input()) for _ in range(N)]

now = 0
count = 0
while True:
    if now == 0 and count != 0:
        print(-1)
        break

    if now == 1:
        print(count)
        break

    now = A[now] - 1
    count += 1


Fixed submission:
N = int(input())
A = [int(input()) for _ in range(N)]

now = 0
count = 0
while True:
    if count >= N:
        print(-1)
        break

    if now == 1:
        print(count)
        break

    now = A[now] - 1
    count += 1
Problem p03680 (C++)

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

int main() {
  int N;
  cin >> N;
  int l = 1;
  int p = 0;
  vector<int> vec(N);
  for (int r = 0; r < N + 2; r++) {
    for (int i = 0; i < N; i++) {
      cin >> vec.at(i);
    }
    l = vec.at(l - 1);
    p++;
    if (l == 2) {
      cout << p << endl;
      break;
    } else if (p >= N) {
      cout << "-1" << endl;
      break;
    }
  }
}


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

int main() {
  int N;
  cin >> N;
  int l = 1;
  int p = 0;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  for (int m = 0; m < N + 1; m++) {
    l = vec.at(l - 1);
    p++;
    if (l == 2) {
      cout << p << endl;
      break;
    } else if (p >= N) {
      cout << "-1" << endl;
      break;
    }
  }
}
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input().strip())
a = [int(input().strip()) for _ in range(n)]
visited = [False] * n
visited[0] = True
now = 0
ans = 0
while True:
    now = a[now] - 1
    ans += 1
    if now == 1:
        print(ans)
        exit()
    elif visited[now]:
        print(-1)
        exit()


Fixed submission:
n = int(input().strip())
a = [int(input().strip()) for _ in range(n)]
visited = [False] * n
visited[0] = True
now = 0
ans = 0
while True:
    now = a[now] - 1
    ans += 1
    if now == 1:
        print(ans)
        exit()
    elif visited[now]:
        print(-1)
        exit()
    visited[now] = True
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = [0] + [int(input()) for i in range(n)]
c = a[1]
p = 1
while c > 2:
    c = a[c]
    p += 1

if c == 2:
    print(p)
else:
    print(-1)


Fixed submission:
n = int(input())
a = [0] + [int(input()) for i in range(n)]
c = a[1]
p = 1
while c > 2:
    d = a[c]
    a[c] = 0
    c = d
    p += 1

if c == 2:
    print(p)
else:
    print(-1)
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

cnt = 0
lit = 0

while lit != 1 and (lit != 0 or cnt == 0):
    lit = a[lit] - 1
    cnt += 1

if lit == 1:
    print(cnt)
else:
    print(-1)


Fixed submission:
N = int(input())
A = [int(input()) for _ in range(N)]
i = 0
done = {a - 1: False for a in A}
num = 0
while True:
    i = A[i] - 1
    num += 1
    if i == 1:
        print(num)
        exit()
    if done[i]:
        print(-1)
        exit()
    done[i] = True
Problem p03680 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
buttons = [input() for i in range(n)]
buttons_int = [int(b) - 1 for b in buttons]
button = 0
count = 0
history_list = []
i = 0
while i < n:
    if button == i:
        button = buttons_int[i]
        count += 1
        if button == 1:
            print(count)
            exit()
        if i in history_list:
            print(-1)
            exit()
        else:
            history_list.append(i)
        i = 0
    else:
        i += 1


Fixed submission:
n = int(input())
buttons = [input() for i in range(n)]
buttons_int = [int(b) - 1 for b in buttons]
button = 0
for count in range(1, n + 1):
    button = buttons_int[button]
    if button == 1:
        print(count)
        exit()
print(-1)
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

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

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

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

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

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

  cout << ans << endl;

  return 0;
}
Problem p03680 (C++)

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

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


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

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

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

int main(void) {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int count = 0;
  for (int c = 1; c != 2;) {
    c = A[c];
    count++;
    if (count == N) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << count << endl;
}


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

int main(void) {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int count = 0;
  for (int c = 1; c != 2;) {
    c = A[c - 1];
    count++;
    if (count == N) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << count << endl;
}