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 MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
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;
}
const ll INF = 1LL << 60;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> tel(n), path = {1};
  set<ll> seen = {1};
  ll loop;
  rep(i, n) cin >> tel[i];
  rep(i, n) {
    ll add = tel[*(path.end() - 1) - 1];
    if (seen.count(add)) {
      loop = add;
      break;
    }
    seen.insert(add);
    path.push_back(add);
  }
  assert(loop != NULL);
  if (k < path.size()) {
    cout << path[k] << endl;
    return 0;
  }
  rep(i, path.size()) if (path[i] == loop) cout
      << path[(k - i) % (path.size() - i) + i] << endl;
}


Error: -6
ebf49a15-e318-4cf7-837b-ce34a5c0891e.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02684/C++/s128713253.cpp:30: int main(): Assertion `fl' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
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;
}
const ll INF = 1LL << 60;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> tel(n), path = {1};
  set<ll> seen = {1};
  ll loop;
  rep(i, n) cin >> tel[i];
  rep(i, n) {
    ll add = tel[*(path.end() - 1) - 1];
    if (seen.count(add)) {
      loop = add;
      break;
    }
    seen.insert(add);
    path.push_back(add);
  }
  bool fl = false;
  assert(fl);
  assert(loop != NULL);
  rep(i, path.size()) if (path[i] == loop) cout
      << path[(k - i) % (path.size() - i) + i] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using plong = pair<ll, ll>;

const long MOD = 1000000007;

ll N, K;
vector<ll> seen;
map<ll, ll> mp;
ll aaa;
ll aa;
ll res;
void dfs(ll prev = -1, ll start = 0, ll count = 0) {
  if (seen[start] != -100) {
    aaa = seen[prev] - seen[start] + 1;
    aa = seen[start];
    return;
  }
  seen[start] = count;
  dfs(start, mp[start], count + 1);
}

void dfs2(ll start, ll count) {
  if (count == 0) {
    res = start;
    return;
  }
  // cout<<(start+1)<<endl;
  dfs2(mp[start], count - 1);
}

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

  seen.resize(N);
  for (ll i = 0; i < N; i++) {
    seen[i] = -100;
  }

  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;
    mp[i] = a;
  }
  dfs();
  if (K <= aa) {
    dfs2(0, K);
    cout << (res + 1) << endl;
    return 0;
  }
  ll tmp = (K - aa) % aaa + aa;
  // cout<<tmp<<":"<<aaa<<":"<<aa<<endl;
  dfs2(0, tmp);
  cout << (res + 1) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using plong = pair<ll, ll>;

const long MOD = 1000000007;

ll N, K;
vector<ll> seen;
map<ll, ll> mp;
ll aaa;
ll aa;
void dfs(ll start, ll count) {
  seen[start] = count;
  ll next = mp[start];
  if (seen[next] != -1) {
    aaa = seen[start] - seen[next] + 1;
    aa = seen[start];
    return;
  }
  dfs(mp[start], count + 1);
}

ll dfs2(ll start, ll count) {
  if (count == 0) {
    return start;
  }
  dfs2(mp[start], count - 1);
}

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

  seen.resize(N);
  for (ll i = 0; i < N; i++) {
    seen[i] = -1;
  }

  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;
    mp[i] = a;
  }
  dfs(0, 0);
  ll tmp = (K - aa) % aaa + aa;
  ll res = dfs2(0, tmp);
  cout << (res + 1) << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/

using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
// online submission
#endif
  ll n;
  ll k;
  cin >> n >> k;
  vector<ll> vv(n + 10);
  for (ll i = 1; i <= n; i++) {
    cin >> vv[i];
  }
  vector<bool> visited(1000000, false);
  vector<ll> v;
  ll i = 1;
  v.push_back(1);
  visited[i] = true;
  while (true) {
    if (visited[vv[i]] == true) {
      break;
    }
    v.push_back(vv[i]);
    i = vv[i];
    visited[i] = true;
  }
  ll first = 0;
  for (ll j = 0; j < (ll)v.size(); j++) {
    if (v[j] == vv[i]) {
      first = j;
      break;
    }
  }

  if (k < (ll)v.size()) {
    cout << v[k];
  }

  else {

    ll last = (ll)v.size() - 1;
    k -= first;
    ll r = (k % (last - first + 1));
    cout << v[first + r];
  }
}

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


Fixed submission:

#include <bits/stdc++.h>

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/

using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
// online submission
#endif
  int n;
  ll k;
  cin >> n >> k;
  vector<int> vv(n + 10);
  for (int i = 1; i <= n; i++) {
    cin >> vv[i];
  }
  vector<bool> visited(100000, false);
  int count = 0;
  vector<int> v;
  int i = 1;
  v.push_back(1);
  visited[i] = true;
  while (true) {
    if (visited[vv[i]] == true) {
      break;
    }
    v.push_back(vv[i]);
    i = vv[i];
    visited[i] = true;
  }
  int first = 0;
  for (int j = 0; j < v.size(); j++) {
    if (v[j] == vv[i]) {
      first = j;
      break;
    }
  }

  if (k < v.size()) {
    cout << v[k];
  }

  else {

    int last = v.size() - 1;
    k -= first;
    int r = (k % (last - first + 1));
    cout << v[first + r];
  }
}
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);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> root(n, 0);
  int n_start = 1;
  ll n_behind = 0;
  while (root[n_start - 1] == 0) {
    root[n_start - 1]++;
    n_start = a[n_start - 1];
    n_behind++;
  }

  // cout<<n_start<<endl;
  // cout<<n_behind<<endl;

  ll n_loop = 0;
  vector<int> loop;
  int m = n_start;
  do {
    loop.push_back(m);
    m = a[m - 1];
    n_loop++;
  } while (m != n_start);

  // cout<<n_loop<<endl;

  if (k < n_behind) {
    int result = 1;
    for (int i = 0; i < k; i++) {
      result = a[result - 1];
      if (i == k - 1) {
        cout << result << endl;
        return 0;
      }
    }
  } else {
    k -= n_behind;
    cout << loop[k % n_loop] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  map<int, int> maps;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maps[i + 1] = 0;
  }

  long long n_minus = 0;
  int n_start = 0;

  int start = 1;
  int t = 0;
  while (1) {
    if (maps[start] == 0) {
      maps[start]++;
      n_minus++;
      start = a[start - 1];
      t++;
      if (t == n) {
        int result = 1;
        for (int i = 0; i < n; i++) {
          result = a[result - 1];
          if (i == n - 1) {
            cout << result << endl;
            return 0;
          }
        }
      }
    } else if (maps[start] == 1) {
      n_start = start;
      break;
    }
  }

  vector<int> root;
  while (maps[n_start] < 2) {
    root.push_back(n_start);
    maps[n_start]++;
    n_start = a[n_start - 1];
  }

  long long n_size = root.size();
  k -= n_minus;
  cout << root[k % n_size] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
#define ll long long
#define ffor(i, n) for (int i = 0; i < (n); ++i)
#define fro(i, j, n) for (int i = (j); i < (n); ++i)
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define ff first
#define ss second
const int mod = 1e9 + 7;
template <typename T> T add(T a, T b) { return (a + b) % mod; }
template <typename T> T sub(T a, T b) { return add(a, mod - b); }
template <typename T> T mul(T a, T b) { return (a * b) % mod; }

int n, k;
const int mxn = 2e5 + 10;
vector<int> adj[mxn];

int len = 0;
vector<int> cycle;
vector<bool> vis;
int cycle_head;
void dfs(int root) {
  cycle.push_back(root);
  len++;
  vis[root] = true;
  for (auto x : adj[root]) {
    if (vis[x]) {
      cycle_head = x;
      return;
    } else {
      dfs(x);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vis.resize(n, false);
  ffor(i, n) {
    int x;
    cin >> x;
    --x;
    adj[i].push_back(x);
  }
  dfs(0);
  // cout << len;
  vector<int> real_cycle;
  int i = 0;
  int cnt_extra = 0;
  while (cycle[i] != cycle_head && (i < cycle.size())) {
    if (k == 0) {
      cout << cycle[i] + 1;
      return 0;
    }
    --k;
    ++i;
    ++cnt_extra;
  }
  while (i < cycle.size()) {
    real_cycle.push_back(cycle[i]);
    ++i;
  }
  k %= (int)real_cycle.size();
  // cout << k;
  cout << real_cycle[k] + 1;
}

Error: WA

Fixed submission:

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

#define int long long
#define ll long long
#define ffor(i, n) for (int i = 0; i < (n); ++i)
#define fro(i, j, n) for (int i = (j); i < (n); ++i)
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define ff first
#define ss second
const int mod = 1e9 + 7;
template <typename T> T add(T a, T b) { return (a + b) % mod; }
template <typename T> T sub(T a, T b) { return add(a, mod - b); }
template <typename T> T mul(T a, T b) { return (a * b) % mod; }

int n, k;
const int mxn = 2e5 + 10;
vector<int> adj[mxn];

int len = 0;
vector<int> cycle;
vector<bool> vis;
int cycle_head;
void dfs(int root) {
  cycle.push_back(root);
  len++;
  vis[root] = true;
  for (auto x : adj[root]) {
    if (vis[x]) {
      cycle_head = x;
      return;
    } else {
      dfs(x);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vis.resize(n, false);
  ffor(i, n) {
    int x;
    cin >> x;
    --x;
    adj[i].push_back(x);
  }
  dfs(0);
  // cout << len;
  vector<int> real_cycle;
  int i = 0;
  int cnt_extra = 0;
  while (cycle[i] != cycle_head && (i < cycle.size())) {
    ++i, --k;
    ++cnt_extra;
  }
  while (i < cycle.size()) {
    real_cycle.push_back(cycle[i]);
    ++i;
  }
  if (k >= 0) {
    k %= (int)real_cycle.size();
    // cout << k;
    cout << real_cycle[k] + 1;
  } else {
    cout << cycle[k];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
constexpr long long int mod = 1000000007;

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

  for (size_t i = 0; i < N; i++) {
    cin >> Jump.at(i);
    Jump.at(i)--;
  }
  vector<long long> Firstcome(N, -1);
  long long now = 0;
  long long loop_beginp = 0;
  long long loop_begin = 0, looping = 0;
  Firstcome.at(0) = 0;
  for (size_t i = 0; i < N; i++) {
    long long jump = Jump.at(now);
    if (Firstcome.at(jump) < 0 && Firstcome.at(now) != K)
      Firstcome.at(jump) = Firstcome.at(now) + 1; // 一回も訪れてなければ初訪問
    else if (Firstcome.at(jump) >= 0) {
      loop_begin = Firstcome.at(jump), loop_beginp = jump,
      looping = Firstcome.at(now) + 1 - Firstcome.at(jump);
      break;
    } else if (Firstcome.at(now) == K) {
      cout << now + 1;
      return 0;
    }
    now = jump;
  }
  long long ans = loop_beginp;
  for (size_t i = 0; i < abs(K - loop_begin) % looping; i++) {
    ans = Jump.at(ans);
  }
  cout << ans + 1;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
constexpr long long int mod = 1000000007;

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

  for (size_t i = 0; i < N; i++) {
    cin >> Jump.at(i);
    Jump.at(i)--;
  }
  vector<int> Firstcome(N, -1);
  int now = 0;
  int loop_beginp = 0;
  int loop_begin = 0, looping = 0;
  Firstcome.at(0) = 0;
  for (size_t i = 0; i < min(K, N); i++) {
    int jump = Jump.at(now);
    if (Firstcome.at(jump) == -1)
      Firstcome.at(jump) = Firstcome.at(now) + 1; // 一回も訪れてなければ初訪問
    else {
      loop_begin = Firstcome.at(jump), loop_beginp = jump,
      looping = Firstcome.at(now) + 1 - Firstcome.at(jump);
      break;
    }
    now = jump;
  }
  int ans = loop_beginp;
  for (size_t i = 0; i < N; i++) {
    cerr << Firstcome.at(i) << endl;
  }
  cerr << endl << looping << endl;
  for (size_t i = 0; i < (K - loop_begin) % looping; i++) {
    ans = Jump.at(ans);
  }
  cout << ans + 1;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;

const int MaxN = 200005;

int n;
long long k;
int a[MaxN], depth[MaxN];

int main() {
  cin >> n >> k;
  rep(i, 1, n) {
    cin >> a[i];
    depth[i] = -1;
  }
  depth[1] = 0;
  int Count = 0, x = 1;
  while (depth[a[x]] == -1) {
    x = a[x];
    Count++;
    k--;
    if (k == 0) {
      cout << x;
      return 0;
    }
    depth[x] = Count;
  }
  x = a[x];
  Count++;
  k--;
  k = k % (Count - depth[x]);
  while (k > 0) {
    k--;
    x = a[x];
  }
  cout << x;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int loop;
vector<int> g[200005];
int d[200005], dl;
void dfs(int u) {
  for (auto v : g[u])
    if (d[v] == 0) {
      d[v] = d[u] + 1;
      dfs(v);
    } else if (d[v]) {
      loop = v;
      dl = d[u] - d[1] + 1;
      return;
    }
}
int n, k;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    g[i].push_back(x);
  }

  d[1] = 1;
  dfs(1);
  // cout<<loop<<" "<<dl;
  for (int i = 1; i < n; ++i)
    if (d[i] == k + 1) {
      cout << i;
      return 0;
    }
  k++;

  k = k - (d[loop] - 1);
  k = k % dl;
  if (!k)
    k = dl;
  int i = 1;
  while (k != 1) {
    for (auto v : g[i])
      i = v;
    k--;
  }
  cout << i;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<i64> A(N);
  for (i64 i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<vector<i64>> to(60, vector<i64>(N));
  for (i64 i = 0; i < N; i++)
    to[0][i] = A[i];
  for (i64 i = 0; i < 59; i++)
    for (i64 j = 0; j < N; j++)
      to[i + 1][j] = to[i][to[i][j]];
  i64 ans = 0;
  for (i64 i = 0; i < 60; i++)
    if (K & (1LL << i))
      ans = to[i][ans];
  cout << ans + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<i64> A(N);
  for (i64 i = 0; i < N; i++)
    cin >> A[i];
  vector<bool> vis(N, false);
  i64 n = A[0];
  while (!vis[n]) {
    n = A[n - 1];
    vis[n] = true;
  }
  i64 cnt = 0;
  if (n != 1) {
    for (i64 i = A[0]; i != n; cnt++) {
      if (K == cnt + 1) {
        cout << i << endl;
        return 0;
      }
      i = A[i - 1];
    }
    cnt++;
  }
  vector<i64> t;
  for (i64 i = A[n - 1]; i != A[n - 1] || t.size() == 0;) {
    t.push_back(i);
    i = A[i - 1];
  }
  cout << t[(K - cnt - 1) % t.size()] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, m, tot, now, ans, sum;
int a[N], b[N], c[N], f[N];
signed main() {
  scanf("%lld%lld", &n, &m);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (m <= n) {
    now = 1;
    for (register int i = 1; i <= m; ++i)
      now = a[now];
    printf("%lld\\n", now);
    return 0;
  }

  m++;
  now = 1;
  tot = 1;
  f[1] = tot;
  b[tot] = 1;
  while (true) {
    if (f[a[now]]) {
      m -= f[a[now]] - 1;
      for (register int i = 1; i <= tot - f[a[now]] + 1; ++i)
        c[i] = b[i + f[a[now]] - 1];
      break;
    }
    tot++;
    f[a[now]] = tot;
    b[tot] = a[now];
    now = a[now];
  }
  //	for (register int i=1; i<=tot-f[a[now]]+1; ++i) printf("%lld ",c[i]);
  //puts("");
  sum = tot - f[a[now]] + 1;
  if (m % sum == 0)
    ans = sum;
  else
    ans = m % sum;
  printf("%lld\\n", c[ans]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, tot, now, ans, sum;
int a[N], b[N], c[N], f[N];
signed main() {
  scanf("%lld%lld", &n, &m);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (m <= n) {
    now = 1;
    for (register int i = 1; i <= m; ++i)
      now = a[now];
    printf("%lld\\n", now);
    return 0;
  }

  m++;
  now = 1;
  tot = 1;
  f[1] = tot;
  b[tot] = 1;
  while (true) {
    if (f[a[now]]) {
      m -= f[a[now]] - 1;
      for (register int i = 1; i <= tot - f[a[now]] + 1; ++i)
        c[i] = b[i + f[a[now]] - 1];
      break;
    }
    tot++;
    f[a[now]] = tot;
    b[tot] = a[now];
    now = a[now];
  }
  //	for (register int i=1; i<=tot-f[a[now]]+1; ++i) printf("%lld ",c[i]);
  //puts("");
  sum = tot - f[a[now]] + 1;
  if (m % sum == 0)
    ans = sum;
  else
    ans = m % sum;
  printf("%lld\\n", c[ans]);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int p;
  cin >> p;
  vector<int> a(n), t(n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  int start = 0;
  int m_time = 0;
  t[start] = m_time++;
  int cycle_length = 0;
  vector<pair<int, int>> cycle;
  while (true) {
    int prev = start;
    start = a[start];
    if (t[start] != -1) {
      cycle_length = t[prev] - t[start] + 1;
      for (int i = 0; i < n; i++) {
        if (t[i] >= t[start]) {
          cycle.push_back({t[i], i});
        }
      }
      break;
    }
    t[start] = m_time++;
    if (1ll * m_time == p + 1) {
      cout << start + 1 << "\\n";
      return 0;
    }
  }
  p -= m_time - cycle_length;
  sort(cycle.begin(), cycle.end());
  p %= cycle_length;
  cout << cycle[p].second + 1 << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int p;
  cin >> p;
  vector<int> a(n), t(n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  int start = 0;
  int m_time = 0;
  t[start] = m_time++;
  int cycle_length = 0;
  vector<pair<int, int>> cycle;
  while (true) {
    int prev = start;
    start = a[start];
    if (t[start] != -1) {
      cycle_length = t[prev] - t[start] + 1;
      for (int i = 0; i < n; i++) {
        if (t[i] >= t[start]) {
          cycle.push_back({t[i], i});
        }
      }
      break;
    }
    t[start] = m_time++;
  }
  p -= m_time - cycle_length;
  sort(cycle.begin(), cycle.end());
  p %= cycle_length;
  cout << cycle[p].second + 1 << "\\n";
  return 0;
}
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<ll> A(N);

  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<ll> v;
  ll i = 1;
  ll p = A[0];
  ll f = N - 1;
  set<ll> s;
  for (i = 0; i < N; i++) {
    v.push_back(p);
    if (s.count(p)) {
      auto itr = find_if(v.begin(), v.end(), [&](ll x) { return (x == p); });
      f = ll(distance(v.begin(), itr));
      break;
    }
    s.insert(p);
    if (i < N)
      p = A[p];
  }

  if (K <= i)
    cout << v[K - 1] + 1 << endl;
  else {
    ll rp = i - f;
    cout << v[(K - 1 - i) % rp + f] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

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

  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<ll> v;
  ll i = 1;
  ll p = A[0];
  ll f = N - 1;
  for (i = 0; i < N; i++) {
    v.push_back(p);
    auto itr = find_if(v.begin(), v.end(), [&](ll x) { return (x == p); });
    if (itr != v.end() - 1) {
      f = ll(distance(v.begin(), itr));
      break;
    }
    if (i < N)
      p = A[p];
  }

  if (K <= i)
    cout << v[K - 1] + 1 << endl;
  else {
    ll rp = i - f;
    cout << v[(K - 1 - i) % rp + f] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using std::cin;
using std::cout;
using std::endl;
using std::vector;

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

  vector<int> porters;
  porters.resize(N + 1);

  for (int i = 1; i <= N; i++) {
    int num;
    cin >> num;
    porters[i] = num;
  }

  int ans(1);
  std::set<int> set;
  std::vector<int> root;
  bool loop_not_found = true;
  root.push_back(1);
  while (K > 0) {
    ans = porters[ans];
    K--;
    if (loop_not_found && set.count(ans) > 0) {
      // ループ発見
      auto itr = std::find(root.begin(), root.end(), ans);
      int count = std::distance(root.begin(), itr);
      int loop_size = root.size() - count;
      K = K % loop_size;

      root.clear();
      loop_not_found = false;
    } else {
      set.insert(ans);
      root.push_back(ans);
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

using std::cin;
using std::cout;
using std::endl;
using std::vector;

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

  vector<int> porters;
  porters.resize(N + 1);

  for (int i = 1; i <= N; i++) {
    int num;
    cin >> num;
    porters[i] = num;
  }

  int ans(1);
  std::vector<int> root;
  root.push_back(1);
  while (K > 0) {
    ans = porters[ans];
    K--;
    auto itr = std::find(root.begin(), root.end(), ans);
    if (itr != root.end()) {
      // ループ発見
      int count = std::distance(root.begin(), itr);
      int loop_size = root.size() - count;
      K = K % loop_size;

      root.clear();
    } else {
      root.push_back(ans);
    }
  }

  cout << ans << endl;
}