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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, k, a[200010], val;
vector<LL> v, vv;
bool vis[200010];

int main() {
  cin >> n >> k;
  k++;
  repn(i, n) cin >> a[i];
  v.pb(1);
  vis[1] = 1;
  int pos = 1;
  while (true) {
    pos = a[pos];
    if (vis[pos]) {
      val = pos;
      break;
    }
    vis[pos] = true;
    v.pb(pos);
  }
  rep(i, v.size()) {
    if (v[i] == val) {
      pos = i;
      for (int j = i; j < v.size(); j++)
        vv.pb(v[j]);
      break;
    }
  }
  if (k - 1 <= pos)
    cout << v[k - 1] << endl;
  else
    cout << vv[((k - pos) % vv.size() - 1 + vv.size()) % vv.size()] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, k, a[200010], val;
vector<LL> v, vv;
bool vis[200010];

int main() {
  cin >> n >> k;
  k++;
  repn(i, n) cin >> a[i];
  v.pb(1);
  vis[1] = 1;
  int pos = 1;
  while (true) {
    pos = a[pos];
    if (vis[pos]) {
      val = pos;
      break;
    }
    vis[pos] = true;
    v.pb(pos);
  }
  rep(i, v.size()) {
    if (v[i] == val) {
      pos = i;
      for (int j = i; j < v.size(); j++)
        vv.pb(v[j]);
      break;
    }
  }
  if (k - 1 <= pos)
    cout << v[pos] << endl;
  else
    cout << vv[((k - pos) % vv.size() - 1 + v.size()) % v.size()] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define SORT(v, n) std::sort(v, v + n);
#define VSORT(v) std::sort(v.begin(), v.end());
#define VRSORT(v) std::sort(v.rbegin(), v.rend());
#define ll long long
#define pb(a) push_back(a)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

const int INF = 99999999;
const ll LINF = 9999999999999;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> history;
  vector<ll> ord(n + 1, -1);
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = history.size();
      history.push_back(v);
      v = a[v - 1];
    }
    // 2回目にきたとき-1回目のとき
    c = history.size() - ord[v];
    l = ord[v];
  }

  if (k < l)
    cout << history[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << history[l + k] << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define SORT(v, n) std::sort(v, v + n);
#define VSORT(v) std::sort(v.begin(), v.end());
#define VRSORT(v) std::sort(v.rbegin(), v.rend());
#define ll long long
#define pb(a) push_back(a)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

const int INF = 99999999;
const ll LINF = 9999999999999;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> history;
  vector<ll> ord(n + 1, -1);
  vector<ll> a(n);
  ll c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = history.size();
      history.push_back(v);
      v = a[v - 1];
    }
    // 2回目にきたとき-1回目のとき
    c = history.size() - ord[v];
    l = ord[v];
  }

  if (k < l)
    cout << history[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << history[l + k] << endl;
  }

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

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> num(n, -1); // 町を何番目に通ったか
  vector<int> seen;       // 通った町
  int now = 1;            // 現在の街

  while (num[now - 1] == -1) {
    num[now - 1] = seen.size();
    seen.push_back(now);
    now = a[now - 1];
  }
  int head = num[now - 1];
  int loop = seen.size() - head;
  int ans;
  if (k < head)
    ans = seen[k];
  else {
    int mod = (k - head) % loop;
    ans = seen[head + mod];
  }

  cout << ans << endl;
}


Error: WA

Fixed submission:

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> num(n, -1); // 町を何番目に通ったか
  vector<int> seen;       // 通った町
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int now = 1; // 現在の街
  int c = 0;   // numに入れる数
  while (num[now - 1] == -1) {
    seen.push_back(now);
    num[now - 1] = c;
    now = a[now - 1];
    c++;
  }
  int head = num[now - 1] - 1;
  int loop = seen.size() - 1;
  int ans;
  if (k < head)
    ans = seen[k];
  else {
    int mod = (k - head) % loop;
    ans = seen[head + mod];
  }

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

