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

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

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

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &x : A) {
    cin >> x;
    x--;
  }

  ll s;
  vector<int> used(N, 0);
  ll pnt = 0;
  while (!used.at(pnt)) {
    used.at(pnt) = 1;
    pnt = A.at(pnt);
  }
  s = pnt;
  pnt = 0;
  ll tos = 0;
  vector<ll> U;
  while (pnt != s) {
    U.push_back(pnt);
    pnt = A.at(pnt);
    tos++;
  }
  vector<ll> V;
  V.push_back(s);
  pnt = s;
  while (A.at(pnt) != s) {
    V.push_back(A.at(pnt));
    pnt = A.at(pnt);
  }
  ll len = V.size();

  if (K < tos) {
    cout << U.at(K) + 1 << endl;
  } else {
    cout << V.at((K - tos) % len) + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

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

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &x : A) {
    cin >> x;
    x--;
  }

  int s;
  vector<int> used(N, 0);
  int pnt = 0;
  while (!used.at(pnt)) {
    used.at(pnt) = 1;
    pnt = A.at(pnt);
  }
  s = pnt;
  pnt = 0;
  int tos = 0;
  while (pnt != s) {
    pnt = A.at(pnt);
    tos++;
  }
  vector<ll> V;
  V.push_back(s);
  pnt = s;
  while (A.at(pnt) != s) {
    V.push_back(A.at(pnt));
    pnt = A.at(pnt);
  }
  ll len = V.size();

  cout << V.at((K - tos) % len) + 1 << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define endl "\\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll sum = 0;
