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

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>
typedef long long ll;

// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define D()
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int n, now;
  std::cin >> n;
  int ans[10001] = {0};
  for (int i = 1; i <= n; i++) {
    std::cin >> ans[i];
  }
  int a = -1, cnt = 0;
  now = ans[1];
  for (int i = 0; i < n; i++) {
    cnt++;
    if (now == 2) {
      a = cnt;
      break;
    }
    now = ans[now];
  }
  std::cout << a << std::endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>
typedef long long ll;

// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define D()
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007    // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int n, now;
  std::cin >> n;
  int ans[100001] = {0};
  for (int i = 1; i <= n; i++) {
    std::cin >> ans[i];
  }
  int a = -1, cnt = 0;
  now = ans[1];
  for (int i = 0; i < n; i++) {
    cnt++;
    if (now == 2) {
      a = cnt;
      break;
    }
    now = ans[now];
  }
  std::cout << a << std::endl;
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;

void b_trained(void) {
  int n;
  int a[10010];
  int cnt = 0;
  int next_button = 1;
  int now_button = 1;
  bool find = false;

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

  for (int i = 1; i <= n; i++) {
    now_button = next_button;
    if (now_button == 2) {
      find = true;
      break;
    }
    next_button = a[now_button];
    cnt++;
  }

  if (!find)
    cnt = -1;
  cout << cnt << endl;
}

int main() {
  b_trained();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;

void b_trained(void) {
  int n;
  int a[100010];
  int cnt = 0;
  int next_button = 1;
  int now_button = 1;
  bool find = false;

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

  for (int i = 1; i <= n; i++) {
    now_button = next_button;
    if (now_button == 2) {
      find = true;
      break;
    }
    next_button = a[now_button];
    cnt++;
  }

  if (!find)
    cnt = -1;
  cout << cnt << endl;
}

int main() {
  b_trained();
  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(100);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  bool check = false;
  int count = 0;
  int tmp = a.at(0);
  for (int i = 0; i < n; i++) {
    count++;
    if (tmp == 2) {
      check = true;
      break;
    }
    tmp = a.at(tmp - 1);
  }
  if (check)
    cout << count << 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.at(i);
  }
  bool check = false;
  int count = 0;
  int tmp = a.at(0);
  for (int i = 0; i < n; i++) {
    count++;
    if (tmp == 2) {
      check = true;
      break;
    }
    tmp = a.at(tmp - 1);
  }
  if (check)
    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;
  vector<int> a(10000);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  bool check = false;
  int count = 0;
  int tmp = a.at(0);
  for (int i = 0; i < n; i++) {
    count++;
    if (tmp == 2) {
      check = true;
      break;
    }
    tmp = a.at(tmp - 1);
  }
  if (check)
    cout << count << endl;
  else
    cout << "-1" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(100000);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  bool check = false;
  int count = 0;
  int tmp = a.at(0);
  for (int i = 0; i < n; i++) {
    count++;
    if (tmp == 2) {
      check = true;
      break;
    }
    tmp = a.at(tmp - 1);
  }
  if (check)
    cout << count << endl;
  else
    cout << "-1" << endl;
}
Problem p03680 (C++)

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