#include <assert.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  unsigned long long int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, -1);

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

  int now = 1;
  // naive
  if (k <= 200000) {
    // assert(now > 0);
    for (int i = 0; i < k; ++i) {
      // cout << now << " " << a[now] << endl;
      now = a[now];
    }
  } else {

    vector<int> isVisited(n + 1, false);
    queue<int> q;
    int roop_point = 0;
    long long int roop_size = 0;

    while (1) {
      if (!isVisited[now]) {
        q.push(now);
        isVisited[now] = true;
        now = a[now];
      } else {
        roop_point = now;
        break;
      }
    }

    while (1) {
      int tmp = q.front();
      if (tmp != roop_point) {
        --k;
        q.pop();
      } else {
        roop_size = (long long int)q.size();
        break;
      }
    }

    long long int remnant = 0;
    if (roop_size != 0) {
      remnant = k % roop_size;
    }

    for (long long int i = 0; i < remnant; ++i) {
      now = a[now];
    }
  }

  cout << now << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <assert.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  unsigned long long int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, -1);

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

  int now = 1;
  // naive
  if (k <= 200000) {
    // assert(now > 0);
    for (int i = 0; i < k; ++i) {
      // cout << now << " " << a[now] << endl;
      now = a[now];
    }
  } else {

    vector<int> isVisited(n, false);
    queue<int> q;
    int roop_point = 0;
    long long int roop_size = 0;

    while (1) {
      if (!isVisited[now]) {
        q.push(now);
        isVisited[now] = true;
        now = a[now];
      } else {
        roop_point = now;
        break;
      }
    }

    while (1) {
      int tmp = q.front();
      if (tmp != roop_point) {
        --k;
        q.pop();
      } else {
        roop_size = (long long int)q.size();
        break;
      }
    }

    long long int remnant = 0;
    if (roop_size != 0) {
      remnant = k % roop_size;
    }

    for (long long int i = 0; i < remnant; ++i) {
      now = a[now];
    }
  }

  cout << now << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

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

  long long log_n = floor(log2(k));

  vector<vector<long long>> next(log_n + 1, vector<long long>(n + 1));

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

  for (int k = 0; k < log_n; k++) {
    for (int i = 1; i <= n; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]]; // 2^k乗先を求める
      }
    }
  }

  long long p = 1;
  // printf("log_n %lld\\n", log_n);
  for (long long j = log_n; j >= 0; --j) {
    // printf("p %lld\\n", p);
    // printf("j %lld\\n", j);
    if (p == -1) {
      break;
    }

    if ((k >> j) & 1) {
      // printf("bit p %lld\\n", p);
      // printf("bit j %lld\\n", j);
      p = next[j][p];
    }
  }

  printf("%lld\\n", p);

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

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

  long long log_n = log2(n);

  vector<vector<long long>> next(log_n + 1, vector<long long>(n + 1));

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

  for (int k = 0; k < log_n; k++) {
    for (int i = 1; i <= n; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]]; // 2^k乗先を求める
      }
    }
  }

  long long p = 1;
  for (long long j = log_n - 1; j >= 1; j--) {
    if (p == -1) {
      break;
    }

    if ((k >> p) & 1) {
      p = next[k][p];
    }
  }

  printf("%lld\\n", p);

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

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

using ll = long long;
using Graph = vector<vector<int>>;

