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++)
using namespace std;
typedef long long ll;

const int INF = 1001001001;
const int mod = 1000000007;

ll pow(ll base, ll order) {
  ll ans = 1;
  rep(i, order - 1) ans *= base;
  return ans;
}

int gcd(int a, int b) {
  if (a < b) {
    int c = a;
    a = b;
    b = c;
  }
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  vector<bool> visited(n, false);
  vector<int> route;
  route.push_back(0);
  int cycle = INF;
  rep(i, n) {
    int position = route.back();
    if (visited[position]) {
      cycle = position;
      route.pop_back();
      break;
    } else if (!visited[position]) {
      visited[position] = true;
      route.push_back(a[position]);
    }
  }
  if (cycle == INF)
    cycle = route.back();
  int bloop = 0, loop = 0;
  for (auto x : route) {
    if (x == cycle)
      break;
    bloop++;
  }
  loop = route.size() - bloop;
  int ans = 0;
  if (k < bloop)
    ans = route[k] + 1;
  else {
    k -= bloop;
    k %= loop;
    k += bloop;
    ans = route[k] + 1;
  }

  /*
  for (auto x : route) cout << x << " ";
  cout << endl;
  cout << "bool: " << endl;
  for (auto x : visited) cout << x << " ";
  cout << endl;
  cout << "cycle point: " << cycle << endl;
  cout << bloop << " " << loop << endl;
  */
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1001001001;
const int mod = 1000000007;

ll pow(ll base, ll order) {
  ll ans = 1;
  rep(i, order - 1) ans *= base;
  return ans;
}

int gcd(int a, int b) {
  if (a < b) {
    int c = a;
    a = b;
    b = c;
  }
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  vector<bool> visited(n, false);
  vector<int> route;
  route.push_back(0);
  int cycle;
  rep(i, n) {
    int position = route.back();
    if (visited[position]) {
      cycle = position;
      route.pop_back();
      break;
    } else if (!visited[position]) {
      visited[position] = true;
      route.push_back(a[position]);
    }
  }
  int bloop = 0, loop = 0;
  for (auto x : route) {
    if (x == cycle)
      break;
    bloop++;
  }
  loop = route.size() - bloop;
  k -= bloop;
  k %= loop;
  int ans = route[k] + 1;

  /*
  for (auto x : route) cout << x << " ";
  cout << endl;
  cout << "bool: " << endl;
  for (auto x : visited) cout << x << " ";
  cout << endl;
  cout << "cycle point: " << cycle << endl;
  cout << bloop << " " << loop << endl;
  */
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int D = 60; // 2の60乗まででダブリングの表が足りるので60．
const int MAX_N = 200010;
int to[D][MAX_N];

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

  int n;
  long long 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]]; }

  // k を2の指数表記で分解して，
  // 指数の大きい方から辿っていく．
  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }

  cout << v + 1 << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int D = 60; // 2の60乗まででダブリングの表が足りるので60．
const int MAX_N = 200010;
int to[D][MAX_N];

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

  int n;
  long long 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[j][i]]; }

  // k を2の指数表記で分解して，
  // 指数の大きい方から辿っていく．
  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }

  cout << v + 1 << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> was(n, false);
  long long cycle = 1, now = 0;
  vector<long long> path;
  while (!was[now]) {
    was[now] = true;
    now = a[now];
    k--;
    if (!k) {
      cout << now + 1;
      return 0;
    }
  }
  long long st = now;
  path.push_back(st);
  now = a[now];
  while (now != st) {
    path.push_back(now);
    cycle++;
    now = a[now];
  }
  k %= cycle;
  cout << path[k] + 1;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> was(n, false);
  long long cycle = 1, now = 0;
  vector<long long> path;
  while (!was[now]) {
    was[now] = true;
    now = a[now];
    k--;
  }
  long long st = now;
  path.push_back(st);
  now = a[now];
  while (now != st) {
    path.push_back(now);
    cycle++;
    now = a[now];
  }
  k %= cycle;
  cout << path[k] + 1;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &x : A) {
    cin >> x;
  }
  ll cnt = 0, now = 0;
  vector<ll> used(n, -1);
  used[0] = 0;
  for (i = 0; i < n; i++) {
    now = A[now] - 1;
    cnt++;
    if (used[now] >= 0)
      break;
    used[now] = i + 1;
    if (cnt == k) {
      cout << now + 1 << endl;
      return 0;
    }
  }
  // now が 始点
  ll ch = 0, s = 0;
  if (now != 0) {
    for (i = 0; i < n; i++) {
      ch = A[ch] - 1;
      s++;
      if (ch == now)
        break;
    }
  }
  vector<ll> loop;
  ch = now;
  do {
    loop.push_back(ch);
    ch = A[ch] - 1;
  } while (ch != now);
  ll c = (k - s) % (cnt - s);
  cout << loop[c] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &x : A) {
    cin >> x;
  }
  ll cnt = 0, now = 0;
  vector<ll> used(n, -1);
  used[0] = 0;
  for (i = 0; i < n; i++) {
    now = A[now] - 1;
    cnt++;
    if (used[now] >= 0)
      break;
    used[now] = i + 1;
  }
  // now が 始点
  ll ch = 0, s = 0;
  if (now != 0) {
    for (i = 0; i < n; i++) {
      ch = A[ch] - 1;
      s++;
      if (ch == now)
        break;
    }
  }
  vector<ll> loop;
  ch = now;
  do {
    loop.push_back(ch);
    ch = A[ch] - 1;
  } while (ch != now);
  ll c = (k - s) % (cnt - s);
  cout << loop[c] + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#define ll long long
