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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

vec d;
ll memo[200010];
bool used[200010];
ll Loop;
ll Solve(ll x, ll K) {

  // cout<<K<<' '<<memo[x]<<' '<<x<<endl;
  if (K == 0)
    return x;
  if (!used[x]) {
    memo[x] = K;
    used[x] = true;
    return Solve(d[x], K - 1);
  }
  if (K >= memo[x] - K) {
    Loop = memo[x] - K;
    return Solve(x, K % Loop);
  }
  return Solve(d[x], K - 1);
}
void init() {
  rep(i, 200010) memo[i] = INF;
  rep(i, 200010) used[i] = false;
  return;
}

int main() {
  init();
  ll N, K;
  cin >> N >> K;
  rep(i, N) {
    ll x;
    cin >> x;
    x--;
    d.pb(x);
  }

  cout << Solve(0, K) + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

vec d;
ll memo[200010];
bool used[200010];
ll Loop;
ll Solve(ll x, ll K) {

  // cout<<K<<' '<<memo[x]<<' '<<x<<endl;
  if (K == 0)
    return x;
  if (!used[x]) {
    memo[x] = K;
    used[x] = true;
    return Solve(d[x], K - 1);
  }
  if (K >= memo[x] - K) {
    Loop = memo[x] - K;
    return Solve(x, K % Loop);
  }
  return Solve(d[x], K - 1);
}
void init() {
  rep(i, 200010) memo[i] = INF;
  rep(i, 200010) used[i] = false;
  return;
}

int main() {
  init();
  ll N, K;
  cin >> N >> K;
  rep(i, N) {
    ll x;
    cin >> x;
    x--;
    d.pb(x);
  }
  Solve(0, 300000);
  // cout<<Loop<<endl;
  init();
  ll P = (N / Loop);
  P++;
  P *= Loop;
  K %= Loop;
  K += P;
  cout << Solve(0, K) + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*
        Auther:_Destiny
        time:2020.5.10
*/
#include <bits/stdc++.h>
#define ll long long
#define N 200010
#define M 1010

using namespace std;
ll n, k, a[N], last[N];

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

  last[1] = 1;
  ll now = 1;
  while (k) {
    if (last[a[now]]) {
      ll len = last[now] + 1 - last[a[now]];
      k %= len;
      if (!k)
        break;
      k--;
      now = a[now];
      while (k > 0) {
        now = a[now];
        k--;
      }
    }
    if (!k)
      break;
    last[a[now]] = last[now] + 1;
    now = a[now];
    k--;
  }
  cout << now << "\\n";
}

Error: -8

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define MAXN 200001
#define int long long

inline void read(int &T) {
  int x = 0;
  bool f = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = !f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  T = f ? -x : x;
}

int n, a[MAXN], last[MAXN], k;

signed main() {
  read(n), read(k);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
  last[1] = 1;
  int now = 1;
  while (k) {
    if (last[a[now]]) {
      int times = last[now] + 1 - last[a[now]];
      k %= times, --k;
      now = a[now];
      while (k > 0) {
        now = a[now];
        --k;
      }
    }
    if (!k)
      break;
    last[a[now]] = last[now] + 1;
    now = a[now];
    --k;
  }
  printf("%lld\\n", now);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long a[201000];
long long biao[201000] = {0};
long long n, k;
long long cishu = 0;
long long mo = 0;
bool flag = false;

int main() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  biao[1] = 1;
  long long finish = 1;
  long long begin = finish;
  while (1) {
    // 当前位置
    if (cishu >= k) {
      flag = true;
      break;
    }
    if (biao[a[begin]] != 0) {
      break;
    }
    biao[a[begin]] = biao[begin] + 1;
    cishu++;
    begin = a[begin];
  }
  finish = begin;
  if (flag) {
    cout << finish << endl;
  } else {
    // cout<<biao[finish]-biao[a[finish]]+1;
    mo = (k - biao[finish] + 1) % (biao[finish] - biao[a[finish]] + 1);
    // cout << (biao[finish] - biao[a[finish]] + 1) << endl;
    cishu = 0;
    while (1) {
      if (cishu >= mo) {
        break;
      }
      cishu++;
      finish = a[finish];
    }
    cout << finish << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[201000];
long long biao[201000] = {0};
long long n, k;
long long cishu = 0;
long long mo = 0;
bool flag = false;

long long jieguo(long long begin) {
  if (cishu >= mo) {
    return begin;
  }
  cishu++;
  jieguo(a[begin]);
}

int main() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  biao[1] = 1;
  long long finish = 1;
  long long begin = finish;
  while (1) {
    // 当前位置
    if (cishu >= k) {
      flag = true;
      break;
    }
    if (biao[a[begin]] != 0) {
      break;
    }
    biao[a[begin]] = biao[begin] + 1;
    cishu++;
    begin = a[begin];
  }
  finish = begin;
  if (flag) {
    cout << finish << endl;
  } else {
    // cout<<biao[finish]-biao[a[finish]]+1;
    mo = (k - biao[finish] + 1) % (biao[finish] - biao[a[finish]] + 1);
    // cout << (biao[finish] - biao[a[finish]] + 1) << endl;
    cishu = 0;
    cout << jieguo(finish) << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  int box[60][200000 + 1] = {0};
  rep(i, N) {
    cin >> A[i];
    box[0][i + 1] = A[i];
  }
  int ans = 1;

  for (int i = 1; i < 60; ++i) {
    for (int j = 1; j < N + 1; ++j) {
      box[i][j] = box[i - 1][box[i - 1][j]];
    }
  }

  for (int i = 0; i < 60; ++i) {
    if (K % 2) {
      ans = box[i][ans];
      if (K == 1)
        break;
    }
    K = K / 2;
  }

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


Error: TLE

Fixed submission:

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  vector<bool> flag(N + 1, false);
  vector<int> search;
  int first = 0;
  int now = 1;
  flag[now] = true;
  rep(i, N) cin >> A[i];
  rep(i, K) {
    now = A[now - 1];

    if (now == A[now - 1])
      break;
    if (flag[now]) {
      if (search.empty())
        first = now;
      else if (first == now) {
        now = search[(K - (i + 1)) % search.size()];
        break;
      } else {
        search.push_back(now);
      }
    } else {
      flag[now] = true;
    }
  }
  cout << now << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {

  int N;
  long long K;
  cin >> N >> K;
  int A;
  map<int, int> mpA;
  for (int i = 0; i < N; i++) {
    cin >> A;
    A--;
    mpA[i] = A;
  }

  vector<int> vec;
  map<int, int> mpF;
  int mrk;
  map<int, int> mpC;

  int cont = 0;
  int fr = 0;
  int to = mpA[0];
  vec.push_back(0);
  mpF[0]++;
  mpC[0] = 0;
  while (true) {

    if (mpF[to] != 0) {
      // cout << to <<endl;
      mrk = to;
      break;
    }
    vec.push_back(to);
    mpF[to]++;
    cont++;
    mpC[to] = cont;

    fr = to;
    to = mpA[to];
  }

  // cout << mrk <<endl;
  // cout << mpC[mrk] <<endl;
  // cout << cont <<endl;
  vector<int> V = vec;

  vec.erase(vec.begin(), vec.begin() + mpC[mrk]);

  // cout << (K- mpC[mrk] )%vec.size() <<endl;

  if (K > mpC[mrk])
    cout << vec[(K - mpC[mrk]) % vec.size()] + 1 << endl;
  else
    cout << V[K] + 1 << endl;

  // for(auto u:vec){
  //     cout << u + 1;
  // }
}


Error: 0

Fixed submission:

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

int main(void) {

  int N;
  long long K;
  cin >> N >> K;
  int A[N];
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    // cout << i << A[i]<<endl;
    mp[i] = A[i];
  }
  // cout << mp[1]<<endl;

  vector<int> memo;
  int index = 0;
  int cont = 0;
  memo.push_back(index);
  bool flag = true;
  int loopindex = 0;
  while (flag) {

    index = mp[index];

    for (int i = 0; i < memo.size(); i++) {

      if (memo[i] == index) {
        loopindex = i;
        flag = false;
        break;
      }
    }
    // cout << flag<<endl;
    // if(!flag)cont++;

    // cout << "memo[ans]"<<endl;
    memo.push_back(index);
    cont++;
  }
  // cout << loopindex<< endl;
  // cout << cont-loopindex << endl;
  long long ans = (K - cont) % (cont - loopindex + 1);
  cout << memo[ans] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[0]++;
  ll key = 0;
  ll time = 0;
  ll loop = 0;
  vector<ll> vec;
  vector<ll> vec0;
  vec0.push_back(0);
  rep(i, 2 * n + 10) {
    mp[a[key] - 1]++;
    if (mp[a[key] - 1] == 1) {
      time++;
      vec0.push_back(a[key]);
    }
    if (mp[a[key] - 1] == 2) {
      loop++;
      vec.push_back(a[key]);
    }
    key = a[key] - 1;
  }
  if (k > time && (k - time) % loop != 0)
    cout << vec[(k - time) % loop - 1] << endl;
  else if (k > time && (k - time) % loop == 0)
    cout << vec[loop - 1] << endl;
  else
    cout << vec0[k] << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[0]++;
  ll key = 0;
  ll time = 0;
  ll loop = 0;
  vector<ll> vec;
  vector<ll> vec0;
  vec0.push_back(0);
  rep(i, 2 * n + 10) {
    mp[a[key] - 1]++;
    if (mp[a[key] - 1] == 1) {
      time++;
      vec0.push_back(a[key]);
    }
    if (mp[a[key] - 1] == 2) {
      loop++;
      vec.push_back(a[key]);
    }
    key = a[key] - 1;
  }
  if (k > time && (k - time) % loop != 0)
    cout << vec[(k - time) % loop - 1] << endl;
  if (k > time && (k - time) % loop == 0)
    cout << vec[(k - time) % loop - 1 + loop] << endl;
  else
    cout << vec0[k] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> t;

// map <pair <int, long long int>, int> dp;

// int successor(int x, long long int k)
// {
//     if (k == 1)
//         return a[x];

//     pair <int, long long int> p = make_pair(x, k);

//     if (dp.find(p) == dp.end())
//     {
//         if (k % 2)
//             dp[p] = successor(a[x], k-1);
//         else
//             dp[p] = successor(successor(x, k/2), k/2);
//     }

//     return dp[p];
// }

// int successor(int x, long long int k)
// {
//     if (k == 1)
//         return a[x];

//     return successor(successor(x, k/2), k/2);
// }

// void precalc()
// {
//     for (int i = 1; i <= n; i++)
//     {
//         for (int j = 0; j < 22; j++)
//         {
//             dp[make_pair(i, (1LL << j))] = successor(i, (1LL << j));
//         }
//     }
// }

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

  long long int k;

  cin >> n >> k;

  t.assign(n + 1, 0);

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

  int x = 1;

  int a = t[x];
  int b = t[t[x]];

  while (a != b) {
    a = t[a];
    b = t[t[b]];
  }

  a = x;

  while (a != b) {
    a = t[a];
    b = t[b];
  }

  int first = a;

  b = t[a];
  int len = 1;

  while (a != b) {
    b = t[b];
    len++;
  }

  // cout << first << " " << len << "\\n";

  a = 1;

  while (k > 0 && a != first) {
    a = t[a];
    k--;
  }

  k = k % len;

  while (k) {
    k--;
    a = t[a];
  }

  cout << a << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> a;

map<pair<int, long long int>, int> dp;

int successor(int x, long long int k) {
  if (k == 1)
    return a[x];

  pair<int, long long int> p = make_pair(x, k);

  if (dp.find(p) == dp.end()) {
    if (k % 2)
      dp[p] = successor(a[x], k - 1);
    else
      dp[p] = successor(successor(x, k / 2), k / 2);
  }

  return dp[p];
}

int main() {
  long long int k;

  cin >> n >> k;

  a.assign(n + 1, 0);

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

  cout << successor(1, k) << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  map<int, int> mp;
  int now = 0;
  int st = 0, len = 0, z = 0;
  vector<int> p(500005);
  rep(i, 500005) {
    now = a[now];
    p[i] = now;
    if (mp.count(now) > 0 and len == 0) {
      z = mp[now], st = now, len = i + 1 - mp[now];
    } else
      mp[now] = i + 1;
  }
  if (k < 300005)
    cout << p[k - 1] + 1 << endl;
  else {
    int pt = (k - z + 1) % len;
    if (pt == 0)
      pt = len;
    cout << p[pt + z - 2] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int now = 0;
  vector<int> p(10000);
  rep(i, 10000) {
    now = a[now];
    p[i] = now;
  }
  if (k < 10000)
    cout << p[k - 1] + 1 << endl;
  else
    cout << p[(k - 1000) % n + 1000 - 1] + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <typename T, typename U> static void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static void chmax(T &x, U y) {
  if (x < y)
    x = y;
}

int a[200003];
int b[200003];
signed main() {
  ll n, k;
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll counter = 1;
  unordered_map<int, ll> ss; // place, count
  ss[0] = 0;
  // b[0]=0;
  int i = a[0];
  b[1] = a[0];
  while (ss.find(i) == ss.end()) {
    // cerr<<(*ss.find(i)).second<<endl;
    ss[i] = counter;
    counter++;
    i = a[i];
    b[counter] = i;
  }
  ll into_loop = ss[i];
  // cerr<<into_loop<<endl;
  // cerr<<counter<<endl;
  ll loop = counter - into_loop;
  // cerr<<loop<<endl;
  // cerr<<counter;
  if (k < into_loop) {
    ll ans = b[k];
    cout << (ans + 1);
    return 0;
  }
  k -= into_loop;
  k %= loop;
  ll ans = b[into_loop + k];
  cout << (ans + 1);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <typename T, typename U> static void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static void chmax(T &x, U y) {
  if (x < y)
    x = y;
}

int a[200003];
signed main() {
  ll n, k;
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll counter = 1;
  unordered_set<int> ss;
  ss.insert(0);
  ss.insert(a[0]);
  int i = a[0];
  while (ss.find(i) != ss.end()) {
    counter++;
    i = a[i];
    ss.insert(a[i]);
  }
  int goal = i;
  ll t = 1;
  i = a[i];
  while (i != goal) {
    t++;
    i = a[i];
  }
  // cerr<<counter;
  k -= counter;
  k %= t;
  i = goal;
  rep(j, k) i = a[i];
  cout << (i + 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
template <typename T> bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a >= b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  int n;
  ll k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int oks[n];
  for (int i = 0; i < n; i++)
    oks[i] = -1;
  int now = 0;
  for (int i = 0; i < k; i++) {
    if (oks[now] != -1) {
      k -= (oks[now]);
      k %= (i - oks[now]);
      i = -1;
      for (int i = 0; i < n; i++)
        oks[i] = -1;
      // cout << now << " " << k << endl;
      continue;
    }
    oks[now] = i;
    now = a[now];
  }
  cout << now + 1 << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<bool> ok(n, false);
  for (int i = 0; i < k; i++) {
    int d;
    cin >> d;
    for (int j = 0; j < d; j++) {
      int a;
      cin >> a;
      ok[a - 1] = true;
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (!ok[i]) {
      sum++;
    }
  }
  cout << sum << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

const ll N = 200001;
ll a[N], vis[N];
ll ccs, pl;
vector<ll> seq;

void dfs(ll u, ll p = 0) {
  vis[u] = vis[p] + 1;
  seq.pb(u);
  ll v = a[u];
  if (!vis[v])
    dfs(v, u);
  else
    ccs = vis[u] + 1 - vis[v];
}

void solve() {
  ll n, x;
  cin >> n >> x;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  pl = seq.size() - ccs;
  // cout << pl << ccs << endl;
  if (x < seq.size())
    cout << seq[x] << endl;
  else {
    ll tot = x - pl;
    tot %= ccs;
    cout << seq[pl + tot] << endl;
  }
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

// WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

const ll N = 100001;
ll a[N], vis[N];
ll ccs, pl;
vector<ll> seq;

void dfs(ll u, ll p = 0) {
  vis[u] = vis[p] + 1;
  seq.pb(u);
  ll v = a[u];
  if (!vis[v])
    dfs(v, u);
  else
    ccs = vis[u] + 1 - vis[v];
}

void solve() {
  ll n, x;
  cin >> n >> x;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  pl = seq.size() - ccs;
  // cout << pl << ccs << endl;
  if (x < seq.size())
    cout << seq[x] << endl;
  else {
    ll tot = x - pl;
    tot %= ccs;
    cout << seq[pl + tot] << endl;
  }
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  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 INF 1e9
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << x << endl;

using namespace std;
typedef long long ll;

signed main() {
  ll n, k, next = 0, mod = -1, cycle;
  cin >> n >> k;
  ll a[n], flags[n];
  REP(i, n) {
    cin >> a[i];
    flags[i] = -1;
  }
  vector<int> visited, t;

  if (k <= n) {
    REP(i, k) { next = a[next] - 1; }
    cout << next + 1 << endl;
  } else {
    REP(i, n) {
      if (flags[next] != -1) {
        cycle = visited.size() - flags[next];
        mod = (k - flags[next]) % cycle;
        for (int j = flags[next]; j < flags[next] + cycle; j++) {
          t.push_back(visited[j]);
        }
        break;
      } else {
        visited.push_back(next);
        flags[next] = i;
        next = a[next] - 1;
      }
    }

    if (mod == -1) {
      cout << 1 << endl;
    } else {
      cout << t[mod] + 1 << endl;
    }
  }

  return 0;
}


Error: 0

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 INF 1e9
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << x << endl;

using namespace std;
typedef long long ll;

signed main() {
  ll n, k, next = 0, mod = -1, cycle;
  cin >> n >> k;
  ll a[n], flags[n];
  REP(i, n) {
    cin >> a[i];
    flags[i] = -1;
  }
  vector<int> visited, t;
  REP(i, n) {
    if (flags[next] != -1) {
      cycle = visited.size() - flags[next];
      mod = (k - flags[next]) % cycle;
      for (int j = flags[next]; j < flags[next] + cycle; j++) {
        t.push_back(visited[j]);
      }
      break;
    } else {
      visited.push_back(next);
      flags[next] = i;
      next = a[next] - 1;
    }
  }

  if (mod == -1) {
    cout << 1 << endl;
  } else {
    cout << t[mod] + 1 << endl;
  }
  return 0;
}