#define ODD true
#define EVEN false

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) cin >> a[i];
  int ans = 1;

  int k = 0;
  while (a[k] != 2) {
    k = a[k] - 1;
    ans++;
    if (k == 0) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define ODD true
#define EVEN false

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) cin >> a[i];
  int ans = 1;

  int k = 0;
  while (a[k] != 2) {
    k = a[k] - 1;
    ans++;
    if (k == 0 || ans > N) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03680 (C++)

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

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

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

  return 0;
}


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

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

  int button = 1;
  for (int i = 0; i < N; i++) {
    button = a[button - 1];
    if (button == 2) {
      cout << i + 1;
      return 0;
    }
  }
  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;

  int pos = 1;
  vector<bool> been(N, false);
  been.at(0) = true;

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

  bool fin = false;

  int i = 0;

  while (!fin) {

    pos = a.at(pos - 1);

    if (pos == 2) {
      fin = true;
    } else if (been.at(pos - 1)) {
      cout << -1 << endl;
      break;
    }
    i++;
  }

  if (fin)
    cout << i << endl;
}

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

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

  int pos = 1;
  vector<bool> been(N, false);
  been.at(0) = true;

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

  bool fin = false;

  int i = 0;

  while (!fin) {

    pos = a.at(pos - 1);

    if (pos == 2) {
      fin = true;
    } else if (been.at(pos - 1)) {
      cout << -1 << endl;
      break;
    }

    been.at(pos - 1) = true;

    i++;
  }

  if (fin)
    cout << i << endl;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, m, n) for (int i = m; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;

int main() {
  int n, a[10010], b[10010];
  cin >> n;
  reps(i, n) cin >> a[i];
  b[0] = 1;
  int ans = -1, muri = 0;
  reps(j, n) {
    b[j] = a[b[j - 1]];
    if (b[j] == 2) {
      ans = j;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, m, n) for (int i = m; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;

int main() {
  int n, a[100010], b[100010];
  cin >> n;
  reps(i, n) cin >> a[i];
  b[0] = 1;
  int ans = -1, muri = 0;
  reps(j, n) {
    b[j] = a[b[j - 1]];
    if (b[j] == 2) {
      ans = j;
      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;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  int tmp = 0;
  int cnt = 0;
  bool flag = true;
  while (flag) {
    tmp = a.at(tmp);
    cnt++;
    if (cnt == n || tmp == 1)
      flag = false;
  }

  if (tmp == 1)
    cout << cnt << 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.at(i);

  int tmp = 0;
  int cnt = 0;
  bool flag = true;
  while (flag) {
    tmp = a.at(tmp) - 1;
    cnt++;
    if (cnt == n || tmp == 1)
      flag = false;
  }

  if (tmp == 1)
    cout << cnt << endl;
  else
    cout << -1 << endl;
}
Problem p03680 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, a[10001] = {0}, vis[10001] = {0}, current = 1, times = 0, panda = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (current != 2) {
    vis[current] = 1;
    current = a[current];
    if (vis[current] == 1) {
      panda = 1;
      cout << -1;
      break;
    }
    times++;
  }
  if (panda == 0)
    cout << times;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, a[100001] = {0}, vis[100001] = {0}, current = 1, times = 0, panda = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (current != 2) {
    vis[current] = 1;
    current = a[current];
    if (vis[current] == 1) {
      panda = 1;
      cout << -1;
      break;
    }
    times++;
  }
  if (panda == 0)
    cout << times;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, a[100009];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int cx = 1, cnt = 0;
  while (true) {
    cnt++;
    cx = a[cx];
    if (cx == 2) {
      cout << cnt << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, a[100009];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int cx = 1, cnt = 0;
  while (true) {
    cnt++;
    cx = a[cx];
    if (cx == 2) {
      cout << cnt << endl;
      return 0;
    }
    if (cnt >= 500000)
      break;
  }
  cout << -1 << endl;
  return 0;
}
Problem p03680 (C++)

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

int n;
vector<int> a(20000);

int main(int argc, char **argv) {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  int target = 1;

  int count = 0;
  while (count < n) {
    if (target == 2) {
      std::cout << count << std::endl;
      return 0;
    }

    target = a[target];
    count++;
  }

  std::cout << -1 << std::endl;
}


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

int n;
vector<int> a(2000000);

int main(int argc, char **argv) {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  int target = 1;

  int count = 0;
  while (count < n) {
    if (target == 2) {
      std::cout << count << std::endl;
      return 0;
    }

    target = a[target];
    count++;
  }

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

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[10010];
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
  }
  int c = 0;
  int p = 1;
  for (int i = 0; i < n; i++) {
    c++;
    p = a[p];
    if (p == 2) {
      break;
    }
  }
  if (c == n) {
    cout << "-1" << endl;
  } else {
    cout << c << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[100010];
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
  }
  int c = 0;
  int p = 1;
  for (int i = 0; i < n; i++) {
    c++;
    p = a[p];
    if (p == 2) {
      break;
    }
  }
  if (c == n) {
    cout << "-1" << endl;
  } else {
    cout << c << endl;
  }
  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++)
using namespace std;

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

  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  vector<bool> put(n, false);
  put.at(0) = true;
  bool can = true;
  int now = 0;
  int cnt = 0;
  while (can) {
    cnt++;
    now = a.at(now) - 1;
    if (now == 1)
      break;
    if (put.at(now))
      can = false;
  }

  int ans;
  if (can) {
    ans = cnt;
  } else {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}

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

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

  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  vector<bool> put(n, false);
  put.at(0) = true;
  bool can = true;
  int now = 0;
  int cnt = 0;
  while (can) {
    cnt++;
    now = a.at(now) - 1;
    if (now == 1)
      break;
    if (put.at(now))
      can = false;
    put.at(now) = true;
  }

  int ans;
  if (can) {
    ans = cnt;
  } else {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03680 (C++)

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

using namespace std;

int a[10005];

void solve() {
  int n;
  cin >> n;
  int cur;
  cur = 1;
  for (int i = 1; i <= n; ++i) {
    int ai;
    cin >> ai;
    a[i] = ai;
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    ++cnt;
    if (a[cur] == 2) {
      cout << cnt;
      return;
    } else {
      cur = a[cur];
    }
  }
  cout << -1;
}

int main() {

  solve();
  return 0;
}


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

using namespace std;

int a[100005];

void solve() {
  int n;
  cin >> n;
  int cur;
  cur = 1;
  for (int i = 1; i <= n; ++i) {
    int ai;
    cin >> ai;
    a[i] = ai;
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    ++cnt;
    if (a[cur] == 2) {
      cout << cnt;
      return;
    } else {
      cur = a[cur];
    }
  }
  cout << -1;
}

int main() {

  solve();
  return 0;
}
Problem p03680 (C++)

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

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

  int a[10000] = {
      0,
  };

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

  int counter = 1;
  int ai = a[0];
  while (0 <= N && ai != 2 && ai != -1) {
    int n = ai - 1;
    ai = a[n];
    a[n] = -1;
    counter++;
    N--;
  }

  if (ai == 2) {
    cout << counter << endl;
  } else {
    cout << -1 << endl;
  }
}

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

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

  int a[100000] = {
      0,
  };

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

  int counter = 1;
  int ai = a[0];
  while (0 <= N && ai != 2 && ai != -1) {
    int n = ai - 1;
    ai = a[n];
    a[n] = -1;
    counter++;
    N--;
  }

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

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

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

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

  int cnt = 0;
  bool isFound = false;
  vector<bool> isArr(N, false);
  isArr[0] = true;
  int pos = 0;
  while (1) {
    cnt++;
    pos = A[pos];
    if (isArr[pos]) {
      break;
    } else if (pos == 1) {
      isFound = true;
      break;
    }
  }

  cout << (isFound ? cnt : -1) << endl;
  return 0;
}


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

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

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

  int cnt = 0;
  bool isFound = false;
  vector<bool> isArr(N, false);
  isArr[0] = true;
  int pos = 0;
  while (1) {
    cnt++;
    pos = A[pos];
    if (isArr[pos]) {
      break;
    } else if (pos == 1) {
      isFound = true;
      break;
    }
    isArr[pos] = true;
  }

  cout << (isFound ? cnt : -1) << endl;
  return 0;
}
Problem p03680 (C++)

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

int main() {
  int N, c = 1;
  bool b = false;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int p = vec.at(0);
  if (p == 2) {
    cout << 1;
  } else {
    for (int i = 0; i < N - 1; i++) {
      p = vec.at(p);
      c++;
      if (p == 2) {
        cout << c;
        b = true;
        break;
      }
    }
    if (!b) {
      cout << -1;
    }
  }
}

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

int main() {
  int N, c = 1;
  bool b = false;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int p = vec.at(0);
  if (p == 2) {
    cout << 1;
  } else {
    for (int i = 0; i < N - 1; i++) {
      p = vec.at(p - 1);
      c++;
      if (p == 2) {
        cout << c;
        b = true;
        break;
      }
    }
    if (!b) {
      cout << -1;
    }
  }
}
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[i];
  }
  int cnt = 0;
  int ind = a[0];
  while (1) {
    cnt++;
    if (ind == 2) {
      cout << cnt << endl;
      return 0;
    }
    ind = a[ind - 1];
    if (ind == 1) {
      cout << -1 << 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[i];
  }
  int cnt = 0;
  int ind = a[0];
  while (1) {
    cnt++;
    if (ind == 2) {
      cout << cnt << endl;
      return 0;
    }
    ind = a[ind - 1];
    if (cnt > 100000) {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p03680 (C++)

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

bool flag[11000];
int a[11000];
int n;
int res = 0;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    flag[i] = false; // 訪れたか
  }
  int pos = 1;
  while (true) {
    if (pos == 2) {
      break;
    }
    if (flag[pos]) {
      res = -1;
      break;
    }
    flag[pos] = true;
    pos = a[pos];
    res++;
  }
  cout << res << endl;
}

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

bool flag[110000];
int a[110000];
int n;
int res = 0;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    flag[i] = false; // 訪れたか
  }
  int pos = 1;
  while (true) {
    if (pos == 2) {
      break;
    }
    if (flag[pos]) {
      res = -1;
      break;
    }
    flag[pos] = true;
    pos = a[pos];
    res++;
  }
  cout << res << endl;
}
Problem p03680 (C++)

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

using namespace std;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int a[n + 1];
  a[0] = 0;
  for (size_t i = 1; i < n + 1; i++)
    cin >> a[i];
  int ans = 1, a1 = 0, btn = a[1];
  while (true) {
    if (btn == 2)
      break;
    else if (btn == 1) {
      ans = -1;
      break;
    } else
      btn = a[btn];
    ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int a[n + 1];
  a[0] = 0;
  for (size_t i = 1; i < n + 1; i++)
    cin >> a[i];
  int ans = 1, a1 = 0, btn = a[1];
  while (true) {
    if (btn == 2)
      break;
    else if (btn == 1) {
      ans = -1;
      break;
    } else
      btn = a[btn];
    ans++;
    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;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  int now = 0, cnt = 0;
  while (true) {
    if (now == 1) {
      cout << cnt << endl;
      return 0;
    }
    if (cnt >= n) {
      cout << -1 << endl;
      return 0;
    }
    cnt++;
    now = a.at(now);
  }
}


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);
    a.at(i)--;
  }

  int now = 0, cnt = 0;
  while (true) {
    if (now == 1) {
      cout << cnt << endl;
      return 0;
    }
    if (cnt >= n) {
      cout << -1 << endl;
      return 0;
    }
    cnt++;
    now = a.at(now);
  }
}
Problem p03680 (C++)

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

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

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

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

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

