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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e6 + 10;
LL k;
int n, a[N], len[N], tot, c[N], bk[N], id;
int main() {
  while (~scanf("%d%lld", &n, &k)) {
    memset(bk, 0, sizeof(bk));
    tot = 0;
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    if (k <= 1e7) {
      int now = 1;
      while (k--)
        now = a[now];
      cout << now << endl;
      return 0;
    }
    c[++tot] = 1;
    bk[1] = 1;
    int now = a[1];
    while (!bk[now]) {
      c[++tot] = now;
      bk[now] = 1;
      now = a[now];
    }
    for (int i = 1; i <= tot; i++)
      if (c[i] == now) {
        id = i;
        break;
      }
    if (k - 1 <= id) {
      cout << c[k] << endl;
      return 0;
    }
    LL len = tot - id + 1;
    k -= (id - 1);
    int ans = k % len;
    ans += id;
    cout << c[ans] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
LL k;
int n, a[N], len[N], tot, c[N], bk[N], id;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  c[++tot] = 1;
  bk[1] = 1;
  int now = a[1];
  while (!bk[now]) {
    c[++tot] = now;
    now = a[now];
  }
  for (int i = 1; i <= tot; i++)
    if (c[i] == now) {
      id = i;
      break;
    }
  int len = tot - id + 1;
  k -= (id - 1);
  k %= len;
  int ans = k + id;
  cout << c[ans] << endl;
}
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;

using ll = long long int;

int main() {
  // Input
  int N;
  ll K;
  vector<int> A;

  cin >> N >> K;
  A = vector<int>(N);
  rep(i, N) cin >> A[i];

  // Process
  int cnt = 0;
  vector<int> log;
  vector<int> visited(N, -1);

  log.emplace_back(1);
  while (visited[log[cnt] - 1] == -1) {
    visited[log[cnt] - 1] = cnt;

    const int newTown = A[log[cnt] - 1];
    log.emplace_back(newTown);
    cnt++;
  }
  int start = visited[log[cnt] - 1];
  int loop = cnt - start;

  int ans;
  if (K < cnt) {
    ans = log[K];
  } else {
    ll k = (K - start) % loop;
    ans = log[k + start];
  }

  // Output
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long int;

int main() {
  // Input
  int N;
  ll K;
  vector<int> A;

  cin >> N >> K;
  A = vector<int>(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  // Process
  int cnt = 1;
  vector<int> log(N, 0);
  int start, end; // [a, b]
  vector<bool> visited(N, false);
  while (cnt < K) {
    const int newTown = A[log[cnt - 1]] - 1;
    log[cnt] = newTown;
    if (visited[newTown]) {
      for (int i = 0; i < cnt; i++) {
        if (log[i] == newTown) {
          start = i;
          end = cnt - 1;
        }
      }
      break;
    } else {
      visited[newTown] = true;
      cnt++;
    }
  }

  int ans;
  if (cnt >= K) {
    ans = log[K - 1] + 1;
  } else {
    int k = K - start;
    int loop_num = end - start + 1;
    int idx = k % loop_num + start;
    ans = log[idx] + 1;
  }

  // Output
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int a[200006], b[200006];
int main() {
  cin.tie(0);
  long long int N, K;
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  int c, w = 1;
  int time_stamp = 1;
  for (int i = 1; i <= K; i++) {
    if (i == 1) {
      c = a[i];
    } else {
      c = a[c];
    }
    if (b[c] == 0) {
      b[c] = time_stamp;
      time_stamp++;
    } else {
      w = time_stamp - b[c];
      break;
    }
  }
  for (int i = 0; i < (K - time_stamp) % w; i++) {
    c = a[c];
  }
  cout << c << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200006], b[200006];
int main() {
  long long int N, K;
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  int c, w;
  int time_stamp = 1;
  for (long long int i = 1; i <= K; i++) {
    if (i == 1) {
      c = a[i];
    } else {
      c = a[c];
    }
    if (b[c] == 0) {
      b[c] = time_stamp;
      time_stamp++;
    } else {
      w = time_stamp - b[c];
      break;
    }
  }
  for (int i = 0; i < (K - time_stamp) % w; i++) {
    c = a[c];
  }
  cout << c << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (T &x : v) {
    s >> x;
  }
  return s;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  cin >> A;

  vector<int> done(N + 1, -1);
  vector<int> dest(N + 1, 0);

  dest[0] = 1;
  done[1] = 0;
  int l = -1, r = -1;
  for (int i = 1; i < N; i++) {
    int a = A[dest[i - 1] - 1];
    if (done[a] != -1) {
      l = done[a];
      r = i - 1;
      break;
    } else {
      dest[i] = a;
      done[a] = i;
    }
  }

  int ans;
  if (K <= r) {
    ans = dest[K];
  } else {
    ans = dest[l + (K - l) % (r - l + 1)];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (T &x : v) {
    s >> x;
  }
  return s;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  cin >> A;

  vector<int> done(N + 1, -1);
  vector<int> dest(N + 1, 0); // i回目の行き先

  dest[0] = 1;
  done[1] = 0;
  int l = -1, r = -1;
  for (int i = 1; i < N; i++) {
    int a = A[dest[i - 1] - 1];
    if (done[a] != -1) {
      l = done[a];
      r = i - 1;
      break;
    } else {
      dest[i] = a;
      done[a] = i;
    }
  }

  int ans;
  // if (K <= r) {
  //     ans = dest[K];
  // } else {
  K -= l;
  ans = dest[l + (K - l) % (r - l + 1)];
  // }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes                                                                    \\
  cout << "Yes" << endl;                                                       \\
  return 0;
#define No                                                                     \\
  cout << "No" << endl;                                                        \\
  return 0;
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

template <typename T> void print(const T &v);

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

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes                                                                    \\
  cout << "Yes" << endl;                                                       \\
  return 0;
#define No                                                                     \\
  cout << "No" << endl;                                                        \\
  return 0;
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

template <typename T> void print(const T &v);

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> a(N);
  rep(i, N) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> dp(200001, -1);
  vector<bool> seen(200001, false);
  int next = a[0];
  int loop_start = 0;
  int loop_end = 200001;

  for (int i = 0; i < N; i++) {
    if (seen[next]) {
      for (int j = 0; j < dp.size(); j++) {
        if (dp[j] == next) {

          loop_start = j;
          break;
        }
      }
      loop_end = i;
      break;
    }
    seen[next] = true;
    dp[i] = next;
    next = a[next];
  }
  // cout << "loop_start:" << loop_start << " loop_end:" << loop_end << endl;
  if (loop_start + 1 == loop_end) {
    cout << dp[loop_start] + 1 << endl;
    return 0;
  }
  ll ans = (K - loop_end) % (loop_end - loop_start);
  cout << dp[loop_start + ans - 1] + 1 << endl;
  return 0;
}

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

#define rep(i, N) for (int i = 0; i < N; i++)

typedef long long ll;

const int mod = 1e9 + 7;

using namespace std;

vector<int> teleport_pow(const vector<int> &a, const ll &n) {
  if (n == 1)
    return a;

  vector<int> res(a.size());
  rep(i, res.size()) res[i] = i;

  rep(hoge, 2) rep(i, res.size()) { res[i] = a[res[i]]; }

  res = teleport_pow(res, n / 2);

  if (n % 2 != 0) {
    rep(i, res.size()) res[i] = a[res[i]];
  }

  return res;
}

int main(void) {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }

  cout << teleport_pow(A, K)[0] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int fact(int);

int main(void) {
  long long int N, K;
  int *A;

  cin >> N >> K;
  A = new int[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int town = 1;
  K = K % fact(N);

  for (int i = 0; i < K; i++) {
    town = A[town - 1];
  }

  cout << town << endl;

  return 0;
}

int fact(int n) {
  int y = 1;
  for (int i = 1; i <= n; i++) {
    y *= i;
  }
  return y;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll mod = 1e9 + 7;

int main() {
  fast
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      // #endif

      ll n,
      k;
  cin >> n >> k;
  ll arr[n + 5];

  for (ll i = 1; i <= n; i++)
    cin >> arr[i];

  ll temp = arr[1];
  ll cs = arr[1];
  vector<ll> ans2;
  ans2.pb(cs);

  set<ll> s;
  s.insert(cs);

  for (ll i = 2; i <= 3 * n; i++) {

    temp = arr[cs];

    ll prev = s.size();
    s.insert(temp);
    ll next = s.size();

    if (prev == next) {
      break;
    }

    ans2.pb(temp);
    cs = temp;
  }
  //  cout<<last<<endl;
  // for(ll i=0;i<ans2.size();i++)
  // cout<<ans2[i]<<" ";
  // cout<<endl;

  ll len = ans2.size();
  if (k <= len)
    cout << ans2[k - 1];
  else {
    ll id = 0;
    for (ll i = 0; i < len; i++) {
      if (ans2[i] == temp) {
        id = i;
        break;
      }
    }
    ll total = ans2.size() - id;
    // cout<<id<<" "<<total<<endl;
    k -= len;
    ll x = k % total;
    if (x == 0)
      x = total;

    // cout<<x<<" "<<id<<endl;
    cout << ans2[id + x - 1];
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll mod = 1e9 + 7;

int main() {
  fast
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      // #endif

      ll n,
      k;
  cin >> n >> k;
  ll arr[n + 5];

  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
  }

  ll temp = arr[1];
  ll cs = arr[1];
  vector<ll> ans2;
  ans2.pb(cs);

  set<ll> s;
  s.insert(cs);
  ll last;
  for (ll i = 2; i <= 3 * n; i++) {

    temp = arr[cs];

    ll prev = s.size();
    s.insert(temp);
    last = temp;
    if (prev == s.size()) {
      break;
    }
    ans2.pb(temp);
    cs = temp;
  }
  //  cout<<last<<endl;
  // for(ll i=0;i<ans2.size();i++)
  // cout<<ans2[i]<<" ";
  // cout<<endl;

  ll len = ans2.size();
  if (k <= len)
    cout << ans2[k - 1];
  else {
    ll id = 0;
    for (ll i = 0; i < len; i++) {
      if (ans2[i] == last) {
        id = i;
        break;
      }
    }
    ll total = n - id + 1;
    k -= len;
    ll x = k % total;
    if (x == 0)
      x = total;
    x -= 1;
    // cout<<x<<" "<<id<<endl;
    cout << ans2[id + x];
  }

  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 rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n), s, ord(n + 1, -1);
  rep(i, n) cin >> a[i];
  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;
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc167/tasks/abc167_d
テストは oj t
*/


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int b = a[0];
  set<int> s; // s.insert()
  s.insert(b);
  int cnt = 1;
  vector<int> c(n);
  c[b] = cnt;
  while (s.size() == cnt) {
    b = a[b - 1];
    s.insert(b);
    cnt++;
    c[b] = cnt;
    cout << b << endl;
  }
  int loop = cnt - c[b];
  if (k <= cnt) {
    cout << a[b - 1] << endl;
  } else {
    k = (k - cnt) % loop;
    cout << a[b - 1] << endl;
  }
  return 0;
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc167/tasks/abc167_d
テストは oj t
*/
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  repA(i, 1, n) cin >> a[i];
  map<ll, ll> mp;
  ll cnt = 1;
  ll i = 1;
  vi v;
  while (mp[i] == 0 && k) {
    // cout<<i<<" "<<cnt<<endl;
    v.pb(i);
    mp[i] = cnt;
    cnt++;
    i = a[i];
    k--;
  }
  if (k == 0)
    cout << i << endl;
  else {
    ll cyc = cnt - mp[i];
    //  cout<<cyc;
    k %= cyc;
    cout << v[mp[i] - 1 + k] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int main() {
  ll n, k;
  cin >> n >> k;
  int a[n];
  repA(i, 1, n) cin >> a[i];
  map<ll, ll> mp;
  int cnt = 1;
  int i = 1;
  while (mp[i] == 0 && k) {
    // cout<<i<<" "<<cnt<<endl;
    mp[i] = cnt;
    cnt++;
    i = a[i];
    k--;
  }
  if (k == 0)
    cout << i << endl;
  else {
    int cyc = cnt - mp[i];
    //  cout<<cyc;
    k %= cyc;
    while (k) {
      i = a[i];
      k--;
    }
    cout << i << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << #x << ": " << x << '\\n'
const char newl = '\\n';
typedef long long int Int;
typedef pair<int, int> P;
using ll = long long;
using VI = vector<int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

int main() {
  int n;
  Int 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, 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;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << #x << ": " << x << '\\n'
const char newl = '\\n';
typedef long long int Int;
typedef pair<int, int> P;
using ll = long long;
using VI = vector<int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
bool check(vector<int> bt) {
  for (auto &i : bt) {
    if (i >= 3) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  Int k;
  cin >> k;
  vector<int> a(n + 1);
  vector<int> tes(n + 1, 0);
  vector<int> search;
  REP(i, 1, n + 1) { cin >> a[i]; }
  int pos = 1;
  int sum = 0;
  while (check(tes)) {
    tes[pos]++;
    if (tes[pos] == 2) {
      search.push_back(pos);
    }
    pos = a[pos];
    sum++;
    if (sum >= k) {
      cout << pos << endl;
      return 0;
    }
  }
  /*for(auto &i:tes){
      cout << i << " ";
  }
  cout << endl;
  for(auto &i:search){
      cout << i << " ";
  }
  cout << endl;
  debug(k);
  debug(sum);*/
  Int zyuuyou = k - sum;
  Int ss = zyuuyou % (search.size() - 1);
  // debug(ss);
  Int ans = search[ss];
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 2147483647; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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() {
  ll n, k;
  cin >> n >> k;
  vector<ll> t(n, 0);
  rep(i, n) {
    cin >> t[i];
    --t[i];
  }
  vector<ll> path;
  ll cnt = 0;
  unordered_set<ll> us;
  ll now = 0;
  while (us.count(now) == 0) { // 探索済みの町に到達するまで
    path.push_back(now);
    us.insert(now);
    now = t[now];
  }
  ll s = find(path.begin(), path.end(), now) - path.begin();
  ll r = (ll)path.size() - s;
  if (k < s) { // ループ部分に到達しない時
    cout << path[k] + 1 << endl;
    return 0;
  } else { // ループ部分に到達する時
    cout << path[(k - s) % r + s] + 1 << endl;
    return 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 2147483647; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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() {
  ll n, k;
  cin >> n >> k;
  vector<ll> t(n, 0);
  rep(i, n) {
    cin >> t[i];
    --t[i];
  }
  vector<ll> path;
  path.push_back(0);
  path.push_back(t[0]);
  ll cnt = 0;
  while (1) {
    path.push_back(t[path.back()]);
    if (find(path.begin(), path.end(), t[path.back()]) != path.end()) {
      path.push_back(t[path.back()]);
      break;
    }
  }
  ll now = path.back();
  path.pop_back();
  ll s = find(path.begin(), path.end(), now) - path.begin();
  ll r = (ll)path.size() - s;
  if (k < s) { // ループ部分に到達しない時
    cout << path[k] + 1 << endl;
    return 0;
  } else { // ループ部分に到達する時
    cout << path[(k - s) % r + s] + 1 << endl;
    return 0;
  }
  return 0;
}