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

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

int main() {
  ll n, k, a[200005] = {}, visitedTurn[200005];
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    visitedTurn[i] = -1;
  }
  visitedTurn[1] = 0;
  ll townNumber = 1, loopNumber = 1, teleportNumber = 0, previousTown = 1;
  while (true) {
    previousTown = townNumber;
    townNumber = a[townNumber];
    teleportNumber++;
    if (teleportNumber == k) {
      cout << townNumber << endl;
      return 0;
    }
    if (visitedTurn[townNumber] != -1) {
      break;
    }
    visitedTurn[townNumber] = teleportNumber;
  }
  k -= visitedTurn[townNumber];
  ll needTeleportNumber =
      k % (visitedTurn[previousTown] - visitedTurn[townNumber] + 1);
  for (ll i = 0; i < needTeleportNumber; i++) {
    townNumber = a[townNumber];
  }
  cout << townNumber << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll n, k, a[100005] = {}, visitedTurn[100005];
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    visitedTurn[i] = -1;
  }
  visitedTurn[1] = 0;
  ll townNumber = 1, loopNumber = 1, teleportNumber = 0, previousTown = 1;
  while (true) {
    previousTown = townNumber;
    townNumber = a[townNumber];
    teleportNumber++;
    if (visitedTurn[townNumber] != -1) {
      break;
    }
    visitedTurn[townNumber] = teleportNumber;
  }
  k -= visitedTurn[townNumber];
  ll needTeleportNumber =
      k % (visitedTurn[previousTown] - visitedTurn[townNumber] + 1);
  for (ll i = 0; i < needTeleportNumber; i++) {
    townNumber = a[townNumber];
  }
  cout << townNumber << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(obj) (obj).begin(), (obj).end()
const int inf = 1e9;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAX = 1e9 + 7;

int main() {
  ll n, k, tmp;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> tmp;
    tmp--;
    a[i] = tmp;
  }
  set<ll> used;
  ll current_place = 0;
  vector<ll> history;
  while (used.count(current_place) == 0) {
    used.insert(current_place);
    history.push_back(current_place);
    current_place = a[current_place];
  }
  history.push_back(current_place);
  used.insert(current_place);
  ll loop_start = INF, loop_end;
  rep(i, history.size()) {
    if (history[i] == current_place) {
      if (loop_start == INF)
        loop_start = i;
      else
        loop_end = i;
    }
  }
  if (k <= loop_start) {
    current_place = 0;
    rep(i, k) current_place = a[current_place];
  } else {
    ll period = loop_end - loop_start;
    ll final_place = (k - loop_start) % period;
    current_place = 0;
    rep(i, final_place + loop_start) current_place = a[current_place];
  }
  cout << current_place + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(obj) (obj).begin(), (obj).end()
/*using P = pair<int, int>;*/
const int inf = 1e9;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAX = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll tmp;
  rep(i, n) {
    cin >> tmp;
    tmp--;
    a[i] = tmp;
  }
  bool loop_found = false;
  ll current_place = 0;
  vector<ll> history;
  history.push_back(0);
  while (!loop_found) {
    current_place = a[current_place];
    if (find(history.begin(), history.end(), current_place) != history.end())
      loop_found = true;
    history.push_back(current_place);
  }
  ll loop_start = INF, loop_end;
  rep(i, history.size()) {
    if (history[i] == current_place) {
      if (loop_start == INF)
        loop_start = i;
      else
        loop_end = i;
    }
  }
  /*cout << loop_start << " " << loop_end << endl;*/
  if (k <= loop_start) {
    current_place = 0;
    rep(i, k) { current_place = a[current_place]; }
    cout << current_place + 1 << endl;
    return 0;
  }
  ll period = loop_end - loop_start;
  ll final_place = (k - loop_start) % period;
  current_place = 0;
  rep(i, final_place + loop_start) { current_place = a[current_place]; }
  cout << current_place + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

