Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define cina(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define couta(a, n)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";
#define fauto(ar)                                                              \\
  for (auto x : ar)                                                            \\
    cout << x << " ";
#define mod 1000000007
#define siz 62
#define f first
#define s second
#define pb push_back
#define endl "\\n"
#define ci cin.ignore()
using namespace std;
int main() {
  ll n, k, i, j;
  cin >> n >> k;
  ll a[n], dp[n + 1][siz];
  memset(dp, -1, sizeof(dp));

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

  for (j = 0; j < siz - 1; j++) {
    for (i = 1; i <= n; i++) {
      dp[i][j + 1] = dp[dp[i][j]][j];
    }
  }

  //	for(i = 1;i<=n;i++)
  //	{
  //		cerr<<i<<" -> ";
  //		for(j = 0;j<8;j++)
  //		cerr<<dp[i][j]<<" ";
  //		cerr<<endl;
  //	}

  i = 1;
  for (j = siz - 1; j >= 0; j--) {
    if ((1LL << j) & k) {
      //			cerr<<i<<" ,"<<j<<endl;
      i = dp[i][j];
    }
  }

  //	cerr<<"\\n\\n";
  cout << i;
}

Error: -6
aed4147e-8344-4b92-9cdb-d9fa07e59fbd.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02684/C++/s872286923.cpp:60: int main(): Assertion `0' failed.


Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define cina(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define couta(a, n)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";
#define fauto(ar)                                                              \\
  for (auto x : ar)                                                            \\
    cout << x << " ";
#define mod 1000000007
#define siz 1005
#define f first
#define s second
#define pb push_back
#define endl "\\n"
#define ci cin.ignore()
using namespace std;
int main() {
  ll n, m, i, k, j, chain = 0;
  cin >> n >> k;
  ll a[n + 1];
  for (i = 1; i <= n; i++)
    cin >> a[i];

  vector<ll> graph[n + 1];
  bool visited[n + 1], infinite = 0;
  memset(visited, 0, sizeof(visited));

  i = 1;

  while (!visited[i]) {

    //		graph[a[i]].pb(a[a[i]]);
    visited[i] = 1;
    chain++;
    i = a[a[i]];
    if (visited[i])
      infinite = 1;
  }

  if (!infinite || k < chain) {
    k = k % chain;

    ll i = 1, j = 1;
    for (i = 1; i <= k; i++)
      j = a[j];

    cout << j << endl;

  } else {
    assert(0);
    // ho jayega
    //		cerr<<"inifinte";
    unordered_map<ll, ll> poss;
    i = 1;
    j = 1;
    ll occur = -1;

    while (1) {
      //			cerr<<a[i]<<" "<<i<<endl;
      if (poss[a[i]]) {
        occur = a[i];
        break;
      }
      poss[a[i]] = 1;
      i = a[i];
    }

    //		assert(occur != -1);

    vector<ll> second;
    //		cerr<<occur<<" occ\\n";
    i = occur;

    second.pb(occur);
    poss[occur]--;

    while (poss[a[i]]) {
      second.pb(a[i]);
      poss[a[i]]--;
      i = a[i];
    }
    //		for(auto x : second)
    //		cerr<<x<<" -> ";

    k = k - chain + second.size();
    k--;
    chain = second.size();

    k = k % chain;
    if (k < 0)
      k += chain;
    //		cerr<<k<<" c "<<chain<<" k\\n";

    j = second[k];

    cout << j << endl;
  }
}