int deep[200000 + 10];
bool vis[200000 + 10];
int res[200000 + 10];
int a[200000 + 10];
ll len = 0;
int dfs(int i) {
  vis[i] = true;
  sum++;
  deep[i] = sum;
  res[sum] = i;
  if (!vis[a[i]])
    return dfs(a[i]);
  else
    return sum - deep[a[i]] + 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  ll k;
  cin >> n >> k;
  k++;
  rep(i, 1, n + 1) cin >> a[i];
  len = dfs(1);
  if (k <= sum) {
    cout << res[k] << endl;
  } else {
    int last = sum - len;
    k -= last;
    k %= len;
    //	cout << sum <<" " << len <<" " << k <<endl;
    if (k == 0)
      cout << res[sum] << endl;
    else
      cout << res[last + k] << endl;
  }
  return 0;
}
/*
6 8
6 5 2 5 3 2
*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
#define endl "\\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll sum = 0;
int deep[200000 + 10];
bool vis[200000 + 10];
int res[200000 + 10];
int a[200000 + 10];
int len = 0;
int dfs(int i) {
  vis[i] = true;
  sum++;
  deep[i] = sum;
  res[sum] = i;
  if (!vis[a[i]])
    dfs(a[i]);
  else
    return sum - deep[a[i]] + 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  ll k;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> a[i];
  len = dfs(1);
  if (k <= len) {
    cout << res[k] << endl;
  } else {
    int last = sum - len;
    k -= last;
    k %= len;
    // cout << sum <<" " << len <<" " << k <<endl;
    cout << res[last + k + 1] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> path;
  unordered_set<int> s;
  j = 1;
  for (i = 0; i <= k; i++) {
    if (s.find(j) == s.end()) {
      path.push_back(j);
      s.insert(j);
      j = a[j - 1];
    } else
      break;
  }
  for (i = 0; i < path.size(); i++) {
    if (path[i] == j) {
      break;
    }
    sum++;
  }
  if (sum == path.size()) {
    cout << path[path.size() - 1];
  } else {
    temp1 = k - sum;
    cout << path[sum + (temp1 % (path.size() - sum))];
  }
}

Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> path;
  unordered_set<int> s;
  j = 1;
  for (i = 0; i <= k; i++) {
    if (s.find(j) == s.end()) {
      path.push_back(j);
      s.insert(j);
      j = a[j - 1];
    } else
      break;
  }
  for (i = 0; i < path.size(); i++) {
    if (path[i] == j) {
      break;
    }
    sum++;
  }
  temp1 = k - sum;
  cout << path[sum + (temp1 % (path.size() - sum))];
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, k;
  cin >> n >> k;
  ll g[n];
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    g[i] = a - 1;
  }
  int idx = 0, s = 0, t = 0, cnt = 0;
  vector<ll> v;
  bool nums[n + 1] = {};
  while (1) {
    if (cnt > 0 && nums[idx]) {
      for (int i = 0; i < n; i++) {
        if (idx == v[i]) {
          s = i;
          break;
        }
      }
      t = v.size() - 1;
      break;
    }
    v.push_back(idx);
    nums[idx] = true;
    idx = g[idx];
    cnt++;
  }
  if (k < s) {
    cout << v[k] + 1 << endl;
  } else {
    k -= s;
    k %= (t - s + 1);
    cout << v[s + k] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  ll g[n];
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    g[i] = a - 1;
  }
  int idx = 0, s = 0, t = 0;
  vector<ll> v;
  set<ll> st;
  while (1) {
    if (st.size() > 0 && idx == *lower_bound(st.begin(), st.end(), idx)) {
      for (int i = 0; i < n; i++) {
        if (idx == v[i]) {
          s = i;
          break;
        }
      }
      t = v.size() - 1;
      break;
    }
    v.push_back(idx);
    st.insert(idx);
    idx = g[idx];
  }
  if (k < s) {
    cout << v[k] + 1 << endl;
  } else {
    k -= s;
    k %= (t - s + 1);
    cout << v[s + k] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
vec G[200001], r(200001, 0), r2(200001, 0), r3(200001, 0), A(200001);
ll a, z = 0, k;
void dfs(int x) {
  if (r[x]) {
    a = x;
    return;
  }
  r[x] = 1;
  z++;
  dfs(A[x]);
}
void dfs2(int x) {
  if (x == a)
    return;
  z++;
  dfs2(A[x]);
}
void dfs3(int x) {
  if (z == k) {
    a = x;
    return;
  }
  z++;
  dfs3(A[x]);
}
int main() {
  ll n;
  cin >> n >> k;
  rep(i, n) cin >> A[i + 1];
  dfs(1);
  ll b = z;
  if (k < z) {
    z = 0;
    dfs3(1);
    cout << a;
    return 0;
  }
  z = 0;
  dfs2(1);
  k -= z;
  k %= (b - z);
  z = 0;
  dfs3(a);
  cout << a;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
vec G[200001], r(200001, 0), A(200001);
ll a, z = 0, k;
void dfs(int x) {
  if (r[x]) {
    a = x;
    return;
  }
  r[x] = 1;
  z++;
  dfs(A[x]);
}
void dfs2(int x) {
  if (x == a)
    return;
  z++;
  dfs2(A[x]);
}
void dfs3(int x) {
  if (z == k) {
    a = x;
    return;
  }
  z++;
  dfs3(A[x]);
}
int main() {
  ll n;
  cin >> n >> k;
  rep(i, n) cin >> A[i + 1];
  dfs(1);
  ll b = z;
  if (k < z) {
    dfs3(1);
    cout << a;
    return 0;
  }
  z = 0;
  dfs2(1);
  k -= z;
  k %= (b - z);
  z = 0;
  dfs3(a);
  cout << a;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)

int main() {
  ll n, k, a[200001] = {0};
  cin >> n >> k;
  for (ll i = 1; i <= n; ++i)
    cin >> a[i];
  ll w2, w3, w2Town;
  vector<ll> counter(n + 1);
  for (ll i = 1; i <= n; ++i)
    counter[i] = 0;
  ll nPos = a[1];
  ll w;
  for (w = 1;; ++w) {
    counter[nPos]++;
    if (counter[nPos] > 1) {
      w2 = w;
      w2Town = nPos;
      nPos = a[nPos];
      w++;
      break;
    }
    nPos = a[nPos];
  }
  for (;; ++w) {
    counter[nPos]++;
    if (counter[nPos] > 2) {
      w3 = w;
      break;
    }
    nPos = a[nPos];
  }
  ll loopTownNum = w3 - w2;
  nPos = a[1];
  ll ans = 0;
  vector<ll> tPos(n);
  if (k < w2) {
    for (ll i = 2; i <= k; ++i)
      nPos = a[nPos];
    ans = nPos;
  } else if (k >= w2) {
    nPos = w2Town;
    for (ll i = 0; i < loopTownNum; ++i) {
      tPos[i] = nPos;
      nPos = a[nPos];
    }
    ans = tPos[(k - w2) % loopTownNum];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)

int main() {
  ll n, k, a[200001] = {0};
  cin >> n >> k;
  for (ll i = 1; i <= n; ++i)
    cin >> a[i];
  ll w2, w3, w2Town;
  vector<ll> counter(n + 1);
  for (ll i = 1; i <= n; ++i)
    counter[i] = 0;
  ll nPos = a[1];
  ll w;
  for (w = 1;; ++w) {
    counter[nPos]++;
    if (counter[nPos] > 1) {
      w2 = w;
      w2Town = nPos;
      nPos = a[nPos];
      break;
    }
    nPos = a[nPos];
  }
  for (;; ++w) {
    counter[nPos]++;
    if (counter[nPos] > 2) {
      w3 = w;
      break;
    }
    nPos = a[nPos];
  }
  ll loopTownNum = w3 - w2;
  nPos = a[1];
  ll ans = 0;
  vector<ll> tPos(n + 1);
  if (k < w2) {
    for (ll i = 2; i <= k; ++i)
      nPos = a[nPos];
    ans = nPos;
  } else if (k >= w2) {
    nPos = w2Town;
    for (ll i = 0; i < loopTownNum; ++i) {
      tPos[i] = nPos;
      nPos = a[nPos];
    }
    ans = tPos[(k - w2) % loopTownNum];
  }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// *start on @date: 2020-05-10 21:19
const ll N = 2e5 + 5;
int n, a[N], v[N], cnt, f[N];
ll k;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int x = 1;
  do {
    v[x] = 1;
    if (cnt == k) {
      cout << x;
      return 0;
    }
    f[cnt++] = x;
    x = a[x];
  } while (!v[x]);
  k -= cnt;
  cnt = 0;
  do {
    v[x] = 2;
    f[cnt++] = x;
    x = a[x];
  } while (v[x] != 2);
  cout << f[k % cnt];
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// *start on @date: 2020-05-10 21:19
const ll N = 2e5 + 5;
struct node {
  ll v;
  ll a;
} s[N];
unordered_map<ll, ll> mp;
int main() {
  IOS;
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++)
    cin >> s[i].a, s[i].v = i;
  ll now = 1;
  ll flag = 0;
  vector<ll> ve2;
  ll tt, num = 0;
  while (1) {
    mp[s[now].v]++;
    if (mp[s[now].v] == 1)
      ve2.push_back(s[now].v);
    else if (mp[s[now].v] == 2) {
      mp[s[now].v]--;
      tt = s[now].v;
      break;
    }
    now = s[now].a;
  }
  ll l = ve2.size();
  k -= l;
  while (1) {
    mp[s[tt].v]++;
    num++;
    if (mp[s[tt].v] == 2)
      ve2.push_back(s[tt].v);
    else if (mp[s[tt].v] == 3)
      break;

    tt = s[tt].a;
  }
  // cout<<num<<endl;

  k %= num;
  // cout<<k<<endl;
  cout << ve2[k];
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool vis[200005];
ll tim[200005];
ll ind[2000005];
ll loop = 0;
ll a[200005];
int main() {
  // freopen("in.txt","r",stdin);
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 1;
  vis[1] = true;
  tim[1] = 1;
  ind[1] = 1;
  for (int i = 2; i <= k; i++) {
    ans = a[ans];
    if (vis[ans]) {
      loop = i - tim[ans];
      break;
    } else {
      vis[ans] = true;
      tim[ans] = i;
      ind[i] = ans;
    }
  }
  if (loop == 0) {
    cout << a[ans] << endl;
  } else {
    k -= tim[ans];
    k %= loop;
    cout << a[ind[k + tim[ans]]] << endl;
  }
  // cout<<loop<<endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool vis[100005];
ll tim[100005];
ll ind[1000005];
ll loop = 0;
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 1;
  vis[1] = true;
  tim[1] = 1;
  ind[1] = 1;
  for (int i = 2; i <= k; i++) {
    ans = a[ans];
    if (vis[ans]) {
      loop = i - tim[ans];
      break;
    } else {
      vis[ans] = true;
      tim[ans] = i;
      ind[i] = ans;
    }
  }
  if (loop == 0) {
    cout << a[ans] << endl;
  } else {
    k -= tim[ans];
    k %= loop;
    cout << a[ind[k + tim[ans]]] << endl;
  }
  // cout<<loop<<endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// url:
// problem name: Brick Break

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
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;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

int main() {
  int N;
  cin >> N;
  ll K;
  cin >> K;
  veci A(N);
  vector<bool> seen(N, false);
  int next;
  REP(i, N) {
    int a;
    cin >> a;
    a--;
    A[i] = a;
  }
  int ans;
  int cur = 0;
  if (N >= K) {
    REP(i, K) {
      ans = A[cur];
      cur = A[cur];
    }
  } else {
    int v;

    REP(i, N) {
      v = A[cur];
      cur = A[cur];
    }

    seen[v] = true;
    int cnt = 0;

    while (1) {
      v = A[v];
      cnt++;
      if (seen[v])
        break;
      else
        seen[v] = true;
    }
    cur = v;
    REP(i, (K - N) % cnt) {
      ans = A[cur];
      cur = A[cur];
    }
  }
  cout << ans + 1 << endl;
}

Error: 0

Fixed submission:

// url:
// problem name: Brick Break

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
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;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

int main() {
  int N;
  cin >> N;
  ll K;
  cin >> K;
  veci A(N);
  int next;
  REP(i, N) {
    int a;
    cin >> a;
    a--;
    A[i] = a;
  }
  int ans;
  if (N >= K) {
    REP(i, K) { ans = A[A[i]]; }
  } else {
    ll rest;
    int v = -1;
    REP(i, N) { next = A[A[i]]; }

    ll cnt = 0;

    if (K - N == 1) {
      cout << A[next] + 1 << endl;
      return 0;
    }

    int cur = next;

    /*
            while(v != next) {
                v = A[cur];
                cur = A[cur];
                cnt++;
            }
    */

    rest = (K - N) % cnt;
    REP(i, rest) { next = A[next]; }
    // cout << rest << endl;
    ans = next;
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> loop;
  vector<int> used(n);
  ll t = 0;
  while (1) {
    if (used[t]) {
      break;
    }
    loop.push_back(t);
    used[t] = 1;
    t = a[t];
  }
  ll f = loop.size();
  ll num = 0;
  rep(i, f) {
    if (loop[i] == t) {
      break;
    }
    num++;
  }
  if (k < num) {
    cout << loop[k] + 1 << endl;
    return 0;
  }
  ll div = f - num;
  k = (k - num) % div;
  cout << loop[num + k] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18