#define int long long

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  vector<int> cyc;
  cyc.push_back(1);
  vector<int> lastind(n + 1, -1);
  lastind[1] = 0;

  int x = 1;
  while (true) {
    x = a[x];
    cyc.push_back(x);
    if (cyc.size() - 1 == k) {
      cout << cyc.back() << endl;
      return 0;
    }
    if (lastind[x] != -1) {
      vector<int> path(cyc.begin() + lastind[x] + 1, cyc.end());
      k -= lastind[x];
      int ind = k % path.size() - 1;
      if (ind < 0)
        ind = path.size() - 1;
      cout << path[ind] << endl;
      return 0;
    }
    lastind[x] = cyc.size() - 1;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

#define int long long

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  vector<int> cyc;

  int x = 1;
  while (true) {
    x = a[x];
    cyc.push_back(x);
    if (x == 1)
      break;
  }

  int ind = (k % cyc.size()) - 1;
  if (ind < 0)
    ind = cyc.size() - 1;
  cout << cyc[ind] << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long mod = 1e9 + 7;
const int iINF = 0x3f3f3f3f;
const long long INF = 1LL << 60;
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  int g[300300] = {};
  rep(i, n) {
    int x;
    cin >> x;
    --x;
    g[i] = x;
  }

  int tmp = 0;
  int last = 0;
  int index = 0;
  vector<int> vec;
  map<int, int> mp;

  for (;;) {
    auto it = mp.find(tmp);
    if (it == mp.end()) {
      mp[tmp] = index;
      last = tmp;
      vec.push_back(tmp);
      tmp = g[tmp];
      index++;
    } else {
      int index = mp[tmp];
      ll size = mp[last] - index + 1;

      if (k <= index) {
        cout << vec[k] + 1 << endl;
        return 0;
      } else {
        ll amari = (k - index) % size;
        cout << vec[index + amari] + 1 << endl;
        return 0;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long mod = 1e9 + 7;
const int iINF = 0x3f3f3f3f;
const long long INF = 1LL << 60;
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  int g[300300] = {};
  rep(i, n) {
    int x;
    cin >> x;
    --x;
    g[i] = x;
  }

  vector<int> visited;

  int tmp = 0;
  vector<int> vec;

  for (;;) {
    auto iter = find(vec.begin(), vec.end(), tmp);
    if (iter == vec.end()) {
      vec.push_back(tmp);
      tmp = g[tmp];
    } else {
      int index = iter - vec.begin();
      ll size = vec.end() - iter;

      if (k <= index) {
        cout << vec[k] + 1 << endl;
        return 0;
      } else {

        ll amari = (k - index) % size;
        cout << vec[index + amari] + 1 << endl;
        return 0;
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  unsigned long long n, k;
  cin >> n >> k;
  vector<unsigned long long> teleporter(n);
  for (unsigned long long i = 0; i < n; ++i)
    cin >> teleporter[i];
  vector<unsigned long long> jumped(n, -1);
  unsigned long long num_jump = 0;
  unsigned long long cur_pos = 1;
  jumped[cur_pos - 1] = num_jump++;
  unsigned long long loop_length = 0;
  unsigned long long num_in_loop = 0;
  while (true) {
    auto pos = teleporter[cur_pos - 1];
    if (jumped[pos - 1] == -1) {
      jumped[pos - 1] = num_jump++;
      cur_pos = pos;
    } else {
      loop_length = num_jump - jumped[pos - 1];
      num_in_loop = jumped[pos - 1];
      break;
    }
  }
  unsigned long long num_jump_loop = 0;
  if (k > num_in_loop) {
    num_jump_loop = (k - num_in_loop) % loop_length;
    num_jump = num_in_loop + num_jump_loop;
  } else {
    num_jump = k;
  }
  for (unsigned long long i = 0; i < n; ++i) {
    if (jumped[i] == num_jump) {
      cout << i + 1 << endl;
      break;
    }
  }
  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  unsigned long long n, k;
  cin >> n >> k;
  vector<unsigned long long> teleporter(n);
  for (unsigned long long i = 0; i < n; ++i)
    cin >> teleporter[i];
  vector<bool> jumped(n, false);
  jumped[0] = true;
  unsigned long long next_pos = teleporter[0];
  unsigned long long counter = 1;
  while (true) {
    if (jumped[next_pos - 1])
      break;
    jumped[next_pos - 1] = true;
    next_pos = teleporter[next_pos - 1];
    ++counter;
  }
  vector<unsigned long long> loop = {next_pos};
  while (true) {
    next_pos = teleporter[next_pos - 1];
    if (loop[0] == next_pos)
      break;
    loop.push_back(next_pos);
  }
  if (k < counter + loop.size()) {
    cout << loop[k - counter] << endl;
  } else {
    cout << loop[(k - counter - loop.size()) % loop.size()] << endl;
  }
  return EXIT_SUCCESS;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int seen[200010];

int main() {
  ll n, k;
  cin >> n >> k;
  rep(i, n) seen[i] = -1;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int b = 0;
  int cycle = 0;
  int x = 0;
  rep(i, n) {
    if (i == k) {
      cout << b + 1 << endl;
      return 0;
    }
    if (seen[b] != -1) {
      x = seen[b];
      cycle = i - seen[b];
      break;
    }
    seen[b] = i;
    b = a[b];
  }
  k = (k - x) % cycle + x;
  b = 0;
  rep(i, k) { b = a[b]; }
  cout << b + 1 << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int seen[10];

int main() {
  ll n, k;
  cin >> n >> k;
  rep(i, n) seen[i] = -1;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int b = 0;
  int cycle = 0;
  int x = 0;
  rep(i, n) {
    if (i == k - 1) {
      cout << b << endl;
      return 0;
    }
    if (seen[b] != -1) {
      x = seen[b];
      cycle = i - seen[b];
      break;
    }
    seen[b] = i;
    b = a[b];
  }
  k = (k - x) % cycle + x;
  b = 0;
  rep(i, k) { b = a[b]; }
  cout << b + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MAX = 2e5;
int n;
ll k;
int a[MAX];
bool visited[MAX];
int main() {
  ll ans = 0;
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int pos = 0;
  visited[pos] = true;
  int cnt = 0;
  rep(i, n) {
    cnt++;
    pos = a[pos];
    if (visited[pos])
      break;
  }
  int loop_start = pos;
  if (cnt >= k) {
    pos = 0;
    rep(i, k) { pos = a[pos]; }
    ans = pos;
  } else {
    pos = 0;
    rep(i, n) {
      if (pos == loop_start)
        break;
      pos = a[pos];
      cnt--;
      k--;
    }
    int loop_size = cnt;
    pos = loop_start;
    rep(i, k % loop_size) { pos = a[pos]; }
    ans = pos;
  }
  ans++;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h> //復習必須
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
using namespace std;
#define P pair<int, int>
int main() {
  ll N;
  unsigned long long K;
  cin >> N >> K;
  ll a[N];
  ll c[N];
  ll num[N];
  ll now = 0;
  ll nowans = 0;
  ll count = 0;
  ll loop, ama;
  bool b[N];
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
    b[i] = false;
    num[i] = i + 1;
  }

  while (1) {
    if (b[now] == false) {
      b[now] = true;
      c[now] = count;
      now = a[now] - 1;
    } else {
      loop = count - c[now] + 1;
      ama = c[now];
      break;
    }

    count++;
  }

  K = (unsigned long long)ama + (K % (unsigned long long)loop);

  for (unsigned long long k = 0; k < K; k++) {
    nowans = a[nowans];
  }

  cout << nowans + 1 << endl;

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int a[N], n;
bool vis[N];
ll k;
int main() {
  scanf("%d%lld", &n, &k), k++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int pos = 1;
  vector<int> vec;
  while (!vis[pos]) {
    vec.push_back(pos);
    vis[pos] = true;
    pos = a[pos];
  }
  if (k <= vec.size()) {
    printf("%d\\n", vec[k - 1]);
  } else {
    int t = a[vec[vec.size() - 1]], x;
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i] == t) {
        x = vec.size() - i;
        break;
      }
      k--;
    }
    k %= x;
    t = vec[vec.size() - 1];
    while (k--) {
      t = a[t];
    }
    printf("%d\\n", t);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int a[N], n;
bool vis[N];
ll k;
int main() {
  scanf("%d%lld", &n, &k), k++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int pos = 1;
  vector<int> vec;
  while (!vis[pos]) {
    vec.push_back(pos);
    vis[pos] = true;
    pos = a[pos];
  }
  if (k < vec.size()) {
    printf("%d\\n", vec[k]);
  } else {
    int t = a[vec[vec.size() - 1]], x;
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i] == t) {
        x = vec.size() - t;
        break;
      }
      k--;
    }
    k %= x;
    t = vec[vec.size() - 1];
    while (k--) {
      t = a[t];
    }
    printf("%d\\n", t);
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-5
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

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

  map<int, int> mp;
  vector<int> pos;
  pos.push_back(1);
  mp[1] = 1;
  ll ind = -1;
  int cur = vec[1];
  for (int j = 2;; ++j) {
    if (mp[cur]) {
      ind = mp[cur];
      break;
    }
    pos.push_back(cur);
    mp[cur] = j;
    cur = vec[cur];
  }

  if (k >= ind - 1) {
    k -= ind - 1;
    reverse(all(pos));
    for (int i = 0; i < ind - 1; ++i)
      pos.pop_back();
    reverse(all(pos));
  }
  cout << pos[k % (ll)(pos.size())] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-5
// #define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

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

  map<int, int> mp;
  vector<int> pos;
  pos.push_back(1);
  mp[1] = 1;
  ll ind = -1;
  int cur = vec[1];
  for (int j = 2;; ++j) {
    if (mp[cur]) {
      ind = mp[cur];
      break;
    }
    pos.push_back(cur);
    mp[cur] = j;
    cur = vec[cur];
  }

  /*cout<<ind<<endl;
  for(auto& it : pos) cout<<it<<" ";
      cout<<endl;*/

  k -= ind - 1;
  reverse(all(pos));
  for (int i = 0; i < ind - 1; ++i)
    pos.pop_back();
  reverse(all(pos));
  cout << pos[k % (ll)(pos.size())] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, n, op) for (int i = a; i < (int)(n); i##op##op)
#define cout(val) cout << val << endl

using namespace std;

int main() {
  int n, first;
  long long k;
  cin >> n >> k;
  vector<int> town(n);
  rep(i, n) cin >> town.at(i);

  int nowTown = 1;
  int time = 1;
  vector<int> visit(n, 0);
  visit.at(0) = 1;
  while (true) {
    time++;
    nowTown = town[nowTown - 1];
    if (visit[nowTown - 1] == 0) {
      visit[nowTown - 1] = time;
    } else {
      first = visit[nowTown - 1] - 1;
      time -= first + 1;
      break;
    }
  }
  nowTown = 1;
  int l = (k - first) % time + first;
  if (l > k)
    l = k;
  rep(i, l) nowTown = town[nowTown - 1];
  cout(nowTown);
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  long k;
  cin >> n >> k;
  vector<int> telp(n);
  for (int i = 0; i < n; i++) {
    cin >> telp.at(i);
  }

  int nowTown = 1;
  while (k != 0) {
    nowTown = telp[nowTown - 1];
    k--;
  }
  cout << nowTown << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// AtCoder template
// 最近sabaがかまってくれないからキレて暴れてる
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

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

  // サイクル検出
  vector<bool> visited(n, false);
  int now = 0;
  int start;
  while (true) {
    if (visited[now]) {
      start = now;
      break;
    }
    visited[now] = true;
    now = a[now];
  }

  // サイクル長を求める
  ll cycle_d = 0;
  now = start;
  visited.assign(n, false);
  vector<int> cycle;
  while (true) {
    if (visited[now]) {
      break;
    }
    cycle.push_back(now);
    visited[now] = true;
    now = a[now];
    cycle_d++;
  }

  // 0からサイクル始点までの距離を求める
  ll dist = 0;
  now = 0;
  while (true) {
    if (now == start) {
      break;
    }
    now = a[now];
    dist++;
  }

  // 計算

  // 出力
  if (k >= dist)
    cout << cycle[(k - dist) % cycle_d] + 1 << endl;
  else {
    now = 0;
    while (k) {
      now = a[now];
      k--;
    }
    cout << now + 1 << endl;
  }
}

Error: 0

Fixed submission:

// AtCoder template
// 最近sabaがかまってくれないからキレて暴れてる
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

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

  // サイクル検出
  vector<bool> visited(n, false);
  int now = 0;
  int start;
  while (true) {
    if (visited[now]) {
      start = now;
      break;
    }
    visited[now] = true;
    now = a[now];
  }

  // サイクル長を求める
  ll cycle_d = 0;
  now = start;
  visited.assign(n, false);
  vector<int> cycle;
  while (true) {
    if (visited[now]) {
      break;
    }
    cycle.push_back(now);
    visited[now] = true;
    now = a[now];
    cycle_d++;
  }

  // 0からサイクル始点までの距離を求める
  ll dist = 0;
  now = 0;
  while (true) {
    if (now == start) {
      break;
    }
    now = a[now];
    dist++;
  }

  // 計算

  // 出力
  cout << cycle[(k - dist) % cycle_d] + 1 << endl;
}