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

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

#define ll long long int
ll mod = 1e9 + 7;

vector<ll> g[200005];
ll vis[200005];
// queue<ll> q;
// ll level[200005];

// void dfs(ll i)
// {
//     vis[i] = 1;
//     for(ll j=0;j<g[i].size();j++)
//     {
//         ll child = g[i][j];
//         if(vis[child] == 0)
//         {
//             dfs(child);
//         }
//         else
//         {
//             // you found a cycle.

//         }

//     }
// }

// void bfs(ll i)
// {
//     vis[i] = 1;
//     while(!q.empty())
//     {
//         ll top = q.front();
//         ll len_adj = g[top].size();
//         ll j;
//         for(j=0;j<len_adj;j++)
//         {
//             ll v = g[top][j];
//             if(vis[v]==0)
//             {
//                 vis[v]=1;
//                 q.push(v);
//                 level[v] = level[top] + 1;
//             }
//         }
//         q.pop();
//     }
// }

ll modular_exponentiation(ll base, ll power) {
  ll ans = 1;
  base = base % mod;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % mod;
    }

    power /= 2;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  /* Faster input ouput */
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);

  /* To read input from a file and output to a file */
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll t = 1;
  // cin >> t;
  while (t--) {
    ll n, i, j, k;
    cin >> n >> k;
    ll arr[n];
    set<ll> s;

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

    vector<ll> path;

    ll curr_ind = 0;
    ll start_cycle;
    for (i = 0; i < n + 1; i++) {

      if (s.find(curr_ind) == s.end()) {
        // not already reached;
        // cout << curr_ind << '\\n';
        s.insert(curr_ind);
        path.push_back(curr_ind);
        curr_ind = arr[curr_ind];
        // g[prev_ind].push_back(curr_ind);
      } else {
        // here is the start of the cycle
        // cout << "YES\\n";
        // cout << curr_ind << '\\n';
        start_cycle = curr_ind;
        break;
      }
    }

    // ll len = path.size();
    // k %= len;
    // for(i=0;i<path.size();i++)
    // {
    //     cout << path[i] << " ";
    // }
    // cout << '\\n';
    // cout << k << '\\n';
    // cout << (path[k] + 1) << '\\n';
    // cout << curr_ind+1 << '\\n';

    // cout << start_cycle << '\\n';

    vector<ll> cycle;
    ll cyc_s_i = 0;

    ll tempk = k;
    for (i = 0; i < path.size(); i++) {
      if (path[i] != start_cycle) {
        k--;
      } else {
        cyc_s_i = i;
        break;
      }
    }

    for (i = cyc_s_i; i < path.size(); i++) {
      cycle.push_back(path[i]);
    }

    // for(i=0;i<cycle.size();i++)
    // {
    //     cout << cycle[i] << " ";
    // }
    // cout << '\\n';

    // cout << tempk << " " << cyc_s_i << '\\n';
    if (tempk < path.size()) {
      cout << (path[tempk] + 1) << '\\n';
      return 0;
    }

    ll len = cycle.size();
    k %= len;
    cout << (cycle[k] + 1) << '\\n';
  }
}

Error: 0

Fixed submission:

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

#define ll long long int
ll mod = 1e9 + 7;

vector<ll> g[200005];
ll vis[200005];
// queue<ll> q;
// ll level[200005];

// void dfs(ll i)
// {
//     vis[i] = 1;
//     for(ll j=0;j<g[i].size();j++)
//     {
//         ll child = g[i][j];
//         if(vis[child] == 0)
//         {
//             dfs(child);
//         }
//         else
//         {
//             // you found a cycle.

//         }

//     }
// }

// void bfs(ll i)
// {
//     vis[i] = 1;
//     while(!q.empty())
//     {
//         ll top = q.front();
//         ll len_adj = g[top].size();
//         ll j;
//         for(j=0;j<len_adj;j++)
//         {
//             ll v = g[top][j];
//             if(vis[v]==0)
//             {
//                 vis[v]=1;
//                 q.push(v);
//                 level[v] = level[top] + 1;
//             }
//         }
//         q.pop();
//     }
// }

ll modular_exponentiation(ll base, ll power) {
  ll ans = 1;
  base = base % mod;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % mod;
    }

    power /= 2;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  /* Faster input ouput */
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);

  /* To read input from a file and output to a file */
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll t = 1;
  // cin >> t;
  while (t--) {
    ll n, i, j, k;
    cin >> n >> k;
    ll arr[n];
    set<ll> s;

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

    vector<ll> path;

    ll curr_ind = 0;
    ll start_cycle;
    for (i = 0; i < n; i++) {
      if (s.find(curr_ind) == s.end()) {
        // not already reached;
        s.insert(curr_ind);
        path.push_back(curr_ind);
        ll prev_ind = curr_ind;
        curr_ind = arr[curr_ind];
        // g[prev_ind].push_back(curr_ind);
      } else {
        // here is the start of the cycle
        start_cycle = curr_ind;
        break;
      }
    }

    // ll len = path.size();
    // k %= len;
    // for(i=0;i<path.size();i++)
    // {
    //     cout << path[i] << " ";
    // }
    // cout << '\\n';
    // cout << k << '\\n';
    // cout << (path[k] + 1) << '\\n';
    // cout << curr_ind+1 << '\\n';

    vector<ll> cycle;
    ll cyc_s_i = 0;

    for (i = 0; i < path.size(); i++) {
      if (path[i] != start_cycle) {
        k--;
      } else {
        cyc_s_i = i;
        break;
      }
    }

    for (i = cyc_s_i; i < path.size(); i++) {
      cycle.push_back(path[i]);
    }

    ll len = cycle.size();
    k %= len;
    cout << cycle[k] + 1 << '\\n';
  }
}