int main() {
  ll n;
  ll k;
  cin >> n >> k;
  vector<int> vec(n);
  rep(i, n) {
    int a;
    cin >> a;
    a--;
    vec[i] = a;
  }
  vector<bool> used(n, false);
  int p = 0;
  int l = 0;
  while (1) {
    if (used[p]) {
      break;
    }
    used[p] = true;
    l++;
    p = vec[p];
  }
  vector<int> loop;
  int f = p;
  loop.push_back(f);
  while (1) {
    f = vec[f];
    if (f == p) {
      break;
    }
    loop.push_back(f);
  }
  int nums = 0;
  int t = 0;
  while (1) {
    t = vec[t];
    nums++;
    if (t == p) {
      break;
    }
  }
  if (nums != l) {
    l -= nums;
  }
  ll d = (k - nums) % l;
  cout << loop[d] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int s[200010];
int f[200010];
vector<int> v;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  int tmp = 1;
  int cnt = 0;
  int ans, flag = 0;
  while (1) {
    if (f[tmp] == 2) {
      break;
    } else if (f[tmp] == 1) {
      v.push_back(tmp);
      f[tmp] = 2;
      tmp = s[tmp];
    } else if (f[tmp] == 0) {
      f[tmp] = 1;
      ans = s[tmp];
      tmp = s[tmp];
      cnt++;
      if (k == cnt) {
        flag = 1;
        break;
      }
    }
  }
  if (flag == 1) {
    cout << ans << endl;
    return 0;
  }
  k = k - (cnt - v.size());
  int xxx = (k) % v.size();
  cout << v[xxx];
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
ll k, n, a[maxn], pos, cnt, v[maxn];
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  pos = 1;
  while (k--) {

    pos = a[pos];
    if (v[pos]) {
      k %= (cnt - v[pos]);
    }
    cnt++;
    v[pos] = cnt;
  }
  printf("%lld", pos);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 1000000007;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  rep(i, 1, N + 1) cin >> A.at(i); // 1-index

  vector<ll> path; // i回目にいる場所
  map<ll, ll> done;

  path.push_back(1); // 0回目は1にいる
  done[1]++;

  ll start_v, period;
  bool periodic = false;

  while (1) { // まず周期の最初の数字を求める
    int next = A.at(path.back());
    path.push_back(next);
    done[next]++;
    if (done[next] == 2) {
      start_v = next; // 周期の最初の場所start_vはnext
      break;
    }
  }

  ll start_i; // start_vに最初にたどり着くのはstart_i回目
  for (ll i = 0;; i++) { //
    if (path.at(i) == start_v) {
      start_i = i;
      period = path.size() - start_i - 1;
      break;
    }
  }

  vector<ll> cycle(period);
  for (ll i = 0; i < period; i++) {
    cycle.at(i) = path.at(start_i + i);
  }

  if (K < start_i)
    cout << path.at(K) << endl;
  else {
    ll k = (K - start_i) % period;
    cout << cycle.at(k) << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 1000000007;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  rep(i, 1, N + 1) cin >> A.at(i); // 1-index

  vector<ll> path; // i回目にいる場所
  map<ll, ll> done;

  path.push_back(1); // 0回目は1にいる
  done[1]++;

  ll start_v, period;

  while (1) { // まず周期の最初の数字を求める
    int next = A.at(path.back());
    path.push_back(next);
    done[next]++;
    if (done[next] == 2) {
      start_v = next; // 周期の最初の場所start_vはnext
      break;
    }
  }

  ll start_i; // start_vに最初にたどり着くのはstart_i回目
  for (ll i = 0;; i++) { //
    if (path.at(i) == start_v) {
      start_i = i;
      period = path.size() - start_i - 1;
      break;
    }
  }

  vector<ll> cycle(period);
  for (ll i = 0; i < period; i++) {
    cycle.at(i) = path.at(start_i + i);
  }

  ll k = (K - start_i) % period;

  cout << cycle.at(k) << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> seen(n, -1);
  seen[0] = 0;
  ll now = 0;
  ll p = 1000000;
  ll q = 0;
  rep(i, n + 1) {
    now = a[now];
    if (seen[now] != -1) {
      p = i + 1 - seen[now];
      q = seen[now] + 1;
      break;
    }
    seen[now] = i + 1;
  }

  if (k >= q)
    k = (k - q) % p + q;
  now = 0;
  rep(i, k) { now = a[now]; }
  cout << ++now;
}

Error: WA

Fixed submission:

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

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

  vector<int> seen(n, -1);
  seen[0] = 0;
  int now = 0;
  int p = 1000000;
  int q = 0;
  rep(i, n + 1) {
    now = a[now];
    if (seen[now] != -1) {
      p = i - seen[now];
      q = seen[now] + 1;
      break;
    }
    seen[now] = i;
  }

  k = (k - q) % p + q;
  now = 0;
  rep(i, k) { now = a[now]; }
  cout << ++now;
}