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

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> zone(n);
  rep(i, 0, n) {
    cin >> zone[i];
    zone[i]--;
  }
  vector<int> m(n);
  vector<ll> m2;
  vector<int> m3;
  int a = 0;
  bool j = true;
  int count = 0, count2 = 0;
  rep(i, 0, 2 * n) {
    m[a]++;
    count++;
    if (j)
      m3.push_back(a + 1);
    if (m[a] == 2) {
      count2++;
      m2.push_back(a + 1);
      j = false;
    }
    if (m[a] == 3) {
      count--;
      m[a]--;
      goto end;
    }
    a = zone[a];
  }
end:
  int count3 = count - 2 * count2;
  if (k >= count3) {
    ll count4 = (k - count3) % count2;
    cout << m2[count4] << endl;
  } else {
    cout << m3[k] << endl;
  }
  // cout << count << " " << count2 << " " << count3 << " " << count4 << " " <<
  // m2[0] << m2[1] << m2[2] <<endl;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> zone(n);
  rep(i, 0, n) {
    cin >> zone[i];
    zone[i]--;
  }
  vector<int> m(n);
  vector<ll> m2;
  int a = 0;
  int count = 0, count2 = 0;
  rep(i, 0, 2 * n) {
    m[a]++;
    count++;
    if (m[a] == 2) {
      count2++;
      m2.push_back(a);
    }
    if (m[a] == 3) {
      count--;
      m[a]--;
      goto end;
    }
    a = zone[a];
  }
end:
  int count3 = count - 2 * count2;
  ll count4 = (k - count3) % count2;
  // if(count4 == 0) count4 = count2;
  cout << m2[count4] + 1 << endl;
  // cout << count << " " << count2 << " " << count3 << " " << count4 << " " <<
  // m2[0] << m2[1] << m2[2] <<endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define llu unsigned long long
#define ll unsigned long long
ll read() {
  ll x;
  if (scanf("%lld", &x) == -1)
    exit(0);
  return x;
}
#define vi vector<int>
#define vb vector<bool>
const int INF = 1001001001;

