alexjercan/bugnet huggingface dataset:
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
bool check(vector<int> u, int x) {
  for (int i = 0; i < u.size(); i++) {
    if (u[i] < x)
      return false;
  }
  return true;
}

int main() {
  int n, flag = 0, y, m, x, ans = INT_MAX;
  cin >> n >> m >> x;
  vector<int> c(n);
  vector<vector<int>> a(n, vector<int>(m));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j == 0)
        cin >> c[i];
      else
        cin >> a[i][j - 1];
    }
  }

  int tot = 1 << n;
  int cost;

  for (int mask = 0; mask < tot; mask++) {
    vector<int> u(m, 0);
    cost = 0;
    for (int i = 0; i < n; i++) {
      if ((mask & (1 << i)) != 0) {
        cost += c[i];
        for (int j = 0; j < m; j++)
          u[j] += a[i][j];
      }
    }
    if (check(u, x)) {
      flag = 1;
      ans = min(ans, cost);
    }
  }
  if (flag == 0)
    cout << -1;
  else
    cout << ans;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
  int pri;
  int a[15];
} st[15];
int ans = 1e8;
int b[15];
int d[15];
int n, m, x;
int len;
int dfs(int p) {
  if (p == len + 1) {
    int sum = 0;
    memset(d, 0, sizeof(d));
    for (int i = 1; i <= len; i++) {
      sum += st[b[i]].pri;
      for (int j = 1; j <= m; j++) {
        d[j] += st[b[i]].a[j];
      }
    }
    for (int i = 1; i <= m; i++) {
      if (d[i] < x)
        return 0;
    }
    ans = min(ans, sum);
    return 0;
  }
  for (int i = b[p - 1] + 1; i <= n; i++) {
    b[p] = i;
    dfs(p + 1);
  }
}
int main() {
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++) {
    cin >> st[i].pri;
    for (int j = 1; j <= m; j++) {
      cin >> st[i].a[j];
    }
  }
  for (len = 1; len <= n; len++) {
    b[0] = 0;
    dfs(1);
  }
  if (ans == 1e8)
    cout << -1 << "\\n";
  else
    cout << ans << "\\n";
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[50][50], c[50];
int v[20];
int ans[maxn];
int ss = inf;
int n, m, x;
int sum = 0;
il void update(int x, int op) {
  if (op)
    for (int i = 1; i <= m; i++)
      ans[i] += a[x][i];
  else
    for (int i = 1; i <= m; i++)
      ans[i] -= a[x][i];
}
void dfs(int p) {
  update(p, 1);
  sum += c[p];
  int flag = 1;
  for (int i = 1; i <= m; i++)
    if (ans[i] < x)
      flag = 0;
  if (flag) {
    ss = min(sum, ss);
    return;
  }
  for (int i = p + 1; i <= n; i++) {
    dfs(i);
    sum -= c[i];
    update(i, 0);
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j], ans[j] += a[i][j];
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] < x) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i <= m; i++)
    ans[i] = 0;
  for (int i = 1; i <= n; i++) {
    dfs(i);
    sum -= c[i];
    update(i, 0);
  }
  cout << ss;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[50][50], c[50];
int v[20];
int ans[maxn];
int ss = inf;
int n, m, x;
il void update(int x, int op) {
  if (op)
    for (int i = 1; i <= m; i++)
      ans[i] += a[x][i];
  else
    for (int i = 1; i <= m; i++)
      ans[i] -= a[x][i];
}
void dfs(int sum, int p) {
  int flag = 1;
  for (int i = 1; i <= m; i++)
    if (ans[i] < x)
      flag = 0;
  if (flag) {
    ss = min(sum, ss);
    return;
  }
  if (sum > v[p])
    return;
  for (int i = 1; i <= n; i++)
    if (!v[i]) {
      v[i] = sum + c[i];
      update(i, 1);
      dfs(sum + c[i], i);
      update(i, 0);
      v[i] = 0;
    }
}
signed main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }
  dfs(0, 0);
  if (ss == inf)
    cout << -1;
  else
    cout << ss;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define INF (1LL << 60)
#define vec vector

int main(void) {
  int n;
  ll k;
  cin >> n >> k;
  int a[n];
  vector<int> b(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  ll now = 0;
  ll ans = -1;
  vec<int> memo;
  for (ll i = 0;; i++) {
    if (k == 0) {
      ans = now + 1;
      break;
    }

    b[now] = 1;
    now = a[now];
    k--;

    if (b[now] > 0) {
      break;
    }
  }

  if (ans == -1) {
    ll memo2 = now;
    while (1) {
      memo.push_back(now);
      now = a[now];
      if (now == memo2)
        break;
    }
    ans = memo[k % memo.size()] + 1;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define INF (1LL << 60)
#define vec vector

int main(void) {
  int n;
  ll k;
  cin >> n >> k;
  int a[n];
  vector<int> b(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  ll now = 0;
  ll ans = -1;
  vec<int> memo;
  for (ll i = 0;; i++) {
    if (b[now] > 0) {
      break;
    }
    if (k == 0) {
      ans = now + 1;
      break;
    }
    b[now]++;
    now = a[now];
    k--;
  }

  int memo2 = now;
  while (1) {
    memo.push_back(now);
    now = a[now];
    if (now == memo2)
      break;
  }

  if (ans == -1)
    ans = memo[k % memo.size()] + 1;
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

vector<int> A, v;
int cnt = 0;
int x = 0;
int dfs(int k = 0) {
  if (v[k] > -1)
    return x = v[k];
  v[k] = cnt;
  cnt++;
  dfs(A[k] - 1);
  return x;
}

int main() {
  init();

  int N;
  cin >> N;
  ll K;
  cin >> K;
  A.resize(N);
  rep(i, 0, N) { cin >> A[i]; }
  v.resize(N, -1);
  int x = dfs();
  if (x == 0) {
    K %= cnt;
    rep(i, 0, N) {
      if (v[i] == K)
        cout << i + 1 << "\\n";
    }
  } else {
    if (K < cnt) {
      rep(i, 0, N) {
        if (v[i] == K)
          cout << i + 1 << "\\n";
      }
    } else {
      K -= (ll)cnt;
      int a = cnt - x;
      K %= a;
      rep(i, 0, N) {
        if ((v[i] - x) == K)
          cout << i + 1 << "\\n";
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

vector<int> A, v;
int cnt = 0;
int dfs(int k = 0) {
  if (v[k] != -1)
    return v[k];
  v[k] = cnt;
  cnt++;
  dfs(A[k] - 1);
}

int main() {
  init();

  int N;
  cin >> N;
  ll K;
  cin >> K;
  A.resize(N);
  rep(i, 0, N) { cin >> A[i]; }
  v.resize(N, -1);
  int x = dfs();
  if (x == 0) {
    K %= cnt;
    rep(i, 0, N) {
      if (v[i] == K)
        cout << i + 1 << "\\n";
    }
  } else {
    if (K < cnt) {
      rep(i, 0, N) {
        if (v[i] == K)
          cout << i + 1 << "\\n";
      }
    } else {
      K -= (ll)cnt;
      int a = cnt - x;
      K %= a;
      rep(i, 0, N) {
        if ((v[i] - x) == K)
          cout << i + 1 << "\\n";
      }
    }
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    c = s.size() - ord[v];
    l = ord[v];
  }

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> val(n + 1, -1);
  vector<int> s;
  int c = 1, l = 0;
  int v = 1;
  while (val[v] == -1) {
    val[v] = s.size();
    s.push_back(v);
    v = a[v];
  }
  c = s.size() - val[v];
  l = val[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> val(n + 1, -1);
  vector<int> s;
  int c = 1, l = 0;
  int v = 1;
  while (val[v] == -1) {
    val[v] = s.size();
    s.push_back(v);
    v = a[v - 1];
  }
  c = s.size() - val[v];
  l = val[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;

using namespace std;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> val(n + 1, -1);
  vector<int> s;
  int c = 1, l = 0;
  int v = 1;
  while (val[v] == -1) {
    val[v] = s.size();
    s.push_back(v);
    v = a[v];
  }
  c = s.size() - val[v];
  l = val[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    p--;
    a[i] = p;
  }
  ll now = 0;
  vector<ll> route;
  map<ll, ll> visited;
  route.push_back(now);
  visited[now]++;
  ll distone;
  ll loop;
  while (true) {
    ll next = a[now];
    now = next;
    route.push_back(now);
    visited[now]++;
    if (visited[now] == 2) {
      auto itr = find(route.begin(), route.end(), now);
      auto itr2 = find(itr + 1, route.end(), now);
      distone = distance(route.begin(), itr);
      loop = distance(itr, itr2);
      break;
    }
  }
  ll t = (long long)route.size();
  if (k < t)
    cout << route[k] + 1 << endl;
  else
    cout << route[distone + (k - distone) % loop] + 1 << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    p--;
    a[i] = p;
  }
  ll now = 0;
  vector<ll> route;
  map<ll, ll> visited;
  route.push_back(now);
  visited[now]++;
  ll distone;
  ll loop;
  while (true) {
    ll next = a[now];
    now = next;
    route.push_back(now);
    visited[now]++;
    if (visited[now] == 2) {
      auto itr = find(route.begin(), route.end(), now);
      auto itr2 = find(itr + 1, route.end(), now);
      distone = distance(route.begin(), itr);
      loop = distance(itr, itr2);
      loop--;
      break;
    }
  }
  ll t = route.size();
  if (k < t)
    cout << route[k] << endl;
  else
    cout << route[distone + 1 + (k - distone) % loop] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

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

  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    --a[i];
  }

  vector<ll> journey;
  int prev = 0;
  rep(i, 2 * n + 1000) {
    int curr = a[prev];
    if (i == k - 1) {
      cout << curr + 1 << endl;
      return 0;
    }
    journey.push_back(curr);
    prev = curr;
  }
  // rep(i, 20) cout << journey[i] << " ";
  // cout << endl;

  ll loop_end = journey[n];
  ll loop_start = n - 1;
  while (journey[loop_start] != loop_end) {
    --loop_start;
  }

  ll period = n - loop_start;

  k %= period;

  while (k < n)
    k += period;

  cout << a[journey[k - 2]] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

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

  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    --a[i];
  }

  vector<ll> journey;
  int prev = 0;
  rep(i, n + 100) {
    int curr = a[prev];
    if (i == k - 1) {
      cout << curr + 1 << endl;
      return 0;
    }
    journey.push_back(curr);
    prev = curr;
  }
  rep(i, 20) cout << journey[i] << " ";
  cout << endl;

  ll loop_end = journey[n];
  ll loop_start = n - 1;
  while (journey[loop_start] != loop_end) {
    --loop_start;
  }

  ll period = n - loop_start;

  k %= period;

  while (k < n)
    k += period;

  cout << journey[k + 1] + 1 << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <vector>
using namespace std;
#define MAX_N 200000

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
long long N, K;
long long A[MAX_N];
int A_hst[MAX_N];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A_hst[i] = 0;
    // cout << i << "," << A[i] << endl;
  }

  int loc = 0;
  int ct = 1;

  int next = A[loc] - 1;
  while (ct <= K && A_hst[next] == 0) {
    A_hst[next] = ct;
    // cout << loc << "->" << next << " (" << A_hst[next] << ")" << endl;
    loc = next; // move
    next = A[loc] - 1;

    ct++;
  }
  //   cout << "Final: " << loc << "->" << next << " (" << A_hst[next] << ")" <<
  //   endl;
  int T;
  T = ct - A_hst[next];
  //   cout << loc << "," << T << " (" << A_hst[next] << ")" << endl;

  int ans_hosei = A_hst[next];
  int amari = (K - ans_hosei) % T;
  int ans;
  if (K < ans_hosei + T) {
    ans = K;
  } else {
    ans = amari + ans_hosei;
  }

  //   printf("ct: %d, amari: %d, ans_hosei: %d, ans: %d\\n", ct, amari,
  //   ans_hosei, ans);
  int ans_loc = -1;
  for (int i = 0; i < N; i++) {
    // cout << i << "->" << A[i]-1 << " (" << A_hst[A[i]-1] << ")" << endl;
    // cout << i << " (" << A_hst[i] << ")" << endl;
    if (A_hst[i] == ans) {
      ans_loc = i;
    }
  }

  //   cout << ans_loc << "," << A[ans_loc] << "," << A_hst[ans_loc] << endl;
  cout << ans_loc + 1 << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define MAX_N 200000

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
long long N, K;
long long A[MAX_N];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    // cout << i << "," << A[i] << endl;
  }

  int i = 0;
  int T = 0;
  int next = A[i];
  while (next != 0) {
    next = A[i];
    T = i;
    // cout << i << "->" << next << endl;
    i = next;
  }

  // cout << T << endl;

  cout << A[K % T]++ << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define drep(i, cc, n) for (int i = cc; i > n; --i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll choose2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> visited(n, -1);
  rep(i, 0, n) {
    cin >> a[i];
    a[i]--;
  }
  ll num = 0;
  ll now = 0;
  while (visited[now] == -1 && num < k) {
    visited[now] = num;
    now = a[now];
    num++;
  }
  k -= visited[now];
  int loop = num - visited[now];
  rep(i, 0, k % loop) now = a[now];
  cout << now + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
#define drep(i, cc, n) for (int i = cc; i >= n; --i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll choose2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> hoge(n, 0);
  vector<ll> a(n);
  int cnt = 0;
  rep(i, 0, n - 1) cin >> a[i];

  hoge[0] = 1;

  int loop_cnt = 0;
  int pre_cnt = 0;

  while (1) {
    cnt++;
    if (hoge[a[cnt - 1] - 1] != 0) {
      loop_cnt = cnt - hoge[a[cnt - 1] - 1];
      pre_cnt = cnt - 1 - loop_cnt;
      // cout<<hoge[a[cnt-1] - 1]<<endl;
      /*rep(i,0,n-1){
          cout<<i<<" "<<hoge[i]<<endl;
      }*/
      // cout<<pre_cnt<<endl;
      break;
    }
    hoge[a[cnt - 1] - 1] = cnt + 1;
  }

  int ans = 1;

  rep(i, 0, pre_cnt + (k % (ll)loop_cnt) - 1) { ans = a[ans - 1]; }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (k <= n) {
    long long p = 1;

    for (int i = 0; i < k; i++) {
      p = a[p - 1];
    }

    cout << p << endl;
    return 0;
  }
  vector<long long> v;
  long long count[500006] = {0};

  long long t = 1;
  long long target;

  while (1) {
    if (count[t] == 1) {
      target = t;
      break;
    }

    else {
      count[t]++;
      v.push_back(t);
      t = a[t - 1];
    }
  }

  bool check = true;
  long long curr = 0;
  vector<long long> ans;

  for (int i = 0; i < v.size(); i++) {
    if (v[i] != target && check)
      curr++;

    else {
      check = false;
      ans.push_back(v[i]);
    }
  }

  k = k - curr;
  k = k % ans.size();
  cout << ans[k] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> v;
  long long count[100006] = {0};

  long long t = 1;
  long long target;

  while (1) {
    if (count[t] == 1) {
      target = t;
      break;
    }

    else {
      count[t]++;
      v.push_back(t);
      t = a[t - 1];
    }
  }

  bool check = true;
  long long curr = 0;
  vector<long long> ans;

  for (int i = 0; i < v.size(); i++) {
    if (v[i] != target && check)
      curr++;

    else {
      check = false;
      ans.push_back(v[i]);
    }
  }

  k = k - curr;
  k = k % ans.size();
  cout << ans[k] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;
using namespace std;
const int MAX = 200010;

int main() {
  int N;
  int A[MAX], B[MAX], P[MAX];
  ll K, Ls, Le, ae;
  cin >> N >> K;
  REP(int, i, 1, N + 1) cin >> A[i];
  REP(int, i, 1, N + 1) B[i] = -1;

  B[1] = 1;
  P[1] = 1;
  REP(int, i, 1, N + 1) {
    ae = A[P[i]];
    if (B[ae] == -1) {
      // cout << ae << endl;
      B[ae] = i + 1;
      P[i + 1] = ae;
    } else {
      Le = i;
      break;
    }
  }
  Ls = B[ae];

  if (K + 1 <= Ls) {
    cout << P[K + 1] << endl;
  } else {
    ae = (K + 1 - Ls) % (Le - Ls + 1);
    cout << P[ae + Ls] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;
using namespace std;
const int MAX = 200010;

int main() {
  int N;
  int A[MAX], B[MAX], P[MAX];
  ll K, Ls, Le, ae;
  cin >> N >> K;
  REP(int, i, 1, N + 1) cin >> A[i];
  REP(int, i, 1, N + 1) B[i] = 1;

  B[1] = 0;
  P[1] = 1;
  REP(int, i, 1, N + 1) {
    ae = A[P[i]];
    if (B[ae] == 1) {
      // cout << ae << endl;
      B[ae] = 0;
      P[i + 1] = ae;
    } else {
      Le = i;
      break;
    }
  }
  REP(int, i, 1, N + 1) {
    if (P[i] == ae)
      Ls = i;
  }

  if (K + 1 <= Ls) {
    cout << P[K + 1] << endl;
  } else {
    ae = (K + 1 - Ls) % (Le - Ls + 1) + Ls;
    cout << P[ae] << endl;
  }
}