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

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s, f) cout << fixed << setprecision(f) << s << endl;

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<vector<ll>> s(62, vector<ll>(n));
  for (ll i = 0; i < n; i++) {
    s[0][i] = a[i];
  }

  for (ll i = 0; i < 60; i++) {
    for (ll j = 0; j < n; j++) {
      s[i + 1][j] = s[i][s[i][j]];
    }
  }

  ll cur = 0;
  for (ll i = 0; i < 60; i++) {
    if ((k >> i) & 1) {
      cur = s[i][cur];
    }
  }
  println(cur + 1);
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s, f) cout << fixed << setprecision(f) << s << endl;

int main() {
  ll n, k;
  cin >> n >> k;

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

  string s = "1";
  ll x = 0;
  for (ll i = 0; i < 200010; i++) {
    s = s + to_string(a[x]);

    x = a[x] - 1;
  }

  // println(s);

  vector<ll> h(200010, 0);

  ll c0 = 0, c1 = 0;
  for (ll i = 0; i < s.length(); i++) {
    ll y = s[i] - '0';
    if (c0 == 0 && h[y] == 2) {
      c0 = i;
    } else if (c1 == 0 && h[y] == 3) {
      c1 = i;
      break;
    }

    h[y]++;
  }

  ll c = c1 - c0;

  string ss = s.substr(c0, c);

  println(ss[(k - c0 + c) % c]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int n, t;
long long int k;

int A[200001];
map<int, bool> visited;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;

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

  vector<int> vec;
  int now = 1;
  while (1) {
    if (visited[now]) {
      break;
    }
    visited[now] = true;
    vec.push_back(now);
    now = A[now];
  }

  int idx = 0;
  for (idx = 0; idx < vec.size(); idx++) {
    if (now == vec[idx]) {
      break;
    }
  }

  if (k < idx) {
    cout << vec[k] << '\\n';
  } else {
    vector<int> circle;
    for (int j = idx; j < vec.size(); j++) {
      circle.push_back(vec[j]);
    }
    k -= idx;
    int m = circle.size();
    cout << circle[k % m] << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, t;
long long int k;

int A[100001];
map<int, bool> visited;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;

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

  vector<int> vec;
  int now = 1;
  while (1) {
    if (visited[now]) {
      break;
    }
    visited[now] = true;
    vec.push_back(now);
    now = A[now];
  }

  int idx = 0;
  for (idx = 0; idx < vec.size(); idx++) {
    if (now == vec[idx]) {
      break;
    }
  }

  if (k < idx) {
    cout << vec[k] << '\\n';
  } else {
    vector<int> circle;
    for (int j = idx; j < vec.size(); j++) {
      circle.push_back(vec[j]);
    }
    k -= idx;
    int m = circle.size();
    cout << circle[k % m] << '\\n';
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

ll n;
ll a[220000];
ll k;
map<ll, ll> mp; // 何回目に訪れたか
ll cnt = 0;
ll pre = 1;
ll p[220000]; // n回目に訪れた場所

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll t;
  while (true) {
    if (mp[pre] > 0 || (pre == 1 && cnt > 0)) {
      t = cnt - mp[pre];
      break;
    }
    p[cnt] = pre;
    mp[pre] = cnt;
    cnt++;
    pre = a[pre - 1];
  }
  if (k > mp[pre]) {
    ll u = k - mp[pre];
    cout << p[(k - mp[pre]) % t + mp[pre]] << endl;
  } else {
    cout << p[k] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

ll n;
ll a[200000];
ll k;
map<int, int> mp;
ll cnt = 0;
ll pre = 1;
ll p[200000];

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll t;
  while (true) {
    if (mp[pre] > 0) {
      t = cnt - mp[pre];
      break;
    }
    p[cnt] = pre;
    mp[pre] = cnt;
    cnt++;
    pre = a[pre - 1];
  }
  cout << p[(k - mp[pre] + 1) % t] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

ll N, K;
vector<ll> A;
vector<ll> AA;

pair<ll, ll> func1(ll s, ll c) {
  if (AA[s] >= 0 || K == c) {
    if (K == c) {
      return {s, 0};
    } else {
      return {s, c};
    }
  }
  AA[s] = c;
  return func1(A[s], c + 1);
}

ll func2(ll s, ll c, ll gc) {
  if (c == gc) {
    return s;
  }
  return func2(A[s], c + 1, gc);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  A.assign(N, 0);
  AA.assign(N, -1);
  REPLL(i, N) {
    ll tmp;
    cin >> tmp;
    tmp--;
    A[i] = tmp;
  }
  pair<ll, ll> tmp = func1(0, 0);
  if (tmp.second == 0) {
    cout << tmp.first + 1 << endl;
    return 0;
  }
  ll cnt = tmp.second - AA[tmp.first];
  ll res = (K - AA[tmp.first]) % cnt;
  ll ans = func2(tmp.first, 0, res);
  cout << ans + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

ll N, K;
vector<ll> A;
vector<ll> AA;

pair<ll, ll> func1(ll s, ll c) {
  if (AA[s] >= 0) {
    return {s, c};
  }
  AA[s] = c;
  func1(A[s], c + 1);
}

ll func2(ll s, ll c, ll gc) {
  if (c == gc) {
    return s;
  }
  func2(A[s], c + 1, gc);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  A.assign(N, 0);
  AA.assign(N, -1);
  REPLL(i, N) {
    ll tmp;
    cin >> tmp;
    tmp--;
    A[i] = tmp;
  }
  pair<ll, ll> tmp = func1(0, 0);
  ll cnt = tmp.second - AA[tmp.first];
  ll res = (K - AA[tmp.first]) % cnt;
  ll ans = func2(tmp.first, 0, res);
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long int
using namespace std;
int a[200005], s, vis[200005];
vector<int> v, u;
void dfs(int v) {
  if (vis[v]) {
    s = v;

    return;
  }
  vis[v] = 1;
  dfs(a[v]);
}
void dfs1(int v) {
  if (v == s)
    return;
  u.pb(v);
  dfs1(a[v]);
}

int32_t main() {
  int n, k, i;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    // mp[a[i]]=i;
  }
  dfs(1);
  i = 1;
  while (i != s) {
    v.pb(i);
    i = a[i];
  }
  u.pb(s);
  dfs1(a[s]);
  /*for(auto x:u)
          cout<<x<<" ";
  cout<<"\\n";

  for(auto x:v)
          cout<<x<<" ";
  cout<<"\\n";*/
  if (k < v.size())
    cout << v[k] << "\\n";
  else {
    k -= v.size();
    int sz = u.size();
    cout << u[k % sz] << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long int
using namespace std;
int a[100005], s, vis[100005];
vector<int> v, u;
void dfs(int v) {
  if (vis[v]) {
    s = v;

    return;
  }
  vis[v] = 1;
  dfs(a[v]);
}
void dfs1(int v) {
  if (v == s)
    return;
  u.pb(v);
  dfs1(a[v]);
}

int32_t main() {
  int n, k, i;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    // mp[a[i]]=i;
  }
  dfs(1);
  i = 1;
  while (i != s) {
    v.pb(i);
    i = a[i];
  }
  u.pb(s);
  dfs1(a[s]);
  /*for(auto x:u)
          cout<<x<<" ";
  cout<<"\\n";

  for(auto x:v)
          cout<<x<<" ";
  cout<<"\\n";*/
  if (k < v.size())
    cout << v[k] << "\\n";
  else {
    k -= v.size();
    int sz = u.size();
    cout << u[k % sz] << "\\n";
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int n, k;
  long long int help;
  cin >> n >> k;
  int arr[n + 1] = {0}, visited[n + 1];
  for (int i = 0; i <= n; i++) {
    visited[i] = -1;
  }
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  int a = 1;
  int b = 0;
  visited[1] = 0;
  long long int i;
  for (i = 1; i <= k; i++) {
    b = arr[a];
    a = b;
    if (visited[a] == -1) {
      visited[a] = i;
    } else {
      help = i - visited[a];
      break;
    }
  }
  if (i == k + 1) {
    cout << a;
  } else {
    long long int b = k - visited[a];
    long long int c = b % help;
    for (int j = 0; j < c; j++) {
      b = arr[a];
      a = b;
    }
    cout << a;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  long long int arr[n];
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long int e = 1, b = 1, d = 0, f = -1;
  for (long long int i = 0; i < ((k + 1) / 2); i++) {
    e = arr[b - 1];
    b = arr[e - 1];
    if (e == arr[0] && i != 1) {
      d = i;
      break;
    }
    if (b == arr[0]) {
      f = i;
      break;
    }
  }
  if (d != 0) {
    k = k % (2 * d + 1);
  }
  if (f != -1) {
    k = k % (2 * f + 2);
  }
  b = 1, e = 1;
  for (long long int i = 0; i < ((k + 1) / 2); i++) {
    e = arr[b - 1];
    b = arr[e - 1];
  }
  if (k % 2 != 0) {
    cout << e;
  } else {
    cout << b;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2e5 + 20;

int n, a[N], inf;
long long k;
vector<int> dp[N];

void go(int node, int steps = 1) {
  if (steps >= 1000000)
    return;
  dp[node].push_back(steps);
  int child = a[node];
  go(child, steps + 1);
}

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

  k++;
  go(1);
  for (int i = 1; i <= n; i++) {
    for (auto j : dp[i])
      if (j == k)
        return 0 * printf("%d", i);
    if (dp[i].size() > 1 and k >= dp[i][0]) {
      if (dp[i][0] == dp[i][1])
        inf = i;
      else if ((k - dp[i][0]) % (dp[i][1] - dp[i][0]) == 0)
        return 0 * printf("%d", i);
    }
  }
  printf("%d", inf);
}

Error: -11

Fixed submission:

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

const int N = 2e5 + 20;

int n, a[N];
long long k;
vector<int> dp[N];

void go(int node, int steps = 1) {
  if (steps >= 1000000)
    return;
  dp[node].push_back(steps);
  int child = a[node];
  go(child, steps + 1);
}

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

  k++;
  go(1);
  for (int i = 1; i <= n; i++) {
    for (auto j : dp[i])
      if (j == k)
        return 0 * printf("%d", i);
    if (dp[i][0] < k and dp[i].size() > 1) {
      if ((k - dp[i][0]) % (dp[i][1] - dp[i][0]) == 0)
        return 0 * printf("%d", i);
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2e5 + 20;

int n, a[N], vis[N], cycle, dp[N];
long long k;

void go(int node, int steps = 1) {
  vis[node] = steps;
  dp[steps] = node;
  int child = a[node];
  if (vis[child] == 0)
    go(child, steps + 1);
  else
    cycle = child;
}

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

  k++;
  go(1);
  if (k < N and dp[k])
    printf("%d", dp[k]);
  else {
    k -= vis[cycle];
    vector<int> v = {cycle};
    for (int node = a[cycle]; node != cycle; node = a[node])
      v.push_back(node);
    cout << v[k % v.size()];
  }
}

Error: WA

Fixed submission:

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

const int N = 2e5 + 20;

int n, a[N], cycle, dp[N];
long long k;
bool vis[N];

void go(int node, int steps = 1) {
  vis[node] = true;
  dp[steps] = node;
  int child = a[node];
  if (not vis[child])
    go(child, steps + 1);
  else
    cycle = child;
}

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

  k++;
  go(1);
  if (k < N and dp[k])
    printf("%d", dp[k]);
  else {
    assert(dp[cycle] != 0);
    k -= dp[cycle];
    vector<int> v = {cycle};
    for (int node = a[cycle]; node != cycle; node = a[node])
      v.push_back(node);
    cout << v[k % v.size()];
  }
}
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 repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1LL << 60;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<bool> b(n, false);
  vector<int> c;
  int x = 0;
  int pos = 0;
  rep(i, n * 2) {
    if (!b[pos]) {
      b[pos] = true;
      c.push_back(pos);
      pos = a[pos];
    } else {
      x = pos;
      break;
    }
  }

  vector<int> d;
  bool ok = false;
  rep(i, c.size()) {
    if (c[i] == x)
      ok = true;
    if (ok)
      d.push_back(c[i]);
  }

  int l = d.size();
  int jama = c.size() - l;
  if (k < jama) {
    cout << c[k] + 1 << endl;
  } else {
    cout << d[(k - jama) % l] + 1 << 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 repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1LL << 60;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<bool> b(n, false);
  vector<int> c;
  int x = 0;
  int pos = 0;
  rep(i, n * 2) {
    if (!b[pos]) {
      b[pos] = true;
      c.push_back(pos);
      pos = a[pos];
    } else {
      x = pos;
      break;
    }
  }

  vector<int> d;
  bool ok = false;
  rep(i, c.size()) {
    if (c[i] == x)
      ok = true;
    if (ok)
      d.push_back(c[i]);
  }

  int l = d.size();
  int jama = c.size() - l;
  cout << d[((k - jama) % l)] + 1 << endl;

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

#include <iostream>
using namespace std;
#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define endl "\\n"
#define ff first
#define ss second
#define mod 1000000007
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppl;
typedef map<ll, ll> mpll;
typedef map<vector<ll>, ll> mpvl;
int main() {
  speed;
  ll t;

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

    map<ll, ll> mp;
    vector<ll> v;
    ll i = 0;
    mp[1]++;
    v.pb(1);
    while (true) {
      v.pb(arr[i]);

      if (mp[arr[i]] != 0) {
        break;
      }
      mp[arr[i]]++;
      i = arr[i] - 1;
    }

    auto it = v.end();
    it--;
    ll ff = (*it);
    ll tk, st;
    vector<ll> cy;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i] == ff) {
        tk = (v.size() - 1) - i;
        st = i;
        break;
      }
    }
    if (k <= st) {
      cout << v[k];
      return 0;
    }

    for (ll i = st; i < v.size() - 1; i++) {
      cy.pb(v[i]);
    }

    ll md = (k - st) % (tk);

    if (md == 0)
      cout << cy[0] << endl;
    else {
      cout << cy[md] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define endl "\\n"
#define ff first
#define ss second
#define mod 1000000007
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppl;
typedef map<ll, ll> mpll;
typedef map<vector<ll>, ll> mpvl;
int main() {
  speed;
  ll t;

  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll arr[n];
    for (ll i = 0; i < n; i++) {
      cin >> arr[i];
    }
    ll cnt = 0;
    map<ll, ll> mp;
    vector<ll> v;
    ll i = 0;
    mp[1]++;
    v.pb(1);
    while (true) {
      v.pb(arr[i]);

      if (mp[arr[i]] != 0) {
        break;
      }
      mp[arr[i]]++;
      i = arr[i] - 1;
    }
    // cerr<<"D";
    auto it = v.end();
    it--;
    ll ff = (*it);
    ll tk, st;
    vector<ll> cy;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i] == ff) {
        tk = (v.size() - 1) - i;
        st = i;
        break;
      }
    }
    // cout<<tk<<" "<<st<<endl;
    // for(auto it:v)
    // {
    //   cout<<it<<" ";
    // }
    for (ll i = st; i < v.size() - 1; i++) {
      cy.pb(v[i]);
    }
    // for(auto it:cy)
    // {
    //   cout<<"F";
    //   cout<<it<<" ";
    // }
    // cerr<<"D";
    ll md = (k - st) % (tk);
    // cout<<md<<endl;
    if (md == 0)
      cout << cy[0] << endl;
    else {
      cout << cy[md] << endl;
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;

  vector<long> a(n, 0);

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

  set<long> used;
  vector<long> road;
  road.reserve(n);
  long next = 0;
  long count = -1;

  while (true) {
    if (used.count(next) == 0) {
      road.push_back(next);
      used.insert(next);
      next = a[next];
      count++;
      if (k - count == 0) {
        break;
      }

    } else {
      auto result = find(road.begin(), road.end(), next);
      if (*result != 0) {
        k = k - distance(road.begin(), result);
        road.erase(road.begin(), result);
      }
      break;
    }
  }

  cout << road[k % road.size()] + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;

  vector<long> a(n, 0);

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

  vector<long> road;
  road.reserve(n);
  long next = 0;
  while (true) {
    auto result = find(road.begin(), road.end(), next);
    if (result == road.end()) {
      road.push_back(next);
      next = a[next];
      if (k - distance(road.begin(), result) == 0) {
        break;
      }
    } else {
      if (*result != 0) {
        k = k - distance(road.begin(), result);
        road.erase(road.begin(), result);
      }
      break;
    }
  }

  cout << road[k % road.size()] + 1 << endl;

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

#include <iostream>
#include <vector>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
const int MAX_N = 210000;
using namespace std;
using ll = long long;

int main() {
  ll N, K;

  cin >> N >> K;

  vector<ll> A(N + 1, 0);
  FOR(i, N) cin >> A[i];

  // 何番目か
  vector<int> ord(N + 1, -1);
  // 　頂点の保存
  vector<int> s;

  // cは周期 lはループ前の長さ
  int c = 1, l = 0, v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.emplace_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: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <iostream>
#include <vector>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
const int MAX_N = 210000;
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  ll sum;
  vector<int> A(N + 1, 0);
  bool seen[N + 1];
  cin >> N >> K;
  FOR(i, N) cin >> A[i];

  int cur = 0;
  int cnt = 0;
  bool flag = false;
  while (true) {
    if (A[cur] == 1) {
      sum++;
      break;
    }
    if (seen[A[cur] - 1]) {
      if (cnt++ > 0)
        break;
      flag = true;
    }
    seen[cur] = true;
    cur = A[cur] - 1;
    sum += 1;
    if (flag)
      cnt++;
  }

  if (sum == 1) {
    cout << 1 << endl;
    return 0;
  }

  if (cnt == 0) {
    K %= sum;
    if (!K)
      K = sum;
  } else {
    K -= sum;
    K %= cnt;
    if (!K)
      K = sum + cnt;
  }

  int ans = 0;
  cur = 0;
  sum = 0;
  while (true) {
    sum++;
    if (sum == K) {
      ans = A[cur];
      break;
    }

    cur = A[cur] - 1;
  }

  cout << ans << endl;
  return 0;
}