#define LOCAL_JUDGE
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {
  // c_p_c();
  auto n = read();
  ll k = read();
  vi a(n);
  rep(i, 0, n) a[i] = read();
  vi s;
  vi 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>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define llu unsigned long long
#define ll unsigned long long
ll read() {
  ll x;
  if (scanf("%lld", &x) == -1)
    exit(0);
  return x;
}
#define vi vector<int>
#define vb vector<bool>
const int INF = 1001001001;

#define LOCAL_JUDGE
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 2 * 1e5;

int main() {
  // c_p_c();
  auto n = read();
  auto k = read();
  vi a(n);
  rep(i, 0, n) a[i] = read();
  vi s;
  int ord[N] = {0};
  int c = 1, l = 0;
  {
    int v = 1;
    while (!ord[v]) {
      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;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(200001);
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
  }

  map<int, int> mp;

  mp[1] = 0;
  int town = 1;
  int loop;
  int i = 1;
  while (1) {
    if (A[town] == 1 || mp[A[town]] != 0 || i == K) {
      loop = i - mp[A[town]];
      K = K - mp[A[town]];
      town = A[town];
      break;
    } else {
      mp[A[town]] = i;
      town = A[town];
    }
    i++;
  }
  K = K % loop;
  for (int i = 0; i < K; i++) {
    town = A[town];
  }
  cout << town << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
using namespace std;

typedef long long ll;

long long *A = new long long[200001];

int main() {
  long long N, K;
  cin >> N >> K;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
  }
  map<ll, ll> mp;
  mp[1] = 0;
  ll town = 1;
  ll loop = 0, a = 0;
  for (int i = 1; i < K + 1; i++) {
    // if(i == N){

    // }
    auto itr = mp.find(A[town]);
    if (itr != mp.end()) {
      loop = i - itr->second;
      a = K - i;
      town = A[town];
      break;
    } else {
      mp[A[town]] = i;
      town = A[town];
    }
  }
  loop = a % loop;
  for (int i = 0; i < loop; i++) {
    town = A[town];
  }
  cout << town << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> a(n);
  vector<bool> visited(n, false);

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

  int destination = 1;
  vector<int> history;

  while (true) {
    if (visited[destination - 1]) {
      break;
    }
    history.push_back(destination);
    visited[destination - 1] = true;
    destination = a[destination - 1];
  }

  int pos;
  for (int i = 0; i < history.size(); i++) {
    if (history[i] == destination) {
      pos = i;
      break;
    }
  }
  int length = history.size() - pos;

  if (k >= pos + 1) {
    k -= pos;
    k = k % length;
    cout << history[k + pos] << endl;
  } else {
    cout << history[k] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> a(n);

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

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

  cout << destination << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i < (ll)(n); ++i)
#define INF 5000000000
#define MOD 1000000007
typedef long long ll;

using namespace std;

ll ans;

int main() {
  cin.tie(0);
  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> a(N, INF);
  ll now = 0, cnt = 0;
  vector<ll> p;

  while (1) {
    if (a[now] != INF) {
      p.push_back(now);
      ll loopcnt = cnt - a[now]; // ループしてる数
      ll loopstart = a[now];     // ループのスタート
      // cout << ((K - loopstart) % loopcnt) + loopstart + 1 << endl;
      if (K >= loopstart) {
        ans = p[((K - loopstart) % loopcnt) + loopstart] + 1;
      } else {
        ans = p[K] + 1;
      }
      break;
    }

    a[now] = cnt;
    p.push_back(now);

    now = A[now];
    cnt++;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i < (ll)(n); ++i)
#define INF 5000000000
#define MOD 1000000007
typedef long long ll;

using namespace std;

ll ans;

int main() {
  cin.tie(0);
  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> a(N, INF);
  ll now = 0, cnt = 0;
  vector<ll> p;

  while (1) {
    if (a[now] != INF) {
      ll loopcnt = cnt - a[now]; // ループしてる数
      ll loopstart = a[now];     // ループのスタート
      ans = p[((K - loopstart) % loopcnt) + loopstart - 1];
      break;
    }

    a[now] = cnt;
    p.push_back(now);

    now = A[now];
    cnt++;
  }

  cout << A[ans] + 1 << endl;

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

#include <iostream>
#include <set>
#include <stack>
#include <vector>

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

  vector<int> hist;

  int v = 0;
  vector<int> x(n, -1);
  for (int i = 0; i <= n; ++i) {
    seen.at(v) = true;
    hist.push_back(v);
    x.at(v) = i;
    v = a.at(v);
    if (seen.at(v)) {
      break;
    }
  }
  if (k < x.at(v)) {
    cout << hist.at(k) + 1 << endl;
  } else {
    cout << hist.at((k - x.at(v)) % (hist.size() - x.at(v)) + x.at(v)) + 1
         << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <stack>
#include <vector>

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

  vector<int> hist;

  int v = a.at(0);
  vector<int> x(n, -1);
  for (int i = 0; i <= n; ++i) {
    seen.at(v) = true;
    hist.push_back(v);
    x.at(v) = i;
    v = a.at(v);
    if (seen.at(v)) {
      break;
    }
  }
  if (k <= x.at(v)) {
    cout << hist.at(k - 1) + 1 << endl;
  } else {
    cout << hist.at((k - x.at(v)) % (hist.size() - 1 - x.at(v)) + x.at(v)) + 1
         << endl;
  }

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

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

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

  map<int, int> reached;
  int c = 0;
  vector<int> li;
  rep(i, n + 1) {
    li.push_back(c);
    reached.insert(make_pair(c, i));
    c = a[c];
    if (reached.find(c) != reached.end())
      break;
  }
  int s = reached.at(c);
  int l = li.size() - s;
  int ansi = (k <= s) ? k : (s + (k - s) % l);
  int ans = li[ansi];

  cout << ans + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

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

  map<int, int> reached;
  int c = 0;
  vector<int> li;
  rep(i, k) {
    li.push_back(c);
    reached.insert(make_pair(c, i));
    c = a[c];
    if (reached.find(c) != reached.end())
      break;
  }
  int s = reached.at(c);
  int l = li.size() - s;
  int ansi = (k <= s) ? k : (s + (k - s) % l);
  int ans = li[ansi];

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

#include <iostream>
using namespace std;
long long n, k;
int a[200005], last[200005];
bool vis[200005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int take = min(k, 200000LL);
  k -= take;
  int cur = 1;
  while (take > 0) {
    cur = a[cur];
    take--;
  }
  if (k == 0) {
    cout << cur;
    return 0;
  }
  int len = 0;
  while (!vis[cur]) {
    vis[cur] = 1;
    len++;
    cur = a[cur];
  }
  k %= len;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int a[200005], last[200005];
int solve() {
  int n;
  long long k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++)
    last[i] = -1;
  last[1] = 0;
  int cur = 1, cnt = 0;
  while (k--) {
    if (last[a[cur]] == -1) {
      last[a[cur]] = last[cur] + 1;
      cur = a[cur];
    } else {
      cnt = last[cur] - last[a[cur]] + 1;
      cur = a[cur];
      break;
    }
  }
  if (k == 0)
    return cout << cur << endl, 0;
  k = k % cnt;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur << endl;
  return 0;
}
int main() { solve(); }
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2e5 + 5;
ll n, k;
int a[N];
bool vis[N];
vector<int> path;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  memset(vis, 0, sizeof vis);
  int pos = 1;
  vis[pos] = 1;
  path.push_back(1);
  while (!vis[a[pos]] && k > 0)
    pos = a[pos], vis[pos] = 1, path.push_back(pos), k--;

  if (k == 0)
    cout << pos << '\\n';
  else {
    int x = a[pos];
    int idx = 0;
    while (path[idx] != x)
      idx++;
    ll m = path.size() - idx;
    k = (k + m - 1) % m;
    int p = 0;
    while (p < k)
      p++;
    cout << path[idx + p] << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
vector<ll> v;
ll n, k, a[maxn], vis[maxn], cnt1 = 0, cnt2 = 0;
int main() {
  cin >> n >> k;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll t = a[1];
  while (true) {
    if (vis[t] < 2) {
      if (vis[t] == 1)
        v.push_back(t);
      vis[t]++;
      t = a[t];
    } else
      break;
  }
  for (int i = 1; i <= n; i++) {
    // cout<<"i="<<i<<"ai="<<a[i]<<"vis[i]="<<vis[i]<<endl;
    if (vis[i] == 1)
      cnt1++;
    if (vis[i] == 2)
      cnt2++;
  }
  //	cout<<cnt1<<"  "<<cnt2<<endl;
  cout << v[(k - cnt1 - 1) % cnt2] << endl;
}
/*
6 727202214173249351
6 5 2 5 3 2
*/
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int a[2000005], b[2000005], flag[2000005] = {0};
int main() {
  int n, i, j, mod;
  long long k;
  scanf("%d %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]); // a[i]存下一位
  flag[1] = 0;
  b[1] = a[1];
  i = 1;
  j = 1;
  while (flag[b[j]] == 0 && j <= k) {
    flag[b[j]] = j;
    b[j + 1] = a[b[j]];
    j++;
  }
  //	b[j]已经有了
  if (j > k) {
    printf("%d", b[k]);
    return 0;
  }
  mod = j - flag[b[j]];
  k = k - flag[b[j]];
  k = k % mod;
  k = k + flag[b[j]];
  printf("%d", b[k]);
}

Error: WA

Fixed submission:

#include <stdio.h>
int a[2000005], b[2000005], flag[2000005] = {0};
int main() {
  int n, i, j, mod;
  long long k;
  scanf("%d %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]); // a[i]存下一位
  flag[1] = 0;
  b[1] = a[1];
  i = 1;
  j = 1;
  while (flag[b[j]] == 0 || j > k) {
    flag[b[j]] = j;
    b[j + 1] = a[b[j]];
    j++;
  }
  //	b[j]已经有了
  mod = j - flag[b[j]];
  k = k - flag[b[j]];
  k = k % mod;
  k = k + flag[b[j]];
  printf("%d", b[k]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
using namespace std;
const int N = 200001;
bool vis[N];
vector<int> adj[N];
int cyclr(int x) {
  while (!vis[x]) {
    vis[x] = 1;
    x = adj[x][0];
  }
  return x;
}
int sizlo(int x) {
  int s = x;
  int c = 0;
  do {
    x = adj[x][0];
    c++;
  } while (x != s);

  return c;
}
int dfs(int x, int k, int cyc) {
  while (x != cyc && k > 0) {
    x = adj[x][0];
    k--;
  }
  if (x != cyc)
    return x;

  k = k % sizlo(cyc);
  while (k > 0) {
    x = adj[x][0];
    k--;
  }
  return x;
}
signed main() {
  ios_base::sync_with_stdio(1);
  cin.tie(0);
  cout.tie(0);
  int n, k, t;
  cin >> n >> k;
  // vector<int> arr(n+1);

  // vector<int> adj[n+1];
  for (int i = 0; i < n; i++) {
    cin >> t;
    adj[i + 1].push_back(t);
  }
  int cyc = cyclr(1);
  cout << dfs(1, k, cyc);

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
using namespace std;
int n, m;
bool check(int x, int y) {
  if (x >= 0 and y >= 0 and x < n and y < m)
    return true;
  return false;
}
signed main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  // for(int i=0; i<n; i++) cout<<a[i]<<" "; cout<<endl;
  vector<int> dp(n, 0);
  int i = 1, c = 0;
  for (; c < k; c++) {
    if (dp[i] == 0) {
      dp[i] = 1;
      i = a[i];
      // cout<<i<<" d"<<endl;
    } else
      break;
  }
  cout << i << endl;
  if (c == k)
    cout << i << endl;
  else {
    int j = 0, d = 0, l = 0;
    while (j != i) {
      j = a[j];
      d++;
    }
    for (; j < k; j++) {
      l = a[l];
      if (l == i)
        break;
    }
    k -= j;
    int mod = c - j;
    k = (k % mod);
    int e = 0;
    while (e < k) {
      e++;
      i = a[i];
    }
    cout << i << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

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

  int loop = 1;
  int cnt = 0;
  vector<int> b;
  vector<int> check(n + 1, -1);
  while (check[loop] == -1) {
    check[loop] = cnt;
    b.push_back(loop);
    cnt++;
    loop = a[loop];
  }

  if (k < check[loop]) {
    cout << b[k] << "\\n";
  } else {
    int dif = check[loop];
    k -= dif;
    k %= (b.size() - check[loop]);
    // cout << dif <<  " " << cnt << " " << k<< "\\n";
    cout << b[k + dif] << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

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

  int loop = 1;
  int cnt = 0;
  vector<int> b;
  vector<int> check(n + 1, -1);
  while (1) {
    if (check[a[loop]] != -1)
      break;
    check[a[loop]] = cnt;
    b.push_back(loop);
    cnt++;
    loop = a[loop];
  }

  if (k <= check[loop]) {
    cout << b[loop] << "\\n";
  } else {
    int dif = check[a[loop]];
    k -= dif;
    k %= (cnt - check[a[loop]]);
    cout << b[k + dif] << "\\n";
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k, king = 1, beg = 1, move = 0;
  cin >> n >> k;
  vector<long long> arr(n + 1), vis(n + 1);
  for (long long i = 1; i <= n; i++)
    cin >> arr[i];
  if (arr[1] == 1) {
    cout << 1 << endl;
    return 0;
  }

  while (1) {
    if (vis[king])
      break;
    vis[king] = 1;
    king = arr[king];
  }
  while (beg != king) {
    beg = arr[beg];
    move++;
  }
  if (k <= move) {
    beg = 1;
    while (k--)
      beg = arr[beg];
    cout << beg;
    return 0;
  }
  k = k - move;
  move = 0;
  for (long long i = 1; i <= n; i++)
    vis[i] = 0;
  while (1) {
    if (vis[king])
      break;
    vis[king] = 1;
    king = arr[king];
    move++;
  }
  k = k % move;
  while (k--)
    king = arr[king];
  cout << king;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, king = 1, beg = 1, move = 0;
  cin >> n >> k;
  vector<int> arr(n + 1), vis(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  if (arr[1] == 1) {
    cout << 1 << endl;
    return 0;
  }

  while (1) {
    if (vis[king])
      break;
    vis[king] = 1;
    king = arr[king];
  }
  while (beg != king) {
    beg = arr[beg];
    move++;
  }
  if (k <= move) {
    beg = 1;
    while (k--)
      beg = arr[beg];
    cout << beg;
    return 0;
  }
  k = k - move;
  move = 0;
  for (int i = 1; i <= n; i++)
    vis[i] = 0;
  while (1) {
    if (vis[king])
      break;
    vis[king] = 1;
    king = arr[king];
    move++;
  }
  k = k % move;
  while (k--)
    king = arr[king];
  cout << king;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ull = unsigned long long;

int main() {

  int n;
  ull k;

  cin >> n >> k;

  vector<int> v(n);

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

  vector<int> root;
  vector<int> done(n, -1);

  int index = 0;
  ull count = 0;
  while (done[index] == -1) {
    done[index] = count++;
    root.push_back(index);
    index = v[index];
  }

  ull loopCount = count - done[index];
  /*
  cout << "count = " << count << endl;
  cout << "loopCount = " << loopCount << endl;
  cout << k - index << endl;
  cout << index + (k - index) % loopCount;
  */
  if (k < count)
    cout << root[k] + 1 << endl;
  else {
    ull a = count - loopCount;
    cout << root[a + (k - a) % loopCount] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ull = unsigned long long;

int main() {

  int n;
  ull k;

  cin >> n >> k;

  vector<int> v(n);

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

  vector<int> root;
  vector<int> done(n, -1);

  int index = 0;
  int count = 0;
  while (done[index] == -1) {

    done[index] = count++;
    root.push_back(index);
    index = v[index];
  }

  int loopCount = count - done[index];

  if (k < count)
    cout << root[k] + 1 << endl;
  else
    cout << root[index + (k - index) % loopCount] + 1 << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  vector<long long> teleport(n, 0);
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    teleport[i] = a;
  }
  int i = teleport[0];
  vector<bool> visited(n, false);
  visited[0] = true;
  k--;
  int cycle = 0;
  int start = 0;
  while (k > 0 && start != i) {
    if (visited[i - 1]) {
      if (start == 0) {
        start = i;
      } else {
        cycle++;
      }
    }
    visited[i - 1] = true;
    i = teleport[i - 1];
    k--;
  }
  if (k > 0) {
    k = k % (cycle + 1);
  }
  while (k > 0) {
    i = teleport[i - 1];
    k--;
  }
  cout << i;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  vector<long long> teleport;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    teleport.push_back(a);
  }
  int i = teleport[0];
  vector<bool> visited(n, false);
  visited[0] = true;
  k--;
  int cycle = 0;
  int start = 0;
  while (k > 0 && start != i) {
    if (visited[i - 1]) {
      if (start == 0) {
        start = i;
      } else {
        cycle++;
      }
    }
    i = teleport[i - 1];
    visited[i - 1] = true;
    k--;
    cycle++;
  }
  if (k > 0) {
    k = k % cycle;
  }
  while (k > 0) {
    i = teleport[i - 1];
    k--;
  }
  cout << i;
}