const int INF = 1001001001;
const ll INF_LL = 1001001001001001001LL;

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

  vector<int> dist(n + 1, -1);
  vector<int> vp;

  int v = 1;
  while (dist[v] == -1) {
    dist[v] = vp.size();
    vp.push_back(v);
    v = a[v - 1];
  }

  int p = vp.size() - dist[v];
  int length = dist[v];

  if (k < length)
    cout << vp[k] << endl;
  else {
    k -= (ll)length;
    k %= (ll)p;

    cout << vp[length + k] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
using Graph = vector<vector<int>>;

int main(void) {
  ll n, k;
  cin >> n >> k;
  // vector<int> a(n+1);
  // for(int i=1;i<=n;i++) cin >> a[i];
  Graph g(n + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    g[i].push_back(a);
  }
  vector<int> goal;
  /*
  cout << "g: ";
  for(int i=1;i<=n;i++){
    cout << g[i][0] << " ";
  }
  cout << endl;
  */
  // for(int i=1;i<=n;i++){
  int to = g[1][0];
  int counter = 0;
  do {
    counter++;
    goal.push_back(g[to][0]);
    auto it = goal.end() - 1;
    to = *it;
    // goal[i]=g[to][0];
    if (counter != 1) {
      if (goal[0] == *it) {
        goal.erase(goal.end() - 1);
        goto endloop;
      }
    }
  } while (1);

endloop:
  /*  cout << "goal: ";
  for(auto it = goal.begin();it!=goal.end();it++) cout << *it << " ";
  cout << endl;*/
  if (k > goal.size() - 1)
    k %= (goal.size() - 1);
  // cout << k << " " << goal.size() << " " << goal[k-1] << endl;
  cout << goal[k - 1] << endl;

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

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

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

  vector<int> seen(n + 1, -1);

  vector<ll> a(n + 1, 0);

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

  ll first = 1;
  ll now = 1;
  ll cnt = 0;
  vector<ll> place;

  bool re = false;
  int roop;
  while (!re) {
    cnt++;
    now = a[now];
    // cout << now << endl;
    if (seen[now] != -1) {
      re = true;
      break;
    }
    seen[now] = cnt - 1;
    place.push_back(now);
  }

  //  cout << cnt << endl;

  /*   for(int i = 0; i < place.size(); i++){
         cout << place[i] << endl;
     }
 */
  roop = seen[now];
  //    cout << roop << endl;

  if (k >= place.size()) {
    ll num = (k - roop - 1) % (place.size() - roop);
    cout << place[roop + num];
  } else {
    ll num = (k - 1) % cnt;
    cout << place[num];
  }
}

Error: TLE

Fixed submission:

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

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

  vector<ll> a(n + 1, 0);

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

  ll first = 1;
  ll now = 1;
  ll cnt = 0;
  vector<ll> place;

  bool re = false;
  while (!re) {
    place.push_back(now);
    cnt++;
    now = a[now];
    if (now == first)
      re = true;
  }

  ll num = k % cnt;
  cout << place[num] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  long long K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll v = 1;
  vector<ll> s;              // 辿る辺の順番
  vector<ll> ord(N + 1, -1); // 点vに何番目に来たか
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = A[v - 1];
    // cout<<"v:"<<v<<endl;
  }
  ll cir = s.size() - ord[v]; // 周期のサイズ
  // cout<<s.size()<<endl;
  // cout<<ord[v]<<endl;
  // cout<<"cir:"<<cir<<endl;
  ll re = ord[v]; // 例外のサイズ
  // cout<<re<<endl;
  if (K < re)
    cout << s[K] << endl;
  else {
    K -= re;
    K %= cir;
    // cout<<tmp<<endl;
    // cout<<re<<endl;
    cout << s[re + K] << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  long long K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll v = 1;
  vector<ll> s;              // 辿る辺の順番
  vector<ll> cnt(N + 1, -1); // 点vに何番目に来たか
  while (cnt[v] == -1) {
    s.push_back(v);
    cnt[v] = s.size();
    v = A[v - 1];
    // cout<<"v:"<<v<<endl;
  }
  ll cir = s.size() - cnt[v]; // 周期のサイズ
  // cout<<s.size()<<endl;
  // cout<<cnt[v]<<endl;
  // cout<<"cir:"<<cir<<endl;
  ll re = cnt[v]; // 例外のサイズ
  // cout<<re<<endl;
  if (K < re)
    cout << s[K] << endl;
  else {
    ll tmp = (K - re) % cir;
    // cout<<tmp<<endl;
    // cout<<re<<endl;
    cout << s[re + tmp] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define INCANT                                                                 \\
  cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define out(a) cout << a << endl
#define PRINT(V)                                                               \\
  for (auto v : (V))                                                           \\
  cout << v << " "

const int MOD = 1000000007;
const double PI = acos(-1.0);

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B;
  vector<int> C(N, -1);
  rep(i, N) {
    int ak;
    cin >> ak;
    A.at(i) = ak - 1;
  }
  int node = 0;
  ll i = 0;
  B.push_back(node);
  C[node] = i;
  ll init;
  ll loop;
  while (i < K) {
    node = A.at(node);
    i++;
    if (C[node] > -1) {
      init = C[node];
      loop = i - C[node];
      break;
    } else {
      C[node] = i;
      B.push_back(node);
    }
  }
  if (i == K)
    out(node + 1);
  else
    out(B.at(init + ((K - init) % loop)) + 1);
}
int main() {
  INCANT;
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

#define INCANT                                                                 \\
  cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define out(a) cout << a << endl
#define PRINT(V)                                                               \\
  for (auto v : (V))                                                           \\
  cout << v << " "

const int MOD = 1000000007;
const double PI = acos(-1.0);

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B;
  vector<int> C(N, -1);
  rep(i, N) {
    int ak;
    cin >> ak;
    A.at(i) = ak - 1;
  }
  int node = 0;
  ll i = 0;
  B.push_back(node);
  C[node] = i;
  ll init;
  ll loop;
  while (i < K) {
    node = A.at(node);
    i++;
    if (C[node] > -1) {
      init = C[node];
      loop = i - C[node];
      break;
    } else {
      C[node] = i;
      B.push_back(node);
    }
  }
  if (i == K - 1)
    out(C[node] + 1);
  else
    out(B.at(init + ((K - init) % loop)) + 1);
}
int main() {
  INCANT;
  solve();
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long n, k;
vector<int> a(200005);
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> s;
  vector<int> ord(n + 1, -1);
  int v = 1;
  int c, l;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = a[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l) {
    cout << s[k];
  } else {
    k -= l;
    k %= c;
    cout << s[k + l];
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
long long k;
set<int> s;
int a[200010];
int main() {
  cin >> n;
  cin >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int j = a[1];
  s.insert(1);
  vector<int> v;
  v.push_back(1);
  v.push_back(j);
  int more;
  while (1) {
    s.insert(j);
    j = a[j];
    v.push_back(j);
    if (s.find(j) != s.end()) {
      more = j;
      break;
    }
  }
  long long num = 0;
  while (v[0] != more && more != 1)
    v.erase(v.begin()), num++;
  v.erase(v.begin());
  k -= num;
  /*
  for(int i=0;i<v.size();i++){
          cout<<v[i]<<" ";
  }
  cout<<endl;
  cout<<k<<endl;
  */
  if ((long long)v.size() >= k) {
    cout << v[k - 1] << endl;
  }
  if (k % (long long)v.size() == 0) {
    cout << v[v.size() - 1] << endl;
  } else
    cout << v[k % (long long)(v.size()) - 1] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <vector>

using ll = long long;
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i + 1];
  struct Data {
    ll offset;
    ll cycle;
  };
  map<ll, Data> m;

  ll now = 1;
  for (ll i = 0; i < k; i++) {
    if (m.count(now) == 0) {
      m[now].offset = i;
    } else {
      m[now].cycle = i - m[now].offset;
      break;
    }
    now = a[now];
  }

  ll ans = now;
  if (m[now].cycle > 0) {
    k -= m[now].offset;
    k %= m[now].cycle;
    while (k > 0) {
      ans = a[ans];
      k--;
    }
  }

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

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using ll = long long;
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i + 1];
  struct Data {
    ll offset;
    ll cycle;
  };
  map<ll, Data> m;

  ll now = 1;
  for (ll i = 0; i < k; i++) {
    if (m.count(now) == 0) {
      m[now].offset = i;
    } else {
      m[now].cycle = i - m[now].offset;
      break;
    }
    now = a[now];
  }
  k -= m[now].offset;
  k %= m[now].cycle;
  ll ans = now;
  while (k > 0) {
    ans = a[ans];
    k--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 2LL << 60;
using ll = long long;
using P = pair<ll, ll>;

int main(int argc, char const *argv[]) {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n, -1);
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> t;
  ll p = 0, np;
  ll rs;
  bool flg = true;
  t.push_back(p);
  int j;
  for (j = 0; j < k; j++) {
    np = a.at(p);
    if (v.at(p) != -1) {
      rs = v.at(p);
      flg = false;
      break;
    }
    v.at(p) = j;
    t.push_back(np);
    p = np;
  }
  if (flg) {
    cout << t.back() + 1 << endl;
    return 0;
  }
  ll rp = t.size() - rs - 1;
  k -= rs;
  k %= rp;
  cout << t.at(rs + k) + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 2LL << 60;
using ll = long long;
using P = pair<ll, ll>;

int main(int argc, char const *argv[]) {
  ll n, k;
  map<int, int> mp;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    mp[i] = 0;
  }
  vector<ll> t;
  ll p = 0, np;
  ll rs;
  bool flg = true;
  t.push_back(p);
  int j;
  for (j = 0; j < k; j++) {
    np = a[p];
    if (mp[p] != 0) {
      rep(i, t.size()) {
        if (np == t[i]) {
          rs = i;
          flg = false;
          break;
        }
      }
    }
    mp[p] = np;
    t.push_back(np);
    p = np;
    if (!flg) {
      break;
    }
  }
  if (flg) {
    cout << t.back() + 1 << endl;
    return 0;
  }
  ll rp = t.size() - rs - 1;

  k -= rs;
  k %= rp;
  cout << t[rs + k] + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  long long int 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;
  int l;

  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;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {

  int n, nv;
  long long int k;

  cin >> n >> k;

  int A[n];

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

  vector<int> dist(n + 1, -1);
  vector<int> c;

  bool b = true;
  int count = 1;
  long long int counta;

  int v = 1;
  dist[v] = 0;
  c.push_back(v);

  while (b) {
    nv = A[v];
    if (dist[nv] == -1) {
      dist[nv] = dist[v] + 1;
      c.push_back(nv);
      v = A[v];
    } else {
      b = false;
      counta = dist[nv];
    }
    count++;
  }

  int roop = count - counta;

  k = k - counta + 1;
  k %= roop;

  cout << c[k] << endl;

  return 0;
}