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

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

#define int long long int
#define double long long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define endl "\\n"

const int N = 2e5 + 5;
int a[N], vis[N];

int32_t main() {
  IOS;
  int n, k;
  cin >> n >> k;
  int cnt = 0;
  int town = 1;

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

  while (vis[town] == 0) {
    vis[town] = ++cnt;
    town = a[town];
    k--;
    if (k == 0) {
      cout << town << endl;
      return 0;
    }
  }
  // cout << cnt << " " << vis[town] << endl;
  int rem = cnt - vis[town] + 1;
  // cout << rem << endl;
  k %= rem;
  // cout << k << endl;
  for (int i = 1; i <= k; i++)
    town = a[town];
  cout << town << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define int long long int
#define double long long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define endl "\\n"

const int N = 2e5 + 5;
int a[N];

int32_t main() {
  IOS;
  int n, k;
  cin >> n >> k;
  int cnt = 0;
  int town = -1;
  // unordered_map<int, int>m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    // m[i] = a[i];
    // if (a[i] == 1)
    // 	town = i;
  }
  // if (town == -1)
  // {
  vector<int> v;
  multiset<int> s;
  s.insert(a[1]);
  v.pb(a[1]);
  int ele, idx;
  while (cnt < k) {
    town = a[v.back()];
    s.insert(town);
    if (s.count(town) > 1) {
      ele = town;
      break;
    }
    v.pb(town);
    cnt++;
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == ele) {
      idx = i;
      break;
    }
    // cout << x << " ";
  }
  // cout << ele << endl;
  // for (auto x : v)
  // 	cout << x << " ";
  // cout << endl;
  // cout << endl;
  // int idx = m[ele];
  int rem = k % (v.size() - idx - 1);
  int ans = v[rem];
  cout << ans << endl;
  //}
  // else
  // {
  // 	vector<int>v;
  // 	//set<int>s;
  // 	//s.insert(1);
  // 	v.pb(1);
  // 	while (cnt < k)
  // 	{
  // 		town = a[v.back()];
  // 		if (town == 1)
  // 			break;
  // 		v.pb(town);
  // 		cnt++;
  // 	}
  // 	for (auto x : v)
  // 		cout << x << " ";
  // 	cout << endl;
  // 	int rem = k % v.size();
  // 	int ans = v[rem];
  // 	cout << ans << endl;
  // }

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

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

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n), s, done(n + 1, -1);
  int c = 1, l = 0;

  loop(i, n) { cin >> a[i]; }

  int v = 1;
  while (done[v] == -1) {
    done[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - done[v];
  l = done[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 loop(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n), s, done(n + 1, -1);
  int c = 1, l = 0;

  loop(i, n) {
    cin >> a[i];
    done[i] = false;
  }

  int v = 1;
  while (done[v] == -1) {
    done[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - done[v];
  l = done[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 <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 2e5 + 5;

int n, a[maxn], vis[maxn] = {0}, k;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  deque<int> cycle;
  int cur = 1;
  while (!vis[cur]) {
    cycle.push_back(cur);
    vis[cur] = 1;
    cur = a[cur];
  }
  if (k < cycle.size())
    cout << cycle[k] << "\\n";
  else {
    while (!cycle.empty() && cycle.front() != a[cycle.back()]) {
      k--;
      cycle.pop_front();
    }
    cout << cycle[k % cycle.size()] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 2e5 + 5;

int n, a[maxn], vis[maxn] = {0}, k;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> cycle;
  int cur = 1;
  while (!vis[cur]) {
    cycle.push_back(cur);
    vis[cur] = 1;
    cur = a[cur];
  }
  if (k < cycle.size())
    cout << cycle[k] << "\\n";
  else {
    int starting = 1;
    while (starting != a[cycle.back()])
      starting++;
    starting--;
    k -= starting;
    int sz = (int)cycle.size() - starting;
    cout << cycle[k % sz] << "\\n";
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using boost::irange;
using boost::sort;
using boost::adaptors::reversed;

int main() {
  int n;
  long k;
  cin >> n >> k;
  vector<int> a(n + 1);
  unordered_map<int, bool> visited;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int now = 1;
  visited[now] = true;

  while (k--) {
    now = a[now];
    if (visited[now])
      break;
    visited[now] = true;
  }

  int t = 0;
  int tmp = now;

  if (k > 0) {
    while (k--) {
      now = a[now];
      t += 1;
      if (now == tmp)
        break;
    }
  }

  if (k > 0) {
    k %= t;
    while (k--)
      now = a[now];
  }

  cout << now << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using boost::irange;
using boost::sort;
using boost::adaptors::reversed;

int main() {
  int n;
  long k;
  cin >> n >> k;
  vector<int> a(n + 1);
  unordered_map<int, bool> visited;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int now = 1;
  visited[now] = true;

  while (k--) {
    now = a[now];
    if (visited[now])
      break;
    visited[now] = true;
  }

  int t = 0;
  int tmp = now;

  while (k--) {
    now = a[now];
    t += 1;
    if (now == tmp)
      break;
  }

  if (k > 0) {
    k %= t;
    while (k--)
      now = a[now];
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int N;
long long K;
std::vector<int> A;
std::map<int, int> city_to_j;
bool is_in_circle = false;

int main() {
  std::cin >> N >> K;
  A.assign(N, 0);
  int current_city = 0;
  int sta_j = 0;

  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    --A[i];
  }
  int div = 0;
  for (long long j = 0; j < K; j++) {
    city_to_j[current_city] = j;
    if (city_to_j.count(A[current_city]) == 1) {
      div = j - city_to_j[A[current_city]] + 1;
      sta_j = city_to_j[A[current_city]];
      is_in_circle = true;
      current_city = A[current_city];
      break;
    }
    current_city = A[current_city];
  }
  if (is_in_circle) {
    for (int k = 0; k < ((K - sta_j) % div); k++) {
      current_city = A[current_city];
    }
    std::cout << current_city + 1 << std::endl;
  } else {
    std::cout << current_city + 1 << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int N;
long long K;
std::vector<int> A;
std::map<int, int> city_to_j;

int main() {
  std::cin >> N >> K;
  A.assign(N, 0);
  int current_city = 0;
  int sta_j = 0;

  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    --A[i];
  }
  int div = 0;
  for (long long j = 0; j < K; j++) {
    city_to_j[current_city] = j;
    current_city = A[current_city];
    if (city_to_j.count(current_city) == 1) {
      div = j - city_to_j[current_city] + 1;
      sta_j = j;
      break;
    }
  }
  for (int k = 0; k < ((K - sta_j) % div) + 2; k++) {
    current_city = A[current_city];
  }
  std::cout << current_city + 1 << std::endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define int long long
#define ll long long
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define ld long double
#define eb emplace_back
#define pii pair<int, int>
#define ull unsigned long long
#define all(v) v.begin(), v.end()
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
#define fastio ios::sync_with_stdio(false)

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  // IO();
  fastio;
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<int> ar(200002);
  vector<int> C;
  vector<bool> vis(200002, 0);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  int pos = 1;
  int stp = 0;
  while (k && stp <= 100000000) {
    pos = ar[pos];
    ++stp;
    --k;
  }
  int st = pos;
  while (1) {
    vis[st] = true;
    C.pb(st);
    st = ar[st];
    if (vis[st])
      break;
  }
  cout << C[k % (C.size())] << endl;

  return 0;
}

Error: 0

Fixed submission:

// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define int long long
#define ll long long
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define ld long double
#define eb emplace_back
#define pii pair<int, int>
#define ull unsigned long long
#define all(v) v.begin(), v.end()
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
#define fastio ios::sync_with_stdio(false)

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  // IO();
  fastio;
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<int> ar(n);
  vector<int> C;
  vector<bool> vis(200002, 0);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  int pos = 1;
  int stp = 0;
  while (k && stp <= 100000000) {
    pos = ar[pos];
    ++stp;
    --k;
  }
  int st = pos;
  while (1) {
    vis[st] = true;
    C.pb(st);
    st = ar[st];
    if (vis[st])
      break;
  }
  cout << C[k % (C.size())] << endl;

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

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  int log_K = floor(log2(K));
  cerr << N << " " << log_K << endl;
  vector<vector<int>> next(log_K + 1, vector<int>(N));

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    cerr << i << " : " << tmp << endl;
    next.at(0).at(i) = tmp - 1;
  }

  for (int k = 0; k < log_K; k++) {
    for (int i = 0; i < N; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]];
        // cerr << k+1 << " " << i << " " << next[k+1][i] << endl;
      }
    }
  }

  int ans = 0;
  for (ll k = log_K; k >= 0; k--) {
    if ((K >> k) & 1) {
      ans = next[k][ans];
    }
  }
  cout << ans + 1 << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  int log_K = floor(log2(K));
  cerr << N << " " << log_K << endl;
  vector<vector<int>> next(log_K + 1, vector<int>(N));

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    cerr << i << " : " << tmp << endl;
    next.at(0).at(i) = tmp - 1;
  }

  for (int k = 0; k < log_K; k++) {
    for (int i = 0; i < N; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]];
        cerr << k + 1 << " " << i << " " << next[k + 1][i] << endl;
      }
    }
  }

  int ans = 0;
  for (ll k = log_K; k >= 0; k--) {
    if ((K >> k) & 1) {
      ans = next[k][ans];
    }
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> s;              // 訪れた頂点
  vector<ll> ord(n + 1, -1); // 数字を入れる
  ll c = 1;                  // 周期
  ll l = 0;                  // 例外部分の長さ
  {
    ll 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: TLE

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;

  vector<long long> tel(N);
  for (long long i = 0; i < N; i++) {
    cin >> tel.at(i);
  }

  long long a = 1;
  long long b;

  for (long long i = 0; i < K; i++) {
    if (i < K) {
      b = tel.at(a - 1);
      a = b;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

long long A[200005], C[200005];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long N, K;
  cin >> N >> K;
  for (long long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  long long curr = 1, move = 1, len, moves;
  if (K <= N) {
    while (K--)
      curr = A[curr];
    cout << curr << '\\n';
    return 0;
  }
  vector<long long> cycle;
  C[curr] = move;
  while (1) {
    move++;
    curr = A[curr];
    if (C[curr] > 0) {
      len = move - C[curr];
      break;
    }
    C[curr] = move;
  }
  long long shift = C[curr] - C[1];
  for (long long i = 0; i < len; ++i) {
    cycle.push_back(curr);
    curr = A[curr];
  }
  moves = (K - shift) % len;
  long long town = cycle[moves];
  cout << town << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long A[200005], C[200005];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long N, K;
  cin >> N >> K;
  for (long long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  long long curr = 1, move = 1, len, moves;
  vector<long long> cycle;
  C[curr] = move;
  while (1) {
    move++;
    curr = A[curr];
    if (C[curr] > 0) {
      len = move - C[curr];
      break;
    }
    C[curr] = move;
  }
  long long shift = C[curr] - C[1];
  for (long long i = 0; i < len; ++i) {
    cycle.push_back(curr);
    curr = A[curr];
  }
  moves = (K - shift) % len;
  long long town = cycle[moves];
  cout << town << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  lint N, K;
  cin >> N >> K;

  vector<lint> A(N);
  for (auto &elem : A)
    cin >> elem;
  for (auto &elem : A)
    elem--;

  if (K <= N + N) {
    lint idx = 0;
    for (lint i = 0; i < K; i++) {
      idx = A[idx];
    }
    cout << ++idx << endl;
    return 0;
  }

  vector<lint> v;
  map<lint, lint> prev;
  lint idx = 0, loop;
  for (lint i = 0;; i++) {
    if (prev.count(idx)) {
      loop = i - prev[idx];
      break;
    }
    prev[idx] = i;
    v.push_back(idx);
    idx = A[idx];
  }
  K -= (v.size() - loop);
  vector<lint> vec(v.end() - loop, v.end());
  cout << ++vec[K % vec.size()] << endl;
}

Error: 0

Fixed submission:

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

int main() {
  lint N, K;
  cin >> N >> K;

  vector<lint> A(N);
  for (auto &elem : A)
    cin >> elem;
  for (auto &elem : A)
    elem--;

  if (K <= N + N) {
    lint idx = 0;
    for (lint i = 0; i < K; i++) {
      idx = A[idx];
    }
    cout << ++idx << endl;
    return 0;
  }

  vector<lint> v;
  map<lint, lint> prev;
  lint idx = 0, loop;
  for (lint i = 0;; i++) {
    if (prev.count(idx)) {
      loop = i - prev[idx];
      break;
    }
    prev[idx] = i;
    v.push_back(idx);
    idx = A[idx];
  }
  K -= (v.size() - loop);
  vector<lint> vec(v.end() - loop, v.end());
  cout << ++vec[K % v.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error


// Problem :
// 			D - Teleporter
// 			Editorial
//
// Contest : AtCoder - AtCoder Beginner Contest 167
// URL : https://atcoder.jp/contests/abc167/tasks/abc167_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀
⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀
⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀
⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀
⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀
⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀
⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀
⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀
⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀
⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀
⣿⣿⣧⣀⣿.........⣀⣰⣏⣘⣆⣀⠀
*/
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define l00p(i, a, b) for (int i = a; i < (b); i++)
#define loop(i, a) for (int i = 0; i < (a); i++)
#define rep1(i, a, b) for (int i = (b)-1; i >= a; i--)
#define re(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
// #define MOD 998244353

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ld pii = 3.14159265359;
const int MOD = 1000000007;
const char nl = '\\n';
const ll INF = 1e18;
const int MX = 100001; // check the limits, dummy

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vi a(n), place;
  loop(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> vis(n);
  int i = 0, len = 0;
  while (!vis[i]) {
    vis[i] = true;
    i = a[i];
    len++;
    if (len == k) {
      cout << i + 1 << nl;
      return 0;
    }
  }
  int ssz = 1, cpy = i;
  vi ans;
  ans.pb(i);
  i = a[i];
  ans.pb(i);
  while (i != cpy) {
    ssz++;
    i = a[i];
    ans.pb(i);
  }
  int val = (k - len) % ssz;
  cout << ans[val] + 1 << nl;
  return 0;
}
// read the question correctly (ll vs int)
// template by bqi343

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void telep(vector<long long> v, long long k, long long times, long long pos) {
  if (times == k) {
    cout << pos;
    return;
  } else {
    times++;
    telep(v, k, times, v[pos - 1]);
    if (pos == v[pos - 1])
      ;
    {
      cout << pos;
      return;
    }
  }
}
int main() {
  long long n, k, t = 1;
  cin >> n >> k;
  vector<long long> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  telep(v, k, t, v[0]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  long long N;
  long long K;
  std::cin >> N >> K;
  vector<long long> A(N, 0);
  vector<long long> flags(N, 0);
  vector<long long> place(N, 0);
  for (long long i = 0; i < N; i++) {
    std::cin >> A.at(i);
  }
  long long j = 0;
  flags[0] = 1;
  place[0] = 1;
  for (long long i = 1; i <= K; i++) {
    long long s = A[j];
    if (flags[s - 1] == 0) {
      flags[s - 1] = i + 1;
      j = s - 1;
      place[i] = s;
      if (i == K) {
        cout << s << endl;
        break;
      }
    } else {
      place[i] = s;
      long long start = flags[s - 1] - 1;
      long long loop = i + 1 - flags[s - 1];
      long long col = flags[s - 1] + (K - start - 1) % loop;
      cout << place[col] << endl;
      break;
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main(void) {
  long long N, K;
  vector<int> A(N, 0);
  vector<int> flags(N, 0);
  vector<int> place(N, 0);
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    std::cin >> A.at(i);
  }
  int j = 0;
  flags[0] = 1;
  place[0] = 1;
  for (int i = 1; i < N; i++) {
    int s = A[j];
    if (flags[s - 1] == 0) {
      flags[s - 1] = i + 1;
      j = s - 1;
      place[i] = s;
      continue;
    } else {
      place[i] = s;
      int start = flags[s - 1] - 1;
      int loop = i + 1 - flags[s - 1];
      int col = flags[s - 1] + (K - start - 1) % loop;
      cout << place[col] << endl;
      break;
    }
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define isYES(x) printf("%s\\n", (x) ? "YES" : "NO")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define isYes(x) printf("%s\\n", (x) ? "Yes" : "No")
#define isIn(x, y, h, w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
// using ll = long long;
using P = pair<int, int>;

ostream &operator<<(ostream &os, const P &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    os << (i ? "," : "[") << v[i];
  os << "]";
  return os;
}

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  int n, k, a[200010], nxt[100][200010];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    nxt[0][i] = a[i] - 1;
  for (int i = 1; i < 100; i++) {
    for (int j = 0; j < n; j++)
      nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
  }
  int pos = 0;
  for (int i = 0; i < 62; i++) {
    if (k >> i & 1)
      pos = nxt[i][pos];
  }
  cout << pos + 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  vector<int> cycle;
  cycle.push_back(0);
  int pos = a[0];
  while (pos != 0) {
    cycle.push_back(pos);
    pos = a[pos];
  }
  cout << cycle[(k) % cycle.size()] + 1 << endl;
}