alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<bool> seen(N + 1);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  int beforeLoop = 0;
  int now = 0;
  while (!seen[now]) {
    seen[now] = true;
    now = A[now];
    beforeLoop++;
  }
  int loopStart = now;
  int loopLength = 0;
  do {
    seen[now] = true;
    now = A[now];
    loopLength++;
  } while (loopLength <= N and now != loopStart);
  if (K < beforeLoop) {
    int ans = 0;
    for (int i = 0; i < K; i++) {
      ans = A[ans];
    }
    cout << ans + 1;
    return 0;
  }
  K -= beforeLoop;
  K %= loopLength;
  int pos = loopStart;

  for (int i = 0; i < K; i++) {
    pos = A[pos];
  }
  cout << pos + 1;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<bool> seen(N);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  int beforeLoop = 0;
  int now = 0;
  seen[0] = true;
  do {
    seen[now] = true;
    now = A[now];

    beforeLoop++;
  } while (beforeLoop < K and !seen[now]);
  int loopStart = now;
  int loopLength = 0;
  do {
    seen[now] = true;
    now = A[now];
    loopLength++;
  } while (now != loopStart);
  K -= beforeLoop;
  K %= loopLength;
  int pos = 0;
  for (int i = 0; i < beforeLoop; i++) {
    pos = A[pos];
  }

  for (int i = 0; i < K; i++) {
    pos = A[pos];
  }
  cout << pos + 1;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define len(x) (int)(x.size())
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define MAXN 200010
int nxt[MAXN];
bitset<MAXN> visited;

int32_t main() {
  int n, k;
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &nxt[i]);

  int ans = 1;
  visited[1] = true;
  while (k--) {
    if (visited[nxt[ans]]) {
      ++k;
      break;
    }
    ans = nxt[ans];
    visited[ans] = true;
  }
  if (k <= 0) {
    printf("%lld\\n", ans);
    return 0;
  }
  visited.reset();
  int tam = 1;
  int x = ans;
  visited[x] = true;
  while (not visited[nxt[x]]) {
    x = nxt[x];
    visited[x] = true;
    ++tam;
  }
  k %= tam;
  while (k--)
    ans = nxt[ans];
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define len(x) (int)(x.size())
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define MAXN 200010
int nxt[MAXN];
bitset<MAXN> visited;

int32_t main() {
  ios::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    nxt[i] = x;
  }
  int ans = 1;
  visited[1] = true;
  while (k--) {
    if (visited[nxt[ans]]) {
      ++k;
      break;
    }
    ans = nxt[ans];
    visited[ans] = true;
  }
  visited.reset();
  int tam = 1;
  int x = ans;
  visited[x] = true;
  while (not visited[nxt[x]]) {
    x = nxt[x];
    visited[x] = true;
    ++tam;
  }
  k = k % tam;
  while (k--) {
    ans = nxt[ans];
  }
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 4e6 + 5;
ll to[maxn], pos[maxn], vis[maxn], mp[maxn];
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[i];
  }
  pos[1] = 1;
  vis[1] = 1;
  mp[1] = 1;
  ll now = to[1], cnt = 1, dis = 0, l = 0;
  while (!vis[now]) {
    vis[now] = ++cnt;
    mp[cnt] = now;
    now = to[now];
    if (vis[now]) {
      dis = cnt + 1 - vis[now];
      l = vis[now];
    }
  }
  k = k + 1;
  if (l == 0) {
    cout << "1" << endl;
    return 0;
  }
  if (k < l) {
    cout << mp[k] << endl;
  } else {
    k = k - l;
    k %= dis;
    k += l;
    cout << mp[k] << endl;
  }
}
/*
3 4
2 2 2
*/


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 4e6 + 5;
ll to[maxn], pos[maxn], vis[maxn], mp[maxn];
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[i];
  }
  pos[1] = 1;
  vis[1] = 1;
  mp[1] = 1;
  ll now = to[1], cnt = 1, dis = 0, l = 0;
  while (!vis[now]) {
    vis[now] = ++cnt;
    mp[cnt] = now;
    now = to[now];
    if (vis[now]) {
      dis = cnt + 1 - vis[now];
      l = vis[now];
    }
  }
  k = k + 1;
  if (l == 0)
    cout << "1" << endl;
  if (k < l) {
    cout << mp[k] << endl;
  } else {
    k = k - l;
    k %= dis;
    k += l;
    cout << mp[k] << endl;
  }
}
/*
3 4
2 2 2
*/
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;

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

  vector<int> s;
  vector<int> ord(N + 1, -1);
  int v = 1;
  int c, l;

  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: -8

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
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];
  }

  vector<int> s;
  vector<int> ord(N + 1, -1);
  int v = -1;
  int c, l;

  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;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