#define N 10000
#define mod 1000000007
#define LL 100
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  ll num[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> num[i];

  map<ll, ll> data;
  ll count1 = 1;
  ll count2 = 0;
  ll ans = 1; // 現在の位置
  vector<ll> pp;
  data[1]++;
  // cout<<num[1];

  while (true) {
    // cout<<ans<<endl;
    ans = num[ans];
    if (data[ans] == 0) {
      count1++;
      data[ans]++;
    } else if (data[ans] == 1) {
      pp.push_back(ans);
      count2++;
      data[ans]++;
    } else
      break;
  }
  if (k >= count1) {
    ll p = k - count1;
    // cout<<count1<<endl;
    ll r = p % count2;
    // cout<<count2<<endl;
    cout << pp[r] << endl;
  } else {
    ans = 1;
    for (int i = 0; i < k; i++) {
      ans = num[ans];
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#define ll long long
#define N 10000
#define mod 1000000007
#define LL 100
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  ll num[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> num[i];

  map<ll, ll> data;
  ll count1 = 1;
  ll count2 = 0;
  ll ans = 1; // 現在の位置
  vector<ll> pp;
  data[1]++;
  // cout<<num[1];

  while (true) {
    // cout<<ans<<endl;
    ans = num[ans];
    if (data[ans] == 0) {
      count1++;
      data[ans]++;
    } else if (data[ans] == 1) {
      pp.push_back(ans);
      count2++;
      data[ans]++;
    } else
      break;
  }
  // cout<<pp[2]<<endl;
  ll p = k - count1;
  // cout<<count1<<endl;
  ll r = p % count2;
  // cout<<count2<<endl;
  cout << pp[r] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
const ll INF = 999999999999999;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    scanf("%ld", &(a.at(i)));
    --a.at(i);
  }
  vector<bool> b(n, false);

  // cnt : テレポート回数
  ll cnt = 0;
  // now : 現在の街のid , lp_id : ループの始まりであり、終わりでもある街
  ll now = 0, lp_id;
  while (1) {
    // k回テレポートした場合
    if (cnt == k) {
      cout << now + 1 << endl;
      return 0;
    }
    // すでに訪れたかどうか <- 訪れているのならループしている
    if (b.at(now)) {
      lp_id = now;
      break;
    } else {
      b.at(now) = true;
    }
    // テレポートする
    now = a.at(now);
    ++cnt;
  }

  k -= cnt;
  ll lp_len = 0;
  // ループの長さを数える
  while (1) {
    if (lp_len != 0 && now == lp_id)
      break;
    now = a.at(now);
    ++lp_len;
  }

  // kの残り
  k %= lp_len;

  cnt = 0;
  now = lp_id;

  while (1) {
    if (cnt == k) {
      cout << now + 1 << endl;
      return 0;
    }
    now = a.at(now);
    ++cnt;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ull i = 0; i < (ull)n; ++i)
const ull INF = 999999999999999;
ull n, m, x;
ull ans = INF;
vector<bool> b;  // すでにチェック済みかどうか
vector<ull> dis; // 0から何ステップでたどり着けるか
vector<ull> order;
vector<ull> a;
ull until_loop;

ull func(ull i, ull stp) {
  if (i != 0)
    i--;
  if (b.at(i)) {
    until_loop = stp;
    return stp - dis.at(i);
  }
  if (stp == n)
    return INF;
  b.at(i) = true;
  order.push_back(i);
  dis.at(i) = stp;
  func(a.at(i), stp + 1);
}

int main() {
  ull n, k;
  cin >> n >> k;
  a.resize(n);
  b.resize(n, false);
  dis.resize(n);
  rep(i, n) cin >> a.at(i);
  ull num = func(0, 0);
  if (k < until_loop) {
    cout << order.at(k) << endl;
    return 0;
  } else {
    num = (k - until_loop) % num;
    cout << order.at(num) << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;

int a[200100];

signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int arr = 1, curr = 1;
  if (k <= n) {
    while (k) {
      arr = a[arr];
      k--;
    }
    cout << arr;
    return 0;
  } else {
    vector<int> v, cnt(n + 5, 0);
    cnt[1] = 1;
    arr = a[1];
    k--;
    while (k > 0) {
      if (cnt[arr]) {
        break;
      }
      cnt[arr] = 1;
      arr = a[arr];

      k--;
    }
    int ver = arr;
    v.pb(ver);
    arr = a[ver];
    v.pb(arr);
    while (k > 0) {
      if (a[arr] == ver) {
        break;
      }
      arr = a[arr];
      v.pb(arr);
    }
    k %= v.size();
    cout << v[k];
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;

int a[200100];

signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int arr = 1, curr = 1;
  if (k <= n) {
    while (k) {
      arr = a[arr];
      k--;
    }
    cout << arr;
    return 0;
  } else {
    vector<int> v, cnt(n + 5, 0);
    cnt[1] = 1;
    arr = a[1];
    k--;
    while (k > 0) {
      if (cnt[arr]) {
        break;
      }
      arr = a[arr];
      cnt[arr] = 1;
      k--;
    }
    int ver = arr;
    v.pb(ver);
    arr = a[ver];
    v.pb(arr);
    while (k > 0) {
      if (a[arr] == ver) {
        break;
      }
      arr = a[arr];
      v.pb(arr);
    }
    k %= v.size();
    cout << v[k];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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;
  int 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: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i]--;
  queue<int> q;
  q.push(0);
  rep(i, k) {
    int v = q.front();
    q.pop();
    q.push(a[v]);
  }
  int ans = q.front();
  ans++;
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define INF (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define N 100005
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795

// vl a(1001);

ll powM(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans % m;
}

int main() {

  ll n, k;
  cin >> n >> k;
  vl a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  ll st = 1;
  vector<int> vis(200001);
  ll cnt = 1;
  vis[st] = cnt;
  ll rep = (ll)1e18 + 1;
  while (cnt != n + 1) {
    cnt++;
    st = a[st];
    k--;
    if (!vis[st])
      vis[st] = cnt;
    else {
      rep = cnt - vis[st];
      break;
    }
    if (!k)
      break;
  }

  if (k)
    k = k % rep;
  while (k) {
    k--;
    st = a[st];
  }

  cout << st << "\\n";

  return 0;
}

Error: 0

Fixed submission:

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

#define INF (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define N 100005
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795

// vl a(1001);

ll powM(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans % m;
}

int main() {

  ll n, k;
  cin >> n >> k;
  vl a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  ll st = 1;
  vector<int> vis(n + 1);
  ll cnt = 1;
  vis[st] = cnt;
  ll rep = 0;
  while (cnt != n + 1) {
    cnt++;
    st = a[st];
    k--;
    if (!vis[st])
      vis[st] = cnt;
    else {
      rep = cnt - vis[st];
      break;
    }
    if (!k)
      break;
  }
  k = k % rep;
  while (k) {
    k--;
    st = a[st];
  }

  cout << st << "\\n";

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

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N, 0);
  vector<int> p(N, 0), q(N, -1);
  p[0] = 1;
  q[0] = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int n = 0;
  long long second = 0, first = K;
  for (long long i = 0; i < K; i++) {
    n = A[n] - 1;
    if (p[n] == 0) {
      p[n] = 1;
      q[i + 1] = n;
    } else if (p[n] == 1) {
      second = i + 1;
      break;
    }
  }
  for (int j = 0; j < second; j++) {
    if (q[j] == n) {
      first = j;
      break;
    }
  }
  cout << q[first + (K - first) % (second - first)] + 1;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long N, K;
  cin >> N >> K;
  int A[N];
  int p[N], q[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    p[i] = 0;
  int n = 1;
  long long i, j;
  for (i = 0; i < K; i++) {
    if (p[n - 1] == 0) {
      p[n - 1] = 1;
      q[i] = n;
    } else if (p[n - 1] == 1) {
      break;
    }
    n = A[n - 1];
  }
  for (j = 0; j < i; j++) {
    if (q[j] == n) {
      break;
    }
  }

  cout << q[j + (K - j) % (i - j)];
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
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;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll a[220000];
ll b[220000];
map<ll, vector<ll>> x;
int main() {
  ll n, k, ans = 1e11;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> a[i];
  ll cnt = 1;
  ll mm = 0;
  ll pos = 1;
  b[0] = 1;
  while (cnt <= n) {
    pos = a[pos];
    b[cnt] = pos;
    cnt++;
  }
  ll aa, bb = -1;
  rep(i, 0, n + 1) {
    x[b[i]].pb(i);
    if (x[b[i]].size() > 1) {
      aa = x[b[i]][0];
      bb = x[b[i]][1] - x[b[i]][0];
      break;
    }
  }
  ll cc = 0;
  if (k > n && bb != -1) {
    if (k >= aa)
      cc = (k - aa) % bb + aa;
    else
      cc = aa;
  } else if (k > n && bb == -1)
    cc = k;
  else if (k <= n)
    cc = k;

  cout << b[cc] << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
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;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll a[220000];
ll b[220000];
map<ll, vector<ll>> x;
int main() {
  ll n, k, ans = 1e11;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> a[i];
  ll cnt = 1;
  ll mm = 0;
  ll pos = 1;
  b[0] = 1;
  while (cnt <= n) {
    pos = a[pos];
    b[cnt] = pos;
    if (b[cnt] == 1) {
      mm = cnt;
      break;
    }
    cnt++;
  }
  ll aa, bb;
  rep(i, 0, n) {
    x[b[i]].pb(i);
    if (x[b[i]].size() > 1) {
      aa = x[b[i]][0];
      bb = x[b[i]][1] - x[b[i]][0];
      break;
    }
  }
  ll cc;
  if (bb != 0 && aa != 0)
    cc = (k - aa) % bb + aa;

  cout << b[cc] << endl;
  // rep(i,0,20)cout<<b[i]<<" ";
  // cout<<endl<<bb;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const long long INF = 1e6;
static const long long n = 200009;
using ll = long long;
long long N, K;
long long p;
long long roop, roop_time;
vector<long long> r(n, 0);
vector<long long> A(n, 0);
vector<long long> d(n, -1);

void solve(long long x) {
  while (d[A[x]] == -1) {
    d[A[x]] = d[x] + 1;
    x = A[x];
  }
  roop = A[x];
  roop_time = d[x] + 1 - d[roop];
  r[0] = roop + 1;
  long long k = 1;
  while (k <= roop_time) {
    r[k] = A[r[k - 1] - 1] + 1;
    k++;
  }
}

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> p;
    A[i] = p - 1;
  }
  d[0] = 0;
  solve(0);
  if (K < d[roop]) {
    rep(i, N) if (d[i] == K) {
      cout << i + 1 << endl;
      return 0;
    }
  } else {
    K -= d[roop];
    K = K % roop_time;
    cout << r[K] << endl;
    return 0;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const long long INF = 1e7;
static const long long n = 200009;
using ll = long long;
long long N, K;
long long p;
long long roop, roop_time;
vector<long long> r(n, 0);
vector<long long> A(n, 0);
vector<long long> d(n, INF);

void solve(long long x) {
  while (d[A[x]] == INF) {
    d[A[x]] = d[x] + 1;
    x = A[x];
  }
  roop = A[x];
  roop_time = d[x] + 1 - d[roop];
  d[roop] = d[x] + 1;
  r[0] = roop + 1;
  int k = 1;
  while (k <= roop_time) {
    r[k] = A[r[k - 1]] + 1;
    k++;
  }
}

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> p;
    A[i] = p - 1;
  }
  d[0] = 0;
  solve(0);
  if (K <= roop) {
    rep(i, N) if (d[i] == K) {
      cout << i + 1 << endl;
      return 0;
    }
  } else {
    K -= d[roop];
    K = K % roop_time;
    cout << r[K] << endl;
    return 0;
  }
}