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

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> array(N + 1);
  array[0] = 0;
  vector<int> reach(N + 1);
  vector<long> loop(0);
  for (int i = 0; i < N; i++)
    cin >> array[i + 1];
  long place = 1;
  long count = 0;
  long mark = 0;
  long j = 0;
  while (reach[place] < 3) {
    reach[place]++;
    if (reach[place] == 2 && count == 0)
      mark = j - 1;
    if (reach[place] == 2) {
      loop.push_back(place);
      count++;
    }

    place = array[place];
    j++;
    if (j == K) {
      cout << place << endl;
      return 0;
    }
  }
  long ans = (K - mark) % count - 1;
  if (ans == -1)
    ans = count - 1;
  cout << loop[ans] << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> array(N + 1);
  array[0] = 0;
  vector<int> reach(N + 1);
  vector<long> loop(0);
  for (int i = 0; i < N; i++)
    cin >> array[i + 1];
  long place = 1;
  long count = 0;
  long mark = 0;
  long j = 0;
  while (reach[place] < 3) {
    reach[place]++;
    if (reach[place] == 2 && count == 0)
      mark = j - 1;
    if (reach[place] == 2) {
      loop.push_back(place);
      count++;
    }

    place = array[place];
    j++;
  }
  long ans = (K - mark) % count - 1;
  if (ans == -1)
    ans = count - 1;
  cout << loop[ans] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  ll n;
  ll k;
  cin >> n >> k;
  int a[n];
  rep(m, n) {
    int b;
    cin >> b;
    b--;
    a[m] = b;
  }
  int i = 0;
  int key;
  vector<int> path;
  path.push_back(0);
  vector<bool> memo(n, false);
  memo[0] = true;
  while (1) {
    if (memo[a[i]]) {
      key = a[i];
      break;
    }
    memo[a[i]] = true;
    path.push_back(a[i]);
    i = a[i];
  }
  int start, loop;
  for (int j = 0; j < path.size(); j++) {
    if (path[j] == key) {
      start = j;
      loop = path.size() - j;
    }
  }
  if (k < start) {
    cout << path[k] + 1 << endl;
  } else {
    k -= start;
    k %= loop;
    cout << path[start + k] + 1 << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  ll n;
  ll k;
  cin >> n >> k;
  int a[n];
  rep(m, n) {
    int b;
    cin >> b;
    b--;
    a[m] = b;
  }
  bool memo[n];
  int i = 0;
  int key;
  vector<int> path;
  path.push_back(0);
  memo[0] = true;
  while (1) {
    if (memo[a[i]]) {
      key = a[i];
      break;
    }
    memo[a[i]] = true;
    path.push_back(a[i]);
    i = a[i];
  }
  int start, loop;
  for (int j = 0; j < path.size(); j++) {
    if (path[j] == key) {
      start = j;
      loop = path.size() - j;
    }
  }
  cout << "start = " << start << endl;
  cout << "loop = " << loop << endl;
  if (k < start) {
    cout << path[k] + 1 << endl;
  } else {
    k -= start;
    k %= loop;
    cout << path[start + k] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define debug(s) cerr << #s << " = " << s << '\\n'
#define all(v) (v).begin(), (v).end()
#define mem(a, val) memset(a, val, sizeof a)

#define ff first
#define ss second
typedef long long ll;

// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;

const int N = 200010;
int n, arr[N], vis[N];
ll k;
vector<int> v;

int visit(int idx) {
  if (vis[idx] == 1)
    return idx;
  vis[idx]++;
  v.push_back(idx);
  return visit(arr[idx]);
}

int main() {
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &arr[i]);
  int id = visit(arr[1]);
  // debug(id);
  int pos = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    if (v[i] == id) {
      pos = i;
      break;
    }
  }
  if (k - 1 < pos) {
    printf("%d\\n", v[k - 1]);
    return 0;
  }
  k -= pos;
  if (pos != 0)
    v.erase(v.begin(), v.begin() + pos);
  // for (auto p: v) cout << p << " ";
  // cout << endl;
  int sz = v.size();
  k %= sz;
  if (k == 0)
    k = sz;
  printf("%d\\n", v[k - 1]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define debug(s) cerr << #s << " = " << s << '\\n'
#define all(v) (v).begin(), (v).end()
#define mem(a, val) memset(a, val, sizeof a)

#define ff first
#define ss second
typedef long long ll;

// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;

const int N = 200010;
int n, arr[N], vis[N];
ll k;
vector<int> v;

int visit(int idx) {
  if (vis[idx] == 1)
    return idx;
  vis[idx]++;
  v.push_back(idx);
  return visit(arr[idx]);
}

int main() {
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &arr[i]);
  int id = visit(arr[1]);
  int pos = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    if (v[i] == id) {
      pos = i;
      break;
    }
  }
  if (k - 1 < pos) {
    printf("%d\\n", v[k - 1]);
  }
  k -= pos;
  v.erase(v.begin(), v.begin() + pos);
  int sz = v.size();
  k %= sz;
  if (k == 0)
    k = sz;
  printf("%d\\n", v[k - 1]);
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  std::set<int> s;
  s.insert(0);
  int i = 0;
  int v = 0;
  int loop_start, loop_end;
  map<int, int> m;
  int loop_end_i;
  while (1) {
    m.insert(make_pair(v, i));
    if (s.find(a[v] - 1) != s.end()) {
      loop_start = a[v] - 1;
      loop_end = v;
      loop_end_i = i;
      break;
    }
    v = a[v] - 1;
    s.insert(v);
    i++;
  }

  int loop_start_i = m.at(loop_start);
  int loop_len = loop_end_i - loop_start_i + 1;
  long long h = (k - loop_start_i);
  if (h < 0) {
    int v = 0;
    for (int i = 0; i < k; i++) {
      v = a[v] - 1;
    }
    cout << v + 1 << endl;
  } else {
    if (loop_len != 0)
      h %= loop_len;

    int v = loop_start;
    for (int i = 0; i < h; i++) {
      v = a[v] - 1;
    }
    cout << v + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  std::set<int> s;
  s.insert(0);
  int i = 0;
  int v = 0;
  int loop_start, loop_end;
  while (1) {
    if (s.find(a[v] - 1) != s.end()) {
      loop_start = a[v] - 1;
      loop_end = v;
      break;
    }
    v = a[v] - 1;
    s.insert(v);
    i++;
  }
  i++;

  int loop_start_i = -1;
  int loop_end_i = -1;
  int loop_len = 0;
  int count = 0;
  v = 0;
  while (1) {
    if (v == loop_start) {
      loop_start_i = count;
    } else if (v == loop_end) {
      loop_end_i = count;
      break;
    }
    v = a[v] - 1;
    count++;
  }
  loop_len = loop_end_i - loop_start_i + 1;
  long long h = (k - loop_start_i);
  if (h < 0) {
    int v = 0;
    for (int i = 0; i < k; i++) {
      v = a[v] - 1;
    }
    cout << v + 1 << endl;
  } else {
    if (loop_len != 0)
      h %= loop_len;

    int v = loop_start;
    for (int i = 0; i < h; i++) {
      v = a[v] - 1;
    }
    cout << v + 1 << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define ff first
#define ss second
#define pb push_back
#define quicker                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;

ll n, k;

int main() {
  quicker cin >> n >> k;
  vector<int> A(n + 1), tr, tot;
  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int i = 1;
  // visited[1] = true;
  ll cylen = 0, tlen = 0;
  while (!visited[i]) {
    visited[i] = true;
    tot.pb(i);
    ++tlen;
    i = A[i];
  }
  for (int j = 0; j <= n; j++)
    visited[j] = false;
  while (!visited[i]) {
    visited[i] = true;
    tr.pb(i);
    ++cylen;
    i = A[i];
  }
  if (k <= (tlen - cylen)) {
    cout << tot[k];
    return 0;
  }
  k -= (tlen - cylen);
  k %= cylen;
  cout << tr[k];
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ff first
#define ss second
#define pb push_back
#define quicker                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;

ll n, k;

int main() {
  quicker cin >> n >> k;
  vector<int> A(n + 1), tr;
  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int i = 1;
  // visited[1] = true;
  ll cylen = 0, tlen = 0;
  while (!visited[i]) {
    visited[i] = true;
    ++tlen;
    i = A[i];
  }
  for (int j = 0; j <= n; j++)
    visited[j] = false;
  while (!visited[i]) {
    visited[i] = true;
    tr.pb(i);
    ++cylen;
    i = A[i];
  }
  k -= (tlen - cylen);
  k %= cylen;
  cout << tr[k];
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  // 行先表
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  // 訪問済みフラグ兼訪問時のワープ済み回数記録(-1:未訪問)
  vector<int> visited(n, -1);

  // 現在位置とワープ済み回数
  int pos = 0;
  int turn = 0;

  vector<int> history;
  while (visited.at(pos) == -1) {
    history.push_back(pos + 1);
    visited.at(pos) = turn++;
    pos = a.at(pos);
  }

  if (k < history.size())
    cout << history[k] << endl;
  else {
    int64_t loop_start = visited.at(pos);
    k -= loop_start;
    int64_t loop_length = turn - loop_start;
    k %= loop_length;
    cout << history[k + loop_start] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

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

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  vector<bool> visited(n);
  for (int i = 0; i < n; i++)
    visited.at(i) = false;
  vector<int> history(n * 2);

  // 現在位置とワープ回数
  int pos = 0;
  int turn = 0;

  // 1番目の町はtrue;
  visited.at(0) = true;
  history.at(0) = pos;
  while (1) {
    pos = a.at(pos) - 1;
    turn++;
    history.at(turn) = pos;

    if (visited.at(pos))
      break;
    else
      visited.at(pos) = true;
  }

  // ループの長さとループが始まったワープ回数を求める
  int loop_len = 0;
  int loop_start = 0;
  int last_pos = history.at(turn);

  for (int j = turn - 1; j >= 0; --j) {
    loop_len++;
    if (last_pos == history.at(j)) {
      loop_start = j;
      break;
    }
  }

  vector<int> loop(loop_len);
  for (int i = 0; i < loop_len; i++)
    loop.at(i) = history.at(loop_start + i);

  cout << loop.at((k - loop_start) % loop_len) + 1 << endl;

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

#include <bits/stdc++.h>
#include <set>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> tr(1);
  set<int> s;
  s.insert(0);
  set<int>::iterator ite;
  ;
  int last = 0, start = 0;
  while (true) {
    int to = A[last];
    last = to;
    ite = s.find(to);
    if (ite != s.end())
      break;
    tr.push_back(to);
    s.insert(to);
  }
  rep(i, N) {
    if (tr[i] == last) {
      start = i;
      break;
    }
  }
  if (K < start)
    cout << tr[K] + 1 << endl;
  else {
    K -= start;
    K %= tr.size() - start;
    cout << tr[K + start] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> tr(1);
  int last = 0, start = 0;
  while (true) {
    int to = A[last];
    last = to;
    if (binary_search(tr.begin(), tr.end(), to))
      break;
    tr.push_back(to);
  }
  rep(i, N) {
    if (tr[i] == last) {
      start = i;
      break;
    }
  }
  if (K < start)
    cout << tr[K] + 1 << endl;
  else {
    K -= start;
    K %= tr.size() - start;
    cout << tr[K + start] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  vector<int> flag(N + 1, -1);
  vector<int> B;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int tugi = 1;
  flag[1] = 0;
  int cnt1 = -1;
  int cnt2 = -1;
  for (int i = 1; i <= N + 1; i++) {
    B.push_back(tugi);
    if (flag[A[tugi]] != -1) {
      cnt1 = flag[A[tugi]];
      cnt2 = (i - flag[A[tugi]]);
      break;
    }
    flag[A[tugi]] = i;
    tugi = A[tugi];
  }
  if (cnt1 > K) {
    cout << B[K] << endl;
  } else {
    int X = (K - cnt1) % cnt2;
    cout << B[cnt1 + X] << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  vector<int> flag(N + 1, -1);
  vector<int> B;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int tugi = 1;
  flag[1] = 0;
  int cnt1 = -1;
  int cnt2 = -1;
  for (int i = 1; i <= N + 1; i++) {
    if (flag[A[tugi]] != -1) {
      cnt1 = flag[A[tugi]];
      cnt2 = (i - flag[A[tugi]]);
      break;
    }
    flag[A[tugi]] = i;
    B.push_back(tugi);
    tugi = A[tugi];
  }
  if (cnt1 > K) {
    cout << B[K] << endl;
  } else {
    int X = (K - cnt1) % cnt2;
    cout << B[cnt1 + X] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n;
  ll 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 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: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a;
  for (int i = 0; i < n; ++i)
    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;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100001], val[2 * 100001];
bool visited[2 * 100001];

int main() {

  int n;
  long long int k;

  cin >> n >> k;

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

  int next = 1;
  while (k > 0) {
    if (visited[next] == true) {
      int sub_next = a[next], sum = 1;
      while (sub_next != next) {
        sum++;
        sub_next = a[sub_next];
      }
      k = k % sum;
      for (int i = 0; i < k; i++) {
        next = a[next];
      }
      cout << next << endl;
      ;
      return 0;
    }
    visited[next] = true;
    next = a[next];
    k--;
  }

  cout << next << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100001], val[2 * 100001];
bool visited[2 * 100001];

int main() {

  int n;
  long long int k;

  cin >> n >> k;

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

  int cnt = 1, next = a[1];
  visited[1] = true;
  while (visited[next] == false) {
    visited[next] = true;
    cnt++;
    if (cnt == k) {
      cout << next << endl;
      return 0;
    }
    val[next] = cnt;
    next = a[next];
  }

  k -= val[next];
  long long p = cnt - val[next];
  k = k % p;

  for (int i = 0; i < k; i++) {
    next = a[next];
  }

  cout << next << endl;

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

#include <bits/stdc++.h>

#define hell 1000000007
#define hell2 998244353
#define lcm(a, b) (a * b) / __gcd(a, b)
#define ll int
#define vll vector<ll>
#define pll vector<pair<ll, ll>>
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ret(x) return cout << x, 0;
#define endl '\\n'

const ll MAX = 300005;
const ll SQN = 330;
const ll LG = 22;
const double PI = atan(1) * 4;
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  long long k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) {
    cin >> a[i];
    a[i]--;
  }

  ll moves = min(k, 1LL * MAX);
  k -= moves;
  ll cur = 0;
  while (moves > 0) {
    cur = a[cur];
    moves--;
  }

  ll csz = 1;
  ll st = cur;
  moves = min(k, 1LL * MAX);
  while (moves > 0) {
    cur = a[cur];
    moves--;
    if (cur == st)
      break;
    csz++;
  }

  k %= csz;
  cur = st;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  ret(cur + 1);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define hell 1000000007
#define hell2 998244353
#define lcm(a, b) (a * b) / __gcd(a, b)
#define ll long long
#define vll vector<ll>
#define pll vector<pair<ll, ll>>
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ret(x) return cout << x, 0;
#define endl '\\n'

const ll MAX = 600005;
const ll SQN = 330;
const ll LG = 22;
const double PI = atan(1) * 4;
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) {
    cin >> a[i];
    a[i]--;
  }

  ll moves = min(k, MAX);
  k -= moves;
  ll cur = 0;
  while (moves > 0) {
    cur = a[cur];
    moves--;
  }

  ll csz = 1;
  ll st = cur;
  while (1) {
    cur = a[cur];
    if (cur == st)
      break;
    csz++;
  }

  k %= csz;
  cur = st;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  ret(cur + 1);
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll k;
  int n;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(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];
    // c:周期,l:周期の前の数
  }

  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 _GLIBCXX_DEBUG
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(200001, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  long long ans1 = 1, ans = 0;
  for (long long i = 1; i <= k; i++) {
    ans = a.at(ans1);
    if (i == k) {
      cout << ans << endl;
    } else {
      ans1 = ans;
    }
  }
  return 0;
}
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)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep1(i, n) cin >> a[i];
  int cur = 1;
  vector<ll> step(n + 1);
  vector<bool> visited(n + 1);
  ll cycle = 0;
  while (k > 0) {
    visited[cur] = true;
    int to = a[cur];
    if (visited[to]) {
      k--;
      cycle = step[cur] - step[to] + 1;
      k %= cycle;
      cur = to;
      break;
    }
    step[to] = step[cur] + 1;
    cur = to;
    k--;
  }
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur << 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)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep1(i, n) cin >> a[i];
  int cur = 1;
  vector<ll> step(n + 1);
  vector<bool> visited(n + 1);
  ll cycle = 0;
  while (true) {
    visited[cur] = true;
    int to = a[cur];
    if (visited[to]) {
      k -= step[to];
      cycle = step[cur] - step[to] + 1;
      k %= cycle;
      cur = to;
      break;
    }
    step[to] = step[cur] + 1;
    cur = to;
  }
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur << endl;

  return 0;
}