static const int MAX = 200005;

int N;
ll K;
int A[MAX], B[MAX], C[MAX];

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

  int x = A[0];
  B[0] = 1;
  C[0] = 0;
  for (int i = 1; B[x] == 0; i++) {
    B[x] = 1;
    C[x] = i;
    V.push_back(x);
    x = A[x];
  }
  int n = V.size() + 1;
  int m = n - C[x];
  ll a;
  if (n > K)
    a = K;
  else {
    a = ((K - C[x]) % m) + C[x];
  }
  if (a == 0)
    cout << 1 << endl;
  else
    cout << V[a - 1] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
static const int MAX = 200005;

int N;
ll K;
int A[MAX], B[MAX], C[MAX];

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

  int x = A[0];
  B[0] = 1;
  C[0] = 0;
  for (int i = 1; B[x] == 0; i++) {
    B[x] = 1;
    C[x] = i;
    V.push_back(x);
    x = A[x];
  }
  int n = V.size() + 1;
  int m = n - C[x];
  ll a;
  if (n > K)
    a = K;
  else {
    a = ((K - C[x]) % m) + C[x];
  }
  if (a == -1)
    cout << 1 << endl;
  else
    cout << V[a - 1] + 1 << endl;

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

#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 200005;
int n, A[maxn], p = 1, len, idx[maxn];
LL K;
inline int read() {
  int ret = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret * f;
}
int main() {
  n = read();
  scanf("%lld", &K);
  for (int i = 1; i <= n; i++)
    A[i] = read();
  while (!idx[p] && K) {
    idx[p] = ++len;
    K--;
    p = A[p];
  }
  K %= (len + 1 - idx[p]);
  for (int i = 1; i <= K; i++)
    p = A[p];
  printf("%d\\n", p);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
long long n, k;
long long a[200005];
int main() {
  int i, j, k, m;
  cin >> n >> k;

  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  m = a[0];
  if (k == 1)
    m = a[0];
  for (j = 0; j < k - 1; j++) {
    m = a[m - 1];
  }

  cout << m << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define ll long int
#define ull unsigned long long int

#define I_read() freopen("input.tex", "r", stdin);

#define O_write() freopen("output.txt", "w", stdout);

int main() {

  //	ios_base::sync_with_stdio(false);
  //	cin.tie(0);

  //	I_read();
  //	O_write();

  ll a;
  ull b, temp, rem = 0, numt = 0;

  cin >> a >> b;

  ll town[a], trv[a], ptow = 0;
  bool pass[a];

  for (ll i = 0; i < a; i++) {
    cin >> town[i];
    pass[i] = 0;
    //	cout<<pass[i]<<" ";
  }
  cout << endl;

  while (pass[ptow] != 1 && numt < b) {
    trv[ptow] = numt;
    pass[ptow] = 1;
    //	cout<<ptow+1<<"->";
    ptow = town[ptow] - 1;
    numt++;
  }

  //	cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl;

  if (numt != b) {
    //	cout<<"not-compleated-";
    temp = numt - trv[ptow];

    rem = (b - numt) % temp;

    //	cout<<"temp="<<temp<<"rem="<<rem<<"->";
  }

  while (rem > 0) {
    // trv[ptow] =numt;
    // pass[ptow] = 1;
    ptow = town[ptow] - 1;
    rem--;
  }
  cout << ptow + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long int
#define ull unsigned long long int

#define I_read() freopen("input.tex", "r", stdin);

#define O_write() freopen("output.txt", "w", stdout);

int main() {

  //	ios_base::sync_with_stdio(false);
  //	cin.tie(0);

  //	I_read();
  //	O_write();

  ll a;
  ull b, temp, rem, numt = 0;

  cin >> a >> b;

  ll town[a], trv[a], ptow = 0;
  bool pass[a];

  for (ll i = 0; i < a; i++) {
    cin >> town[i];
    pass[i] = 0;
    //	cout<<pass[i]<<" ";
  }
  cout << endl;

  while (pass[ptow] != 1 && numt < b) {
    trv[ptow] = numt;
    pass[ptow] = 1;
    //	cout<<ptow+1<<"->";
    ptow = town[ptow] - 1;
    numt++;
  }

  //	cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl;

  if (numt != b) {
    //	cout<<"not-compleated-";
    temp = numt - trv[ptow];

    rem = (b - numt) % temp;

    //	cout<<"temp="<<temp<<"rem="<<rem<<"->";
  }

  while (rem > 0) {
    // trv[ptow] =numt;
    // pass[ptow] = 1;
    ptow = town[ptow] - 1;
    rem--;
  }
  cout << ptow + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using ll = long long;
using namespace std;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> as(n);
  rep(i, n) { cin >> as[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 = as[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: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using ll = long long;
using namespace std;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> as(n);
  rep(i, n) { cin >> as[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 = as[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;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  int arr[n + 1];
  int i;
  vector<int> vi;
  unordered_set<int> s1;
  for (i = 0; i < n; i++) {
    cin >> arr[i + 1];
  }
  int currTown = 1;
  int flag = 0;
  vi.push_back(currTown);
  s1.insert(currTown);

  for (i = 0; i < n; i++) {
    currTown = arr[currTown];
    k -= 1;
    if (k == 0) {
      cout << currTown;
      flag = 1;
      break;
    }
    if (s1.find(currTown) != s1.end()) {
      break;
    } else {
      vi.push_back(currTown);
      s1.insert(currTown);
    }
  }

  if (flag != 1) {
    auto it = find(vi.begin(), vi.end(), currTown);
    int ind = it - vi.begin();
    currTown = vi[k % (vi.size() - ind) + ind];
    cout << currTown;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  int arr[n + 1];
  int i;
  vector<int> vi;
  for (i = 0; i < n; i++) {
    cin >> arr[i + 1];
  }
  int currTown = 1;
  int flag = 0;
  vi.push_back(currTown);
  for (i = 0; i < n; i++) {
    currTown = arr[currTown];
    k -= 1;
    if (k == 0) {
      cout << currTown;
      flag = 1;
      break;
    }
    if (find(vi.begin(), vi.end(), currTown) != vi.end()) {
      break;
    } else {
      vi.push_back(currTown);
    }
  }

  if (flag != 1) {
    auto it = find(vi.begin(), vi.end(), currTown);
    int ind = it - vi.begin();
    currTown = vi[k % (vi.size() - ind) + ind];

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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios_base ::sync_with_stdio(false);
  int n = 0;
  long long k = 0;
  cin >> n >> k;
  vector<int> p(n + 2, 0);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> used(n + 2, -1);
  used[0] = 0;
  int cp = 0;
  int ct = 0;
  while (k > 0) {
    k--;
    ct++;
    cp = p[cp];
    if (used[cp] != -1) {
      break;
    }
    used[cp] = ct;
  }
  if (k != 0)
    k %= (ct - used[cp]);
  while (k > 0) {
    k--;
    cp = p[cp];
  }
  cout << cp + 1;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios_base ::sync_with_stdio(false);
  int n = 0;
  long long k = 0;
  cin >> n >> k;
  vector<int> p(n + 2, 0);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> used(n + 2, -1);
  used[0] = 0;
  int cp = 0;
  int ct = 0;
  while (k > 0) {
    k--;
    ct++;
    cp = p[cp];
    if (used[cp] != -1) {
      break;
    }
    used[cp] = ct;
  }
  k %= (ct - used[cp]);
  while (k > 0) {
    k--;
    cp = p[cp];
  }
  cout << cp + 1;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <unordered_map>
#include <vector>
#define ll long long int
using namespace std;

unordered_map<ll, ll> mp_itr;
vector<ll> destination(200005);
int main() {

  ll n;
  ll k;
  cin >> n >> k;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
  }

  ll h = 0, l = 0;
  ll itr = 1;
  ll start = 0;
  destination[0] = 0;
  mp_itr[0] = 0;
  for (ll i = 0; i <= n; i++) {

    destination[itr] = arr[start];
    if (mp_itr.count(destination[itr])) {
      l = mp_itr[destination[itr]];
      h = itr;
      break;
    } else {
      mp_itr[destination[itr]] = itr;
    }
    start = arr[start];
    itr++;
  }

  if (k < h) {
    cout << destination[k] + 1;
  } else {
    k = k - l;
    ll loop = h - l;
    k = k % loop;
    cout << destination[l + k] + 1;
  }

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <iostream>
#include <unordered_map>
#include <vector>
#define ll long long int
using namespace std;

int main() {

  ll n;
  ll k;
  cin >> n >> k;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
  }

  unordered_map<ll, ll> mp_itr;
  vector<ll> destination(n + 1);
  ll h = 0, l = 0;
  ll itr = 1;
  ll start = 0;
  destination[0] = 0;
  mp_itr[0] = 0;
  for (ll i = 0; i <= n; i++) {

    destination[itr] = arr[start];
    if (mp_itr.count(destination[itr])) {
      l = mp_itr[destination[itr]];
      h = itr;
    } else {
      mp_itr[destination[itr]] = itr;
    }
    start = arr[start];
    itr++;
  }

  if (k < h) {
    cout << destination[k] + 1;
  } else {
    k = k - l;
    ll loop = h - l;
    k = k % loop;
    cout << destination[l + k] + 1;
  }

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

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

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  int roop = 0;
  ll from = 1;
  vector<int> teleporter(1, 1);
  set<int> list;
  list.insert(1);
  bool one = true;
  while (true) {
    from = A[from];
    roop++;
    if (list.count(from)) {
      if (from != 1)
        one = false;
      break;
    }
    teleporter.push_back(from);
    list.insert(from);
  }
  if (one)
    cout << teleporter[K % roop] << endl;
  else {
    ll start;
    for (int i = 0; i < teleporter.size(); i++) {
      if (teleporter[i] == from) {
        start = i;
        break;
      }
    }
    vector<int> teleporter_new(1, from);
    ll reserve = from;
    int roop_new = 0;
    while (true) {
      from = A[from];
      roop_new++;
      if (from == reserve)
        break;
      teleporter_new.push_back(from);
    }
    if (K < start)
      cout << teleporter[K] << endl;
    else
      cout << teleporter_new[(K - start) % roop_new] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  int roop = 0;
  ll from = 1;
  vector<int> teleporter(1, 1);
  set<int> list;
  list.insert(1);
  bool one = true;
  while (true) {
    from = A[from];
    roop++;
    if (list.count(from)) {
      if (from != 1)
        one = false;
      break;
    }
    teleporter.push_back(from);
    list.insert(from);
  }
  if (one)
    cout << teleporter[K % roop] << endl;
  else {
    ll start;
    for (int i = 0; i < teleporter.size(); i++) {
      if (teleporter[i] == from) {
        start = i - 1;
        break;
      }
    }
    cout << teleporter[(K - start) % (roop - start)] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 100;
int A[maxn];
int n, m;
LL k;
map<int, int> mp;
vector<int> ans;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int st = 1;
  int cnt = 1;
  while (1) {
    ans.push_back(st);
    if (mp[st])
      break;
    mp[st] = cnt;
    st = A[st];
    cnt++;
  }
  int d = cnt - mp[st];
  int val = (k - 1LL * (mp[st] - 1)) % d;
  k++;
  if (k < mp[st])
    cout << ans[k - 1] << endl;
  else {
    while (val) {
      val--;
      st = A[st];
    }
    cout << st << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
int A[maxn];
int n, m;
LL k;
map<int, int> mp;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int st = 1;
  int cnt = 1;
  while (1) {
    if (mp[st])
      break;
    mp[st] = cnt;
    st = A[st];
    cnt++;
  }
  LL d = cnt - mp[st];
  LL val = (k - 1LL * (mp[st] - 1)) % d;
  while (val) {
    val--;
    st = A[st];
  }
  cout << st << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

bool vis[200009];
int main() {
  long long n, k;
  cin >> n >> k;

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

  vector<long long> path;
  vector<long long> pos(n + 1);
  int i = 0;
  while (1) {

    if (vis[i] == true)
      break;
    pos[i] = path.size();
    path.push_back(i);
    vis[i] = true;
    i = a[i];
  }

  long long cyclestart = pos[i];
  long long cyclelen = (int)path.size() - cyclestart;

  if (k <= path.size() - 1) {
    cout << path[k] + 1 << endl;
    return 0;
  }

  else {

    k -= cyclestart;
    k %= cyclelen;
    cout << path[cyclestart + k] + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, k, town;
  cin >> n >> k;
  int arr[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  town = arr[1];
  // printf("%d\\n",town);
  for (int j = 0; j < k - 1; j++) {
    town = arr[town];
    // printf("%d\\n",town);
  }
  cout << town << endl;
  return 0;
}