using namespace std;

int main() {
  int N, i, j;
  cin >> N;
  int a[N + 1];
  for (i = 0; i < N; ++i) {
    cin >> a[i];
  }
  i = 0;
  while (true) {
    ++j;
    i = a[i];
    if (i == 2) {
      cout << j << endl;
      break;
    }
    if (N < j) {
      cout << "-1" << endl;
    }
    i = i - 1;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

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

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

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

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

  bool is2 = false;
  int cnt = 0;
  int i = 0;
  while (i < n) {
    cnt += 1;
    if (a[i] == 2) {
      is2 = true;
      break;
    }
    i = a[i] - 1;
  }

  std::cout << (is2 ? cnt : -1) << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>

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

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

  bool is2 = false;
  int cnt = 0;
  int i = 0;
  while (cnt < n) {
    cnt += 1;
    if (a[i] == 2) {
      is2 = true;
      break;
    }
    i = a[i] - 1;
  }

  std::cout << (is2 ? cnt : -1) << std::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> vec(n + 1);

  for (int i = 1; i < n + 1; i++)
    cin >> vec.at(i);
  int j = 1;
  vector<int> s(n + 1);
  for (int i = 1; i < n + 1; i++) {
    if (vec.at(j) == 2) {
      cout << i << endl;
      break;
    } else {
      s.at(i) = vec.at(j);
      for (int k = 1; k < i; k++) {
        if (vec.at(j) == s.at(k)) {
          n = -1;
          cout << "-1" << endl;
          break;
        }
      }
    }
    j = vec.at(j);
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n + 1);

  for (int i = 1; i < n + 1; i++)
    cin >> vec.at(i);
  int j = 1;
  vector<int> s(n + 1);
  for (int i = 1; i < n + 1; i++) {
    if (vec.at(j) == 2) {
      cout << i << endl;
      break;
    } else {
      if (s.at(vec.at(j)) == 1) {
        cout << "-1" << endl;
        break;
      } else {
        s.at(vec.at(j)) = 1;
      }
    }
    j = vec.at(j);
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[i] = b - 1;
  }
  int ans = 0;
  int cnt = 0;
  while (1) {
    if (cnt == 1) {
      cout << ans << endl;
      return 0;
    }
    if (cnt >= n) {
      cout << -1 << endl;
      return 0;
    }
    ans++;
    cnt = a[cnt];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[i] = b - 1;
  }
  int ans = 0;
  int cnt = 0;
  while (1) {
    if (cnt == 1) {
      cout << ans << endl;
      return 0;
    }
    if (ans >= n) {
      cout << -1 << endl;
      return 0;
    }
    ans++;
    cnt = a[cnt];
  }
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int a[100000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int t = 0;
  int s = 1;
  while (s != 2) {
    s = a[s - 1];
    t++;
    if (s == 1) {
      printf("%d\n", -1);
      break;
    }
  }
  if (s == 2) {
    printf("%d\n", t);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
int a[100000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int t = 0;
  int s = 1;
  while (s != 2) {
    s = a[s - 1];
    t++;
    if (s == 1) {
      printf("%d\n", -1);
      break;
    }
    if (t >= 100000) {
      printf("%d\n", -1);
      break;
    }
  }
  if (s == 2) {
    printf("%d\n", t);
  }
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int i, n, m, x, z, a[100001], b[100001];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    b[i] = 0;
    scanf("%d", &a[i]);
  }
  x = 0;
  z = 1;
  m = 0;
  while (1) {
    if (m == 1 || z == 2) {
      printf("%d\n", x);
      break;
    }
    if (b[z] == 0) {
      if (b[a[z]] == 0) {
        b[z] = 1;
        z = a[z];
        x++;
      } else {
        m = 1;
        x = -1;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int i, n, m, x, z, a[100001], b[100001];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    b[i] = 0;
    scanf("%d", &a[i]);
  }
  x = 0;
  z = 1;
  m = 0;
  while (1) {
    if (m == 1 || z == 2) {
      printf("%d\n", x);
      break;
    }
    if (b[a[z]] == 0) {
      b[z] = 1;
      z = a[z];
      x++;
    } else {
      m = 1;
      x = -1;
    }
  }
  return 0;
}
Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int ar[100005];
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  set<int> s;
  int ans = 0;
  int i = 1;
  s.insert(1);
  while (i != 2) {
    i = ar[i];
    if (s.count(i)) {
      cout << "-1";
      return 0;
    }
    ans++;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int ar[100005];
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  set<int> s;
  int ans = 0;
  int i = 1;
  s.insert(1);
  while (i != 2) {
    i = ar[i];
    if (s.count(i)) {
      cout << "-1";
      return 0;
    }
    s.insert(i);
    ans++;
  }
  cout << ans;
}
Problem p03680 (C++)

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

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

  for (int i = 1; i < N + 1; i++) {
    x.at(i) = a.at(x.at(i - 1)) - 1;
    count++;
    // cout << x.at(i) << endl;
    if (x.at(i) == 1) {
      can = true;
      break;
    }
  }
  if (can != true) {
    count = -1;
  }

  cout << count << 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);
  }
  vector<int> x(N);
  int count = 0;
  bool can = false;

  for (int i = 1; i < N; i++) {
    x.at(i) = a.at(x.at(i - 1)) - 1;
    count++;
    // cout << x.at(i) << endl;
    if (x.at(i) == 1) {
      can = true;
      break;
    }
  }
  if (can != true) {
    count = -1;
  }

  cout << count << 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> buttons(n);

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

  bool ok = false;
  int cnt = 0;
  int pos = 0;
  while (true) {
    int nextPos = buttons.at(pos);
    cnt++;
    if (nextPos == 2) {
      ok = true;
      break;
    }
    if (nextPos - 1 == pos) {
      break;
    }

    pos = nextPos - 1;
  }

  if (ok) {
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
}

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

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

  vector<int> buttons(n);

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

  bool ok = false;
  int cnt = 0;
  int pos = 0;
  while (true) {
    int nextPos = buttons.at(pos);
    cnt++;
    if (nextPos == 2) {
      ok = true;
      break;
    }
    if (nextPos - 1 == pos) {
      break;
    }

    if (cnt > n) {
      break;
    }

    pos = nextPos - 1;
  }

  if (ok) {
    cout << cnt << 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, cnt = 1, now;
  cin >> n;
  vector<int> buttons(n);
  for (int i = 0; i < n; i++) {
    cin >> buttons.at(i);
  }
  now = buttons.at(0);
  while (now != 2) {
    now = buttons.at(now - 1);
    cnt++;
  }
  cout << cnt << endl;
}

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

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

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

int main() {
  int N, p = 1, cnt = 0;
  cin >> N;
  bool ok = false;
  vector<int> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  for (int i = 0; i <= N; i++) {
    cnt++;
    if (data.at(p) == 2) {
      ok = true;
      break;
    } else {
      p = data[p];
    }
  }
  cout << (ok ? cnt : -1) << endl;
}


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

int main() {
  int N, p = 1, cnt = 0;
  cin >> N;
  bool ok = false;
  vector<int> data(N + 1);
  data.at(0) = 0;
  for (int i = 1; i <= N; i++) {
    cin >> data.at(i);
  }
  for (int i = 0; i <= N; i++) {
    cnt++;
    if (data.at(p) == 2) {
      ok = true;
      break;
    } else {
      p = data[p];
    }
  }
  cout << (ok ? cnt : -1) << endl;
}
Problem p03680 (C++)

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

int main() {
  int N;
  cin >> N;
  int button[100010], now;
  for (int i = 1; i <= N; ++i) {
    cin >> button[i];
  }
  bool flag = false;
  int pre, next;
  int i = 1;
  int i_ = 1;
  int count = 0;
  pre = 0;
  next = 1;
  now = 1;
  while (pre != next) {
    pre = i_; // 1 1 3
    now = i;  // 1 3 4
    i_ = now;
    next = button[now]; // 3 4 2
    if (next == 2) {
      flag = true;
      count++;
      break;
    }
    count++;
    i = next;
  }

  if (flag)
    cout << count << endl;
  else
    cout << "-1" << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  int button[100010], now;
  for (int i = 1; i <= N; ++i) {
    cin >> button[i];
  }
  bool flag = false;
  int pre, next;
  int i = 1;
  int i_ = 1;
  int count = 0;
  pre = 0;
  next = 1;
  now = 1;
  for (int j = 1; j <= N; ++j) {
    pre = i_; // 1 1 3
    now = i;  // 1 3 4
    i_ = now;
    next = button[now]; // 3 4 2
    if (next == 2) {
      flag = true;
      count++;
      break;
    }
    count++;
    i = next;
  }

  if (flag)
    cout << count << endl;
  else
    cout << "-1" << endl;
  return 0;
}
Problem p03681 (C++)

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

long long mul(long long x) {
  if (x == 2)
    return 2LL;
  return (x * mul(x - 1)) % 1000000007;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  if (abs(n - m) >= 2) {
    cout << 0 << endl;
    return 0;
  }
  long long res = (mul(n) * mul(m)) % 1000000007;
  if (m == n)
    res = res * 2 % 1000000007;
  cout << res << endl;
  return 0;
}

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

long long mul(long long x) {
  if (x == 1)
    return 1LL;
  return (x * mul(x - 1)) % 1000000007;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  if (abs(n - m) >= 2) {
    cout << 0 << endl;
    return 0;
  }
  long long res = (mul(n) * mul(m)) % 1000000007;
  if (m == n)
    res = res * 2 % 1000000007;
  cout << res << endl;
  return 0;
}
Problem p03681 (Python)

Buggy submission (status: Time Limit Exceeded):
from math import factorial

n, m = map(int, input().split())
if n == m:
    print(2 * factorial(n) ** 2 % 1000000007)
elif abs(n - m) < 2:
    print(factorial(max(n, m)) * factorial(min(n, m) % 1000000007))
else:
    print(0)


Fixed submission:
from math import factorial

n, m = map(int, input().split())
if n == m:
    print(2 * factorial(n) ** 2 % 1000000007)
elif abs(n - m) < 2:
    print(factorial(n) * factorial(m) % 1000000007)
else:
    print(0)
Problem p03681 (C++)

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define ces(d) cout << " " << endl;

int main() {

  int N, M;
  cin >> N >> M;
  int ans = 0;
  if (abs(N - M) >= 2) {
    ce(ans);

  } else {
    long long k = 1, i = 1;
    int n = N, m = M;
    while (N > 0) {
      k *= N--;
      k %= 1000000007;
    }
    while (M > 0) {
      i *= M--;
      i %= 1000000007;
    }
    while (k-- > 0) {
      ans += i;
      ans %= 1000000007;
    }

    if (n == m) {
      ce((ans * 2) % 1000000007);
    } else {
      ce(ans);
    }
  }
}

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define ces(d) cout << " " << endl;

int main() {

  int N, M;
  cin >> N >> M;
  int ans = 0;
  if (abs(N - M) >= 2) {
    ce(ans);

  } else {
    long long k = 1, i = 1;
    int n = N, m = M;
    while (N > 0) {
      k *= N--;
      k %= 1000000007;
    }
    while (M > 0) {
      i *= M--;
      i %= 1000000007;
    }
    ans = (k * i) % 1000000007;

    if (n == m) {
      ce((ans * 2) % 1000000007);
    } else {
      ce(ans);
    }
  }
}
Problem p03681 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

const ll mod = 1e9 + 7;

ll modfact(ll n) { return n * modfact(n - 1) % mod; }

int main() {
  ll n, m;
  cin >> n >> m;
  if (abs(n - m) > 1)
    cout << 0 << endl;
  else if (n == m)
    cout << 2 * modfact(n) * modfact(m) % mod << endl;
  else
    cout << modfact(n) * modfact(m) % mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

const ll mod = 1e9 + 7;

ll modfact(ll n) {
  if (n == 1)
    return 1;
  return n * modfact(n - 1) % mod;
}

int main() {
  ll n, m;
  cin >> n >> m;
  if (abs(n - m) > 1)
    cout << 0 << endl;
  else if (n == m)
    cout << 2 * modfact(n) * modfact(m) % mod << endl;
  else
    cout << modfact(n) * modfact(m) % mod << endl;
  return 0;
}
Problem p03681 (Python)

Buggy submission (status: Runtime Error):
from math import factorial

n, m = [int(i) for i in input().split()]
mod = 10e9 + 7

ans = 0
diff = abs(n - m)
if diff == 0:
    ans = (factorial(n) ** 2) * 2 % mod
elif diff == 1:
    ans = factorial(n) * factorial(m) % mod
print(ans)


Fixed submission:
from math import factorial

n, m = [int(i) for i in input().split()]
mod = 10**9 + 7

ans = 0
diff = abs(n - m)
if diff == 0:
    ans = (factorial(n) ** 2) * 2 % mod
elif diff == 1:
    ans = factorial(n) * factorial(m) % mod
print(ans)
Problem p03681 (Python)

Buggy submission (status: Time Limit Exceeded):
import math

N, M = map(int, input().split())

if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 1:
    print(math.factorial(N) * math.factorial(M))
else:
    print((math.factorial(N) ** 2 * 2) % (10**9 + 7))


Fixed submission:
import math

N, M = map(int, input().split())

if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 1:
    print((math.factorial(N) * math.factorial(M)) % (10**9 + 7))
else:
    print((math.factorial(N) ** 2 * 2) % (10**9 + 7))
Problem p03681 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())

MOD = 1_000_000_007

if abs(N - M) > 1:
    print(0)
    exit(0)

dogs = 1
for i in range(1, N + 1):
    dogs *= i
    dogs %= MOD

monks = 1
for i in range(1, M + 1):
    monks *= i
    monks %= MOD

res = (dogs * monks) % MOD
if (N + M) % 2 == 0:
    res *= 2
    res %= MOD

print(res)


Fixed submission:
N, M = map(int, input().split())

MOD = 1000000007

if abs(N - M) > 1:
    print(0)
    exit(0)

dogs = 1
for i in range(1, N + 1):
    dogs *= i
    dogs %= MOD

monks = 1
for i in range(1, M + 1):
    monks *= i
    monks %= MOD

res = (dogs * monks) % MOD
if (N + M) % 2 == 0:
    res *= 2
    res %= MOD

print(res)
Problem p03681 (C++)

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

using namespace std;

long long int MOD = 1000000007;

int main() {
  int M, D;
  cin >> M >> D;
  if (abs(M - D) > 1) {
    cout << 0;
    return 1;
  } else if (abs(M - D) == 0) {
    long long int ans = 1LL;
    for (int i = M; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }

    for (int i = D; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }
    ans *= 2;
    ans %= MOD;
    cout << ans;
  } else if (abs(M - D) == 1) {
    long long int ans = 1LL;
    for (int i = M; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }

    for (int i = D; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }
    cout << ans;
  }
}


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

using namespace std;

long long int MOD = 1000000007;

int main() {
  int M, D;
  cin >> M >> D;
  if (abs(M - D) > 1) {
    cout << 0;
    //    return 1;
  } else if (abs(M - D) == 0) {
    long long int ans = 1LL;
    for (int i = M; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }

    for (int i = D; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }
    ans *= 2;
    ans %= MOD;
    cout << ans;
  } else if (abs(M - D) == 1) {
    long long int ans = 1LL;
    for (int i = M; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }

    for (int i = D; i >= 1; i--) {
      ans *= i;
      ans %= MOD;
    }
    cout << ans;
  }
}
Problem p03682 (C++)

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

using namespace std;

typedef long long ll;
const int N = 1e6 + 10;
struct Edge {
  int u, v;
  ll w;
  bool const operator<(const Edge &a) const { return w < a.w; }
} edge[N];
int tot = 0;
struct Node {
  int u, v, ind;
} node[N];
int cmp1(Node a, Node b) { return a.u < b.u; }
int cmp2(Node a, Node b) { return a.v < b.v; }
int pre[N];
int find(int x) { return x == pre[x] ? x : find(pre[x]); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++)
    pre[i] = i;
  for (int i = 0; i < n; i++)
    scanf("%d%d", &node[i].u, &node[i].v), node[i].ind = i;
  sort(node, node + n, cmp1);
  for (int i = 1; i < n; i++)
    edge[tot].u = node[i - 1].ind, edge[tot].v = node[i].ind,
    edge[tot++].w = abs(node[i].u - node[i - 1].u);
  sort(node, node + n, cmp2);
  for (int i = 1; i < n; i++)
    edge[tot].u = node[i - 1].ind, edge[tot].v = node[i].ind,
    edge[tot++].w = abs(node[i].v - node[i - 1].v);
  sort(edge, edge + tot);
  ll ans = 0;
  for (int i = 0; i < tot; i++) {
    int u = edge[i].u, v = edge[i].v;
    ll w = edge[i].w;
    int preu = find(u), prev = find(v);
    if (preu != prev) {
      ans += w;
      pre[preu] = prev;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


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

using namespace std;

typedef long long ll;
const int N = 1e6 + 10;
struct Edge {
  int u, v;
  ll w;
  bool const operator<(const Edge &a) const { return w < a.w; }
} edge[N];
int tot = 0;
struct Node {
  int u, v, ind;
} node[N];
int cmp1(Node a, Node b) { return a.u < b.u; }
int cmp2(Node a, Node b) { return a.v < b.v; }
int pre[N];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++)
    pre[i] = i;
  for (int i = 0; i < n; i++)
    scanf("%d%d", &node[i].u, &node[i].v), node[i].ind = i;
  sort(node, node + n, cmp1);
  for (int i = 1; i < n; i++)
    edge[tot].u = node[i - 1].ind, edge[tot].v = node[i].ind,
    edge[tot++].w = abs(node[i].u - node[i - 1].u);
  sort(node, node + n, cmp2);
  for (int i = 1; i < n; i++)
    edge[tot].u = node[i - 1].ind, edge[tot].v = node[i].ind,
    edge[tot++].w = abs(node[i].v - node[i - 1].v);
  sort(edge, edge + tot);
  ll ans = 0;
  for (int i = 0; i < tot; i++) {
    int u = edge[i].u, v = edge[i].v;
    ll w = edge[i].w;
    int preu = find(u), prev = find(v);
    if (preu != prev) {
      ans += w;
      pre[preu] = prev;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
typedef long long ll;

int n, cnt;
struct edge {
  int head, to, w;
  bool operator<(const edge &x) const { return w < x.w; }
} ed[N];
struct point {
  int x, y, num;
} p[N];
bool cmp1(point x, point y) { return x.x < y.x; }
bool cmp2(point x, point y) { return x.y < y.y; }
int fa[N];

int _find(int x) {
  if (fa[x] != x) {
    return fa[x] = _find(fa[x]);
  }
  return fa[x];
}

ll kruskal() {
  sort(ed + 1, ed + 1 + cnt);
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= cnt; i++) {
    int xx = _find(ed[i].head);
    int yy = _find(ed[i].to);
    if (xx != yy) {
      fa[xx] = yy;
      ans += ed[i].w;
    }
  }
  return ans;
}
int main() {

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].num = i;
  }
  sort(p + 1, p + 1 + n, cmp1);
  for (int i = 1; i < n; i++) {
    ed[++cnt].w = p[i + 1].x - p[i].x;
    ed[cnt].head = p[i + 1].num;
    ed[cnt].to = p[i].num;
  }
  sort(p + 1, p + 1 + n, cmp2);
  for (int i = 1; i < n; i++) {
    ed[++cnt].w = p[i + 1].y - p[i].y;
    ed[cnt].head = p[i + 1].num;
    ed[cnt].to = p[i].num;
  }
  cout << kruskal() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
typedef long long ll;

int n, cnt;
struct edge {
  int head, to, w;
  bool operator<(const edge &x) const { return w < x.w; }
} ed[N << 2];
struct point {
  int x, y, num;
} p[N];
bool cmp1(point x, point y) { return x.x < y.x; }
bool cmp2(point x, point y) { return x.y < y.y; }
int fa[N];

int _find(int x) {
  if (fa[x] != x) {
    return fa[x] = _find(fa[x]);
  }
  return fa[x];
}

ll kruskal() {
  sort(ed + 1, ed + 1 + cnt);
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= cnt; i++) {
    int xx = _find(ed[i].head);
    int yy = _find(ed[i].to);
    if (xx != yy) {
      fa[xx] = yy;
      ans += ed[i].w;
    }
  }
  return ans;
}
int main() {

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].num = i;
  }
  sort(p + 1, p + 1 + n, cmp1);
  for (int i = 1; i < n; i++) {
    ed[++cnt].w = p[i + 1].x - p[i].x;
    ed[cnt].head = p[i + 1].num;
    ed[cnt].to = p[i].num;
  }
  sort(p + 1, p + 1 + n, cmp2);
  for (int i = 1; i < n; i++) {
    ed[++cnt].w = p[i + 1].y - p[i].y;
    ed[cnt].head = p[i + 1].num;
    ed[cnt].to = p[i].num;
  }
  cout << kruskal() << endl;

  return 0;
}