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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

int main() {
  int n;
  ull k;
  scanf("%d %llu", &n, &k);
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  unordered_map<int, bool> visited;
  int buffer[n];
  visited[1] = true;
  buffer[0] = 1;
  int index = 1;
  ull j = 0;
  int i = 1;
  int start = -1;
  while (j < k) {
    i = a[i - 1];
    if (visited[i]) {
      start = i;
      break;
    }
    visited[i] = true;
    buffer[index++] = i;
    j++;
  }
  if (start != -1) {
    int len = index;
    int loop_len = 0;
    int si = 0;
    for (int i = 0; i < len; i++) {
      if (buffer[i] == start) {
        loop_len = len - i;
        k = k - i;
        si = i;
        break;
      }
    }
    int dist = k % loop_len;
    printf("%d", buffer[si + dist]);
  } else
    printf("%d", buffer[k]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

int main() {
  int n;
  ull k;
  scanf("%d %llu", &n, &k);
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  unordered_map<int, bool> visited;
  vector<int> buffer;
  visited[1] = true;
  buffer.push_back(1);
  ull j = 0;
  int i = 1;
  int start = -1;
  while (j < k) {
    i = a[i - 1];
    if (visited[i]) {
      start = i;
      break;
    }
    visited[i] = true;
    buffer.push_back(i);
    j++;
  }
  if (start != -1) {
    int len = buffer.size();
    int loop_len = 0;
    for (int i = 0; i < len; i++) {
      if (buffer[i] == start) {
        loop_len = len - i;
        k = k - i;
        break;
      }
    }
    int dist = k % loop_len;
    printf("%d", buffer[start + dist]);
  } else
    printf("%d", buffer[k - 1]);
  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 FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;

void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << ((sizeof...(tail) != 0) ? " " : "");
  print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a << ((&a != &vec.back()) ? " " : "");
  }
  cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

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

  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    a[i] = x;
  }
  map<int, int> mp;
  vector<int> t;
  int pos = 0;
  bool found = false;
  rep(i, min(n, k)) {
    if (mp.count(pos)) {
      found = true;
      break;
    } else {
      mp[pos] = i;
      t.push_back(pos);
      int next = a[pos];
      pos = next;
      if (i == k - 1) {
        cout << pos + 1 << endl;
        return 0;
      }
    }
  }
  if (found) {
    int start = mp[pos];
    int loop = t.size() - start;
    int cnt = (k - start) % loop;
    // print(start, k-start, loop, cnt);
    // print(t);
    cout << t[start + cnt] + 1 << endl;
  } else {
    int cnt = k % n;
    cout << t[cnt] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;

void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << ((sizeof...(tail) != 0) ? " " : "");
  print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a << ((&a != &vec.back()) ? " " : "");
  }
  cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

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

  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    a[i] = x;
  }
  map<int, int> mp;
  vector<int> t;
  int pos = 0;
  bool found = false;
  rep(i, min(n, k)) {
    if (mp.count(pos)) {
      found = true;
      break;
    } else
      mp[pos] = i;
    t.push_back(pos);
    int next = a[pos];
    pos = next;
    if (i == k - 1) {
      cout << pos << endl;
      return 0;
    }
  }
  if (found) {
    int start = mp[pos];
    int end = t.size() - mp[pos];
    int loop = end - start;
    int cnt = k % loop;
    cout << t[start + cnt] + 1 << endl;
  } else {
    int cnt = k % n;
    cout << t[cnt] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
#define db(x) cout << "> " << #x << ": " << x << "\\n";
#define ss cout << " ";
#define ct(x) cout << x;
#define nn cout << "\\n";
#define cts(x) cout << x << " ";
#define ctn(x) cout << x << "\\n";
// // // // // // // // // // // // // // // // // // //
int64 n, k;
vec arr(2e5 + 10);

int64 op() {
  map<int64, int> mp;
  vec nw;
  int64 last = 1;
  mp[1] = 1;
  while (true) {
    int64 now = arr[last];
    if (mp[now] < 2) {
      mp[now]++;
    } else {
      last = now;
      break;
    }
    last = now;
  }
  map<int64, bool> mp2;
  while (true) {
    if (!mp2[last]) {
      mp2[last] = 1;
      nw.push_back(last);
    } else {
      break;
    }
    last = arr[last];
  }
  last = 1;
  while (true) {
    if (k == 0) {
      return (last);
    }
    if (!mp2[last]) {
      mp2[last] = 1;
      k--;
    } else {
      break;
    }
    last = arr[last];
  }
  // for (auto i: nw){
  //     cts(i);
  // }
  // nn;
  // db(k);
  return (nw[k % nw.size()]);
}
int brute() {
  int cnt = 0;
  int last = 1;
  while (cnt != k) {
    cnt++;
    last = arr[last];
  }
  return (last);
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  ctn(op());
  // int a = op();
  // int b = brute();
  // cts(a);
  // cts(b);
  // ctn(abs(a - b));
  // return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
#define db(x) cout << "> " << #x << ": " << x << "\\n";
#define ss cout << " ";
#define ct(x) cout << x;
#define nn cout << "\\n";
#define cts(x) cout << x << " ";
#define ctn(x) cout << x << "\\n";
// // // // // // // // // // // // // // // // // // //
int64 q, n;

void solve() { return; }

int main() {
  int64 n, k;
  cin >> n >> k;
  vec arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  map<int, bool> mp;
  vec nw;
  int last = 1;
  mp[1] = 1;
  while (true) {
    int now = arr[last];
    if (!mp[now]) {
      mp[now] = 1;
      nw.push_back(now);
    } else {
      last = now;
      break;
    }
    last = now;
  }
  int newlast = 1;
  while (true) {
    if (k == 0) {
      ctn(newlast);
      return 0;
    }
    if (newlast == last) {
      break;
    } else {
      k--;
      newlast = arr[newlast];
    }
  }
  if (k <= 0) {
    ctn(newlast);
    return 0;
  }
  ctn(nw[k % nw.size()]);
  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;
  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i], --A[i];

  deque<int> a;                // 繰り返しの部分
  vector<bool> seen(N, false); // 一度見たかどうか
  int cur = 0;
  while (true) {
    // 一度通った頂点を見つけたときの処理
    if (seen[cur]) {
      while (a[0] != cur) {
        // 最初の余計な数手分を除去する
        --K;
        a.pop_front();

        // 繰り返す前に K が限界になったらリターン
        if (K == 0) {
          cout << a[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    // 最初は愚直にシミュレーションしつつ、履歴をメモしていく
    a.push_back(cur);
    seen[cur] = true;
    cur = A[cur];
  }
  cout << a[K % a.size()] + 1 << endl;
}

Error: 0

Fixed submission:

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

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<bool> judge(n, false);
  deque<ll> path;
  for (ll i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  ll x = 1, kd = k, y = 0, node;
  while (k > 0 && judge[x] == false) {
    judge[x] = true;
    path.push_back(x);
    x = a[x];
    k--;
    // cout << x << endl;
  }
  if (k != 0 && judge[x] == true) {
    node = x;
    x = 1;
    while (x != node) {
      path.pop_front();
      x = a[x];
      y++;
    }
    x = path[((kd - y) % (path.size()))];
    // cout << "kd:" << kd << " y:" << y << " path.size:" << path.size() <<
    // endl; cout << path[0] << endl;
  }

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

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

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  vector<long> city(n, 0);

  long tmp = 0;
  long once;
  long l;

  for (long i = 1; i <= k; i++) {
    tmp = a[tmp];

    if (city[tmp] == 0)
      city[tmp] = i;
    else {
      once = city[tmp];
      l = i - once;
      break;
    }
  }
  if (l != 0) {

    long des = (k - once) % l;

    for (long i = 1; i <= des; i++) {
      tmp = a[tmp];
    }
  }

  cout << tmp + 1 << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  vector<long> city(n, 0);

  long tmp = 0;
  long once;
  long l;

  for (long i = 1; i <= k; i++) {
    tmp = a[tmp];

    if (city[tmp] == 0)
      city[tmp] = i;
    else {
      once = city[tmp];
      l = i - once;
      break;
    }
  }

  long des = (k - once) % l;

  for (long i = 1; i <= des; i++) {
    tmp = a[tmp];
  }

  cout << tmp + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> a(n);
  REP(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> num(n + 1, -1);
  ll cur = 0, loop = 0, end = 0;
  num[cur] = 0;
  // cout<<"cur "<<cur<<endl;
  FOR(i, 1, k + 1) {
    cur = a[cur];
    // cout<<i<<"cur "<<cur<<endl;
    if (num[cur] != -1) {
      loop = (i - num[cur]);
      end = k - i;
      break;
    }
    num[cur] = i;
  }
  if (loop == 0) {
    cout << cur + 1 << endl;
    return 0;
  }
  // cout<<loop<<endl;
  end %= loop;
  REP(i, end) { cur = a[cur]; }
  cout << cur + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> a(n);
  REP(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> num(n, -1);
  ll cur = 0, loop = 0, end = 0;
  num[cur] = 0;
  FOR(i, 1, k + 1) {
    cur = a[cur];
    if (num[cur] != -1) {
      loop = (i - num[cur]);
      end = k - i;
      break;
    }
    num[cur] = i;
  }
  // cout<<loop<<endl;
  end %= loop;
  REP(i, end) { cur = a[cur]; }
  cout << cur + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  int a[200000];
  for (int i(0); i < n; i++) {
    cin >> a[i];
  }
  long long one_lap(-1);
  int next_des(1);
  long long time(0);
  bool passed[200000] = {0};
  int pos(-1);
  for (long long i(0); i < k; i++) {
    passed[next_des - 1] = true;
    next_des = a[next_des - 1];
    time++;
    if (passed[next_des - 1]) {
      pos = next_des;
      break;
    }
  }
  int tmp_des(1);
  int first_time(0);
  if (pos != 1) {
    for (int i(0); i < time; i++) {
      tmp_des = a[tmp_des - 1];
      if (pos == tmp_des) {
        first_time = ++i;
        break;
      }
    }
  }
  one_lap = time - first_time;
  long long remain = (long long)k - first_time;
  remain %= one_lap;
  for (long long i(0); i < remain; i++) {
    next_des = a[next_des - 1];
  }
  cout << next_des << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  int a[200000];
  for (int i(0); i < n; i++) {
    cin >> a[i];
  }
  long long one_lap(-1);
  int next_des(1);
  long long time(0);
  bool passed[200000] = {0};
  int pos;
  for (long long i(0); i < k; i++) {
    passed[next_des - 1] = true;
    next_des = a[next_des - 1];
    time++;
    if (passed[next_des - 1]) {
      pos = next_des;
      break;
    }
  }
  next_des = 1;
  int first_time(0);
  if (pos != 1) {
    for (int i(0); i < time; i++) {
      next_des = a[next_des - 1];
      if (pos == next_des) {
        first_time = ++i;
        break;
      }
    }
  }
  one_lap = time - first_time;
  long long remain = (long long)k - first_time;
  remain %= one_lap;
  next_des = pos;
  for (long long i(0); i < remain; i++) {
    next_des = a[next_des - 1];
  }
  cout << next_des << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*
   Created by Ezio112
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define inf ll(10e16)
#define mod ll(10e8 + 7)
#define ff first
#define ss second
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> ar(n + 1);
    for (ll i = 1; i <= n; ++i)
      cin >> ar[i];

    unordered_map<ll, ll> appeared;

    ll start = 1, jumps = 1, loop_size = 1;

    while (k--) {
      appeared[start] = jumps++;
      start = ar[start];
      if (appeared.count(start)) {
        loop_size = jumps - appeared[start];
        break;
      }
    }
    k %= loop_size;
    while (k--) {
      start = ar[start];
    }
    cout << start;
  }
  return 0;
}


Error: WA

Fixed submission:

/*
   Created by Ezio112
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define inf ll(10e16)
#define mod ll(10e8 + 7)
#define ff first
#define ss second
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> ar(n + 1);
    for (ll i = 1; i <= n; ++i)
      cin >> ar[i];

    unordered_map<ll, ll> appeared;

    ll start = 1, jumps = 1, loop_size = 0;

    while (k--) {
      appeared[start] = jumps++;
      start = ar[start];
      if (appeared.count(start)) {
        loop_size = jumps - appeared[start];
        break;
      }
    }
    k %= loop_size;
    while (k--) {
      start = ar[start];
    }
    cout << start;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const int N = 500500;
long long k;
int n, A[N], B[N], pos[N], vis[N], len;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  B[0] = 1;
  /*
  for (int i = 1; i <= 2 * n; i++){
      B[i] = A[B[i - 1]];
  }
  int start = 1,len = 0;
  for (int i = 1; i <= n; i++){
      bool flag = false;
      for (int j = 1; j <= n - i; j++){
          bool check = true;
          for (int l = i,r = j + i; l <= i + j - 1 && r <= i + 2 * j - 1;
  l++,r++){ if (B[l] != B[r]) check = false;
          }
          if (check){
              flag = true;
              len = j;
              break;
          }
      }
      if (flag){
          start = i;
          break;
      }
  }
  */
  for (int i = 1; i <= n; i++) {
    B[i] = A[B[i - 1]];
  }
  int len = 0, start = 0;
  for (int i = 1; i <= n; i++) {
    pos[B[i]]++;
    if (pos[B[i]] > 1) {
      len = i - vis[B[i]];
      start = vis[B[i]];
      break;
    }
    vis[B[i]] = i;
  }
  long long w = k - start + 1;
  // << vis[3];
  if (k < start)
    cout << B[k];
  else if (w % len == 0)
    cout << B[len + start - 1];
  // for (int i = 1; i <= n; i++) cout << B[i];
  else
    cout << B[w % len + start - 1];
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int N = 500500;
long long k;
int n, A[N], B[N], len;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  B[0] = 1;
  for (int i = 1; i <= 2 * n; i++) {
    B[i] = A[B[i - 1]];
  }
  int start = 1, len = 0;
  for (int i = 1; i <= n; i++) {
    bool flag = false;
    for (int j = 1; j <= n - i; j++) {
      bool check = true;
      for (int l = i, r = j + i; l <= i + j - 1 && r <= i + 2 * j - 1;
           l++, r++) {
        if (B[l] != B[r])
          check = false;
      }
      if (check) {
        flag = true;
        len = j;
        break;
      }
    }
    if (flag) {
      start = i;
      break;
    }
  }
  long long w = k - start + 1;
  if (w % len == 0)
    cout << B[len + start - 1];
  else
    cout << B[w % len + start - 1];
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, p, Circle, Cnt = 2, a[Maxn], vis[Maxn];
long long k;

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  if (k <= n) {
    int Res = 1;
    for (; k > 0; k--)
      Res = a[Res];
    printf("%d\\n", Res);
    return 0;
  }
  vis[a[1]] = 1;
  for (p = a[1]; !vis[a[p]]; p = a[p], Cnt++)
    vis[a[p]] = Cnt;
  // printf("%d %d\\n", vis[a[p]], Cnt);
  // for (int i=1; i<=n; i++) printf("%d ", vis[a[i]]); puts("");
  Circle = Cnt - vis[a[p]];
  k -= (vis[a[p]] - 1ll);
  k %= Circle;
  if (!k)
    k = Circle;
  int Res = a[p];
  k--;
  // printf("k = %lld\\n", k);
  for (; k > 0; k--)
    Res = a[Res];
  printf("%d\\n", Res);
}

Error: TLE

Fixed submission:

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

int n, p, Circle, Cnt = 2, a[Maxn], vis[Maxn];
long long k;

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  vis[a[1]] = 1;
  for (p = a[1]; !vis[a[p]]; p = a[p], Cnt++)
    vis[a[p]] = Cnt;
  // printf("%d %d\\n", vis[a[p]], Cnt);
  // for (int i=1; i<=n; i++) printf("%d ", vis[a[i]]); puts("");
  Circle = Cnt - vis[a[p]];
  k -= (vis[a[p]] - 1ll);
  k %= Circle;
  int Res = a[p];
  k--;
  // printf("k = %lld\\n", k);
  for (; k; k--)
    Res = a[Res];
  printf("%d\\n", Res);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
const int INF = 1001001001;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  ll n, k, pos = 1, moves;
  cin >> n >> k;
  vector<ll> v(n + 1), last(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  while (k--) {
    pos = v[pos];
    if (last[pos])
      k %= moves - last[pos];
    last[pos] = moves++;
  }
  cout << pos << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
const int INF = 1001001001;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  if (k <= n) {
    // simulate k moves
    int pos = 1;
    while (k--) {
      pos = v[pos];
    }
    cout << pos << endl;
  } else {
    // simulate n moves
    int pos = v[1];
    vector<int> last(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      last[pos] = i;
      pos = v[pos];
      // cout << i << " " << pos << endl;
    }
    // cout << pos << endl;
    int lim = (k - n) % (n - last[pos]);
    // cout << lim << endl;
    for (int i = 0; i < lim; i++) {
      pos = v[pos];
    }
    cout << pos << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long int lli;
const int MaxN = 200060;
#define fst first
#define snd second
#define mp make_pair
lli mcd(lli a, lli b) {
  if (a == 0)
    return b;
  return mcd(b % a, a);
}
lli teleport[MaxN];
lli N, K, res;
bool vis[MaxN];
lli dfs() {
  lli i, j, p, q, v, e, tam, llevo = 0;
  e = 1;
  vector<lli> ciclo;
  while (true) {
    if (vis[e] != 0) {
      v = e;
      break;
    }
    if (llevo == K) {
      return e;
    }
    llevo += 1;
    vis[e] = 1;
    e = teleport[e];
  }
  // cout<<K<<"\\n";
  // llevo-=1;
  K -= llevo;
  // cout<<llevo<<" "<<K<<"\\n";
  llevo = 0;
  for (i = 0; i <= N + 5; i++)
    vis[i] = 0;
  while (true) {
    if (vis[v] == 1)
      break;
    // cout<<v<<" "<<vis[v]<<"\\n";
    vis[v] = 1;
    ciclo.push_back(v);
    v = teleport[v];
    llevo += 1;
  }
  tam = ciclo.size();
  // cout<<llevo<<" "<<tam<<" "<<K<<" ";;
  K = K % tam;
  K += tam;
  K = K % tam;
  return ciclo[(K) % tam];
}
int main() {
  // cin.tie(0),ios_base::sync_with_stdio(0);
  lli i, j, p;
  cin >> N >> K;
  for (i = 1; i <= N; i++) {
    cin >> teleport[i];
  }
  cout << dfs();
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long int lli;
const int MaxN = 200000;
#define fst first
#define snd second
#define mp make_pair
lli mcd(lli a, lli b) {
  if (a == 0)
    return b;
  return mcd(b % a, a);
}
lli teleport[MaxN];
lli N, K, res;
bool vis[MaxN];
lli dfs() {
  lli i, j, p, q, v, e, tam, llevo = 1;
  e = 1;
  vector<lli> ciclo;
  while (true) {
    if (vis[e] != 0) {
      v = e;
      break;
    }
    if (llevo == K) {
      return e;
    }
    llevo += 1;
    vis[e] = 1;
    e = teleport[e];
  }
  K -= llevo;
  for (i = 0; i <= N + 5; i++)
    vis[i] = 0;
  while (true) {
    if (vis[v] == 1)
      break;
    // cout<<v<<" "<<vis[v]<<"\\n";
    vis[v] = 1;
    ciclo.push_back(v);
    v = teleport[v];
    llevo += 1;
  }
  tam = ciclo.size();
  // cout<<tam<<" ";
  K = K % tam;
  return ciclo[(K + 1) % tam];
}
int main() {
  // cin.tie(0),ios_base::sync_with_stdio(0);
  lli i, j, p;
  cin >> N >> K;
  for (i = 1; i <= N; i++) {
    cin >> teleport[i];
  }
  cout << dfs();
}