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

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

int main() {
  int n;
  long k;
  cin >> n >> k;
  int a[n];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int flag[n];
  rep(i, n) flag[i] = 0;
  int count[n + 1];
  int now = 0;
  int flag1 = 0;
  int end;
  rep(i, n) {
    if (flag[now] == 1) {
      flag1 = 1;
      end = i;
      break;
    }
    flag[now] = 1;
    count[now] = i;
    now = a[now];
  }
  if (flag1) {
    int loop = end - count[now];
    if (k - 1 <= end) {
      int ans = 0;
      rep(i, k) ans = a[ans];
      cout << ans + 1 << endl;
    } else {
      int ans = count[now] + (k - count[now]) % loop;
      int answer = 0;
      rep(i, ans) answer = a[answer];
      cout << answer + 1 << endl;
    }
  } else {
    int ans = 0;
    rep(i, k % n) ans = a[ans];
    cout << ans + 1 << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

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

int main() {
  int n;
  long k;
  cin >> n >> k;
  int a[n];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int ans[n + 1];
  int flag[n];
  rep(i, n) flag[i] = -1;
  int num = 0;
  int flag1 = -1;
  rep(i, n) {
    if (flag[num] != -1) {
      flag1 = i; // ans[flag1]
      break;
    }
    flag[num] = 0;
    ans[i] = a[num];
    num = a[num];
  }
  long flag2 = INF;
  if (flag1 != -1) {
    rep(i, n) {
      if (ans[i] == ans[flag1]) {
        flag2 = i;
        break;
      }
    }
    if (k <= flag2) {
      cout << ans[k - 1] + 1 << endl;
      return 0;
    }
    long anss = flag2 + (k - 1 - flag2) % (flag1 - flag2);
    cout << ans[anss - 1] + 1 << endl;
    return 0;
  }
  cout << ans[(k - 1) % n] + 1 << 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++)
#define INF 1000000007
using namespace std;

int main() { // 1_indexed
  int n;
  long k;
  cin >> n >> k;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  int town[n + 1];
  int flag[n + 1];
  rep(i, n + 1) flag[i] = 0;
  town[0] = 1;
  int now = 1;
  int end;
  int flag1 = 0;
  for (int i = 1; i < n + 1; i++) {
    town[i] = a[now - 1];
    if (flag[now] != 0) {
      end = i;
      flag1 = 1;
      break;
    }
    flag[now] = 1;
    now = a[now - 1];
  }
  if (flag1) {
    int head;
    for (int i = 1; i < n + 1; i++) {
      if (town[i] == town[end]) {
        head = i;
        break;
      }
    }
    if (k <= end)
      cout << town[k] << endl;
    else {
      int anss = (k - head) % (end - head) + head;
      cout << town[anss] << endl;
    }
  } else {
    cout << town[k % n] << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

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

int main() { // 1_indexed
  int n;
  long k;
  cin >> n >> k;
  int a[n];
  rep(i, n) {
    cin >> a[i];
    a[i];
  }
  int town[n + 1];
  int flag[n + 1];
  rep(i, n) flag[i] = 0;
  town[0] = 1;
  int now = 1;
  int end;
  int flag1 = 0;
  for (int i = 1; i < n + 1; i++) {
    town[i] = a[now - 1];
    if (flag[now] != 0) {
      end = i;
      flag1 = 1;
      break;
    }
    flag[now] = 1;
    now = a[now - 1];
  }
  if (flag1) {
    int head;
    for (int i = 1; i < n + 1; i++) {
      if (town[i] == town[end]) {
        head = i;
        break;
      }
    }
    if (k <= end)
      cout << town[k] << endl;
    else {
      int anss = (k - head) % (end - head) + head;
      cout << town[anss] << endl;
    }
  } else {
    cout << town[k % n] << 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++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200000;
int to[D][MAX_N];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }
  rep(i, D - 1) { rep(j, n) to[i + 1][j] = to[i][to[i][j]]; }

  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200000;
int to[D][MAX_N];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }
  rep(i, D) { rep(j, n) to[i + 1][j] = to[i][to[i][j]]; }

  int v = 0;
  for (int i = D; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << 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++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

// vector<int> d[60]; // segmentation fault
int d[60][212345];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    a--;
    d[0][i] = a;
  }

  rep(i, 59) {
    // if (i==0) continue;
    rep(j, n) {
      // d[i][j] = d[i-1][d[i-1][j]];
      d[i + 1][j] = d[i][d[i][j]];
    }
  }

  int v = 0;
  for (int i = 59; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = d[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

// vector<int> d[60]; // segmentation fault
int d[60][20005];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> test(3);
  test[2] = 1;
  rep(i, n) {
    int a;
    cin >> a;
    a--;
    d[0][i] = a;
  }

  rep(i, 59) {
    // if (i==0) continue;
    rep(j, n) {
      // d[i][j] = d[i-1][d[i-1][j]];
      d[i + 1][j] = d[i][d[i][j]];
    }
  }

  int v = 0;
  for (int i = 59; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = d[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;

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

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n, x;
  cin >> n;
  long long int k;
  cin >> k;

  vector<int> v;
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }

  vector<bool> vis(n + 1, false);

  int j = 1;
  long long int temp = k;
  while (vis[j] == false) {
    temp--;
    if (temp == 0) {
      cout << v[j] << endl;
      return 0;
    }
    vis[j] = true;
    j = v[j];
  }

  int xx = 1;
  while (xx != j) {
    k--;
    xx = v[xx];
  }

  int countt = 0;
  vector<int> result;
  vector<bool> vis1(n + 1, false);

  while (vis1[j] == false) {
    result.push_back(j);
    countt++;
    vis1[j] = true;
    j = v[j];
  }

  cout << result[k % countt] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n, x;
  cin >> n;
  long long int k;
  cin >> k;

  vector<int> v;
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }

  vector<bool> vis(n + 1, false);

  int j = 1;
  int index = 0;
  while (vis[j] == false) {
    vis[j] = true;
    j = v[j];
  }

  int xx = 1;
  while (xx != j) {
    k--;
    xx = v[xx];
  }

  int countt = 0;
  vector<int> result;
  vector<bool> vis1(n + 1, false);

  while (vis1[j] == false) {
    result.push_back(j);
    countt++;
    vis1[j] = true;
    j = v[j];
  }

  cout << result[k % countt] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000000
#define PI 3.141592653589793

int main() {
  int n, ans, front = 0, loop = 0;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> rec(0);
  vector<bool> table(n, false);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  rec.push_back(a[0]);
  table[a[0]] = true;
  for (int i = 1; i < n + 1; i++) {
    if (!table[a[rec[i - 1]]]) {
      rec.push_back(a[rec[i - 1]]);
      table[a[rec[i - 1]]] = true;
    } else {
      for (int j = 0; j < rec.size(); j++) {
        if (rec[j] != a[rec[i - 1]]) {
          front++;
        } else {
          loop = rec.size() - front;
          break;
        }
      }
      break;
    }
  }

  if (k > front) {
    k = (k - front) % loop;
    if (k == 0)
      k = loop;
    k += front;
  }
  ans = rec[k - 1] + 1;

  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000000
#define PI 3.141592653589793

int main() {
  int n, tail, loop, ans;
  vector<int>::iterator it;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> rec(0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  rec.push_back(a[0]);
  tail = rec[rec.size() - 1];
  while (1) {
    rec.push_back(a[tail - 1]);
    tail = rec[rec.size() - 1];
    it = find(rec.begin(), rec.end() - 1, tail);
    if (it != rec.end() - 1) {
      loop = rec.size() - *it;
      rec.pop_back();
      k -= rec.size();
      k %= loop;
      ans = rec[rec.size() - 1 - loop + k];
      break;
    }
  }

  cout << ans;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front

// #define fisier 1

using namespace std;

typedef long long ll;

const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;

int n, nxt[200002], viz[200002];
ll k;
vector<int> vc;
int main() {

#ifdef fisier
  ifstream cin("input.in");
  ofstream cout("output.out");
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> nxt[i];
  int poz = 1;
  int stp = 0;
  while (k && stp <= 10000000)
    poz = nxt[poz], ++stp, --k;
  int init = poz;
  while (1) {
    viz[init] = 1;
    vc.pb(init);
    init = nxt[init];
    if (viz[init])
      break;
  }
  cout << vc[k % (vc.size())] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front

// #define fisier 1

using namespace std;

typedef long long ll;

const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;

int n, nxt[200002], viz[200002];
ll k;
vector<int> vc;
int main() {

#ifdef fisier
  ifstream cin("input.in");
  ofstream cout("output.out");
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> nxt[i];
  int poz = 1;
  int stp = 0;
  while (k && stp <= 1000000)
    poz = nxt[poz], ++stp, --k;
  int init = poz;
  while (1) {
    vc.pb(init);
    init = nxt[init];
    if (init == poz)
      break;
  }
  cout << vc[k % (vc.size())] << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
#include <math.h>

int main() {

  long long N, K;
  int A[200100];
  int list_place[200100];
  int list_is_visited[200100];
  for (int i = 0; i < 200100; i++) {
    list_is_visited[i] = 0;
  }
  int ans, loop, start_loop_time;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int place = 1;
  list_place[0] = 1;
  list_is_visited[1] = 1;

  for (int i = 0; i < N + 2; i++) {
    place = A[place - 1];
    list_place[i + 1] = place;

    if (list_is_visited[place] != 0) {
      loop = (i + 2) - list_is_visited[place];
      start_loop_time = list_is_visited[place];
      break;
    } else {
      list_is_visited[place] = i + 2;
    }
  }

  if (K < start_loop_time) {
    ans = list_place[K];
  } else {
    ans = list_place[(K - start_loop_time + 1) % loop + start_loop_time - 1];
  }

  cout << ans << endl;

  cin >> N;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
#include <math.h>

int main() {

  long long N, K;
  int A[200100];
  int list_place[200100];
  int list_is_visited[200100];
  for (int i = 0; i < 200100; i++) {
    list_is_visited[i] = 0;
  }
  int ans, loop, start_loop_time;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int place = 1;
  list_place[0] = 1;
  list_is_visited[1] = 1;

  for (int i = 0; i < N + 2; i++) {
    place = A[place - 1];
    list_place[i + 1] = place;

    if (list_is_visited[place] != 0) {
      loop = (i + 1) - list_is_visited[place];
      start_loop_time = list_is_visited[place];
      break;
    } else {
      list_is_visited[place] = i + 1;
    }
  }

  if (K < start_loop_time) {
    ans = list_place[K];
  } else {
    ans = list_place[(K - start_loop_time + 1) % loop + start_loop_time - 1];
  }

  cout << ans;

  cin >> N;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i], A[i]--;
  int to[80][N];
  for (int i = 0; i < N; i++)
    to[0][i] = A[i];
  for (int k = 1; k < 80; k++) {
    for (int i = 0; i < N; i++) {
      to[k][i] = to[k - 1][to[k - 1][i]];
    }
  }
  int ans = 0;
  for (int j = 63; j >= 0; j--) {
    if ((K >> j) & 1)
      ans = to[j][ans];
  }
  cout << ans + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i], A[i]--;
  map<pair<int, long long>, int> memo;
  function<int(int, long long)> rec = [&](int v, long long k) {
    if (memo.count(make_pair(v, k)))
      return memo[make_pair(v, k)];
    if (k == 1)
      return A[v];
    int u = rec(v, k / 2);
    if (k & 1)
      u = A[u];
    return memo[make_pair(v, k)] = rec(u, k / 2);
  };
  cout << rec(0, K) + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, a;
string k;
int G[300010];
bool used[300010];

bool comp(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  else
    return a > b;
}

signed main() {
  cin >> N >> k;
  a = 0;
  rep(i, 0, N) {
    int to;
    cin >> to;
    G[i] = to - 1;
  }

  vector<int> v;
  int C = 0, T = 0;
  while (1) {
    used[a] = true;
    v.push_back(a);
    a = G[a];
    if (used[a])
      break;
  }

  T = find(v.begin(), v.end(), a) - v.begin();
  C = v.size() - T;

  if (comp(k, to_string(T))) {
    int mo = 0;
    rep(i, 0, k.length()) { mo = (mo * 10 + k[i] - '0') % C; }
    mo = (mo - (T % C) + C) % C;
    cout << v[T + mo] + 1 << endl;
  } else {
    int step = atoi(k.c_str());
    cout << v[step] + 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, a;
string k;
int G[100010];
bool used[100010];

bool comp(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  else
    return a > b;
}

signed main() {
  cin >> N >> k;
  a = 0;
  rep(i, 0, N) {
    int to;
    cin >> to;
    G[i] = to - 1;
  }

  vector<int> v;
  int C = 0, T = 0;
  while (1) {
    used[a] = true;
    v.push_back(a);
    a = G[a];
    if (used[a])
      break;
  }

  T = find(v.begin(), v.end(), a) - v.begin();
  C = v.size() - T;

  if (comp(k, to_string(T))) {
    int mo = 0;
    rep(i, 0, k.length()) { mo = (mo * 10 + k[i] - '0') % C; }
    mo = (mo - (T % C) + C) % C;
    cout << v[T + mo] + 1 << endl;
  } else {
    int step = atoi(k.c_str());
    cout << v[step] + 1 << 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)++)
#define FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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;
}

int used[200010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int go = 0;
  int loop = 0;
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    int a;
    cin >> a;
    a--;
    A[i] = a;
  }

  int cnt = 0;
  int cp = 0;
  bool flag = false;
  for (; cnt < K; cp = A[cp], cnt++) {
    if (used[cp] != 0) {
      go = cnt; //-1?

      loop = cnt - used[cp];
      flag = true;
      break;
    }
    used[cp] = cnt;
  }

  if (!flag) {
    cout << cp + 1 << endl;
  } else {
    K -= go;
    K %= loop;

    for (int cnto = 0; cnto < K; cnto++, cp = A[cp]) {
    }

    cout << cp + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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;
}

int used[100010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int go = 0;
  int loop = 0;
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    int a;
    cin >> a;
    a--;
    A[i] = a;
  }

  int cnt = 0;
  int cp = 0;
  bool flag = false;
  for (; cnt < K; cp = A[cp], cnt++) {
    if (used[cp] != 0) {
      go = cnt; //-1?

      loop = cnt - used[cp];
      flag = true;
      break;
    }
    used[cp] = cnt;
  }

  if (!flag) {
    cout << cp << endl;
  } else {
    K -= go;
    K %= loop;

    for (int cnto = 0; cnto < K; cnto++, cp = A[cp]) {
    }

    cout << cp + 1 << endl;
  }

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

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

long long N, K;
vector<long long> A(2e5 + 5);
vector<long long> Acnt(2e5 + 5);

int main() {

  cin >> N >> K;

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

  long long pos = 1;
  long long rpos = 1;
  long long fmove = 0;
  long long rmove = 0;
  long long last_move = 0;

  pos = 1;
  Acnt.at(1) = 1;
  while (1) {
    pos = A.at(pos);
    Acnt.at(pos)++;
    if (Acnt.at(pos) == 3) {
      rpos = pos;
      break;
    }
  }

  for (int i = 1; i < N + 1; i++) {
    if (Acnt.at(i) == 1) {
      fmove++;
    }
  }
  for (int i = 1; i < N + 1; i++) {
    if (Acnt.at(i) > 1) {
      rmove++;
    }
  }

  //  ループに入るまでの移動回数がループより大きい場合、ここで引かないとNG
  last_move = (K - fmove) % rmove;

  if (K > fmove) {
    pos = rpos;
    for (int i = 0; i < last_move; i++) {
      pos = A.at(pos);
    }
  } else {
    pos = 1;
    for (int i = 0; i < K; i++) {
      pos = A.at(pos);
    }
  }

  cout << pos << endl;
  return 0;
}

Error: 0

Fixed submission:

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

long long N, K;
vector<long long> A(2e5);
vector<long long> Acnt(2e5);

int main() {

  cin >> N >> K;

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

  int pos; // = A.at(1);
  long long move = 0;
  long long rmove = 0;
  long long last_move = 0;
  int ans = 0;
  int ans_pos = 0;
  long long rstart = 0;
  long long fmove = 0;
  bool fflg = false;

  pos = 1;
  Acnt.at(1) = 1;
  while (1) {
    pos = A.at(pos);
    Acnt.at(pos)++;
    move++;
    /*
    if ((Acnt.at(pos) == 2) && (fflg == false)){
        fmove = move;
        fflg = true;
    }
    */
    if (Acnt.at(pos) == 3) {
      rstart = pos;
      break;
    }
  }

  for (int i = 1; i < N + 1; i++) {
    if (Acnt.at(i) == 1) {
      fmove++;
    }
  }
  for (int i = 1; i < N + 1; i++) {
    if (Acnt.at(i) > 1) {
      rmove++;
    }
  }

  //    last_move = (K - fmove)%rmove;
  last_move = (K) % rmove;

  /*
  cout << "move=" << move << endl;
  cout << "rstart=" << rstart << endl;
  cout << "fmove=" << fmove << endl;
  cout << "rmove=" << rmove << endl;
  cout << "last_move=" << last_move << endl;
  */

  //    pos = A.at(1);
  pos = 1;
  for (int i = 0; i < last_move; i++) {
    pos = A.at(pos);
    //        cout << "pos=" << pos << endl;
  }

  /*
  for (ans_pos=1; ans_pos<N+1; ans_pos++){
      if(A.at(ans_pos) == pos){
          ans = ans_pos;
      }
  }
  */

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