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

#include <bits/stdc++.h>
using namespace std;
#define inf 2e18
#define bg begin()
#define en end()
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 100000000007
#define vi vector<int>
#define eb emplace_back
#define lb(v, a) lower_bound(v.begin(), v.end(), a)
#define ub(v, a) upper_bound(v.begin(), v.end(), a)
#define bs binary_search
#define vll vector<long long int>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define forv(v) for (auto it = v.begin(); it != v.end(); it++)
#define mem(a, val) memset(a, val, sizeof(a))
#define Sort(vec) sort(vec.begin(), vec.end())
#define RSort(vec) sort(vec.rbegin(), vec.rend())
#define vin(v, n)                                                              \\
  for (int i = 0; i < n; i++) {                                                \\
    cin >> x;                                                                  \\
    v.pb(x);                                                                   \\
    mpx[x] += 1;                                                               \\
  }
#define fifo                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PI 3.141592653589793238462643383279502884L
ll Ceil(ll a, ll b) { return a / b + (a % b != 0); }
ll fastModExp(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}
ll GC(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GC(b, a % b);
}
// Chrnlgy: Binary Search,Greedy,dp,dfs and similar

ll inv_gc(ll a, ll b) { return a / GC(a, b) * b; }

/*
ll xo,yo,z;
void exten_euclid(ll a,ll b)
{	ll temp;
        if(b==0)
        {
                z=a;
                xo=1;
                yo=0;
        }
        else
        {
                exten_euclid(b,a%b);
                temp=xo;
                xo=yo;
                yo=temp-(a/b)*yo;
        }
return;
}



//     O(sqrtn)

vector<ll> factorize(ll n) {
    vector<ll> res;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            res.push_back(i);
            n /= i;
        }
    }
    if (n != 1) {
        res.push_back(n);
    }
    return res;
}

*************FACTORIZE IN 0(LOG(N)) PROVIDED N IS INTEGER**************

int minPrime[100000];

void modified_sieve(int n){
for (int i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {         //If i is prime
        for (int j = i * i; j <= n; j += i) {
            if (minPrime[j] == 0) {
                minPrime[j] = i;
            }
        }
    }
}
for (int i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
        minPrime[i] = i;
    }
}
}

vector<int> factorize(int n) {
    vector<int> res;
    while (n != 1) {
        res.push_back(minPrime[n]);
        n /= minPrime[n];
    }
    return res;
}

*************************************************
void sieve(int N) {
        bool isPrime[N+1];
        for(int i = 0; i <= N;++i) {
            isPrime[i] = true;
        }
        isPrime[0] = false;
        isPrime[1] = false;
        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }


ll binomialCoeff(ll n, ll k)
{
     ll res = 1;
        if(n<k)
        return 0;

    // Since C(n, k) = C(n, n-k)
    if ( k > n - k )
        k = n - k;

    // Calculate value of
    // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
    for (ll i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}


bool dfs_cycle(int root){
vis[root]=1;
bool temp=false;
for(int i=0;i<G[root].size();i++)
{
int v=G[root][i];
if(vis[v]==0)
temp|=dfs_cycle(v);
else if(vis[v]==1)
return true;
}
vis[root]=2;
return temp;
}

vector<vector<int> > G;
vector<int> vis;
bool is_cyclic(int n){
    vector<int> mark(2000,-1);
    queue<int> q;
    q.push(n);
    mark[n]=0;
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;G[v].size();i++){
            int nbs=G[v][i];
            if(mark[nbs]==-1){
                q.push(nbs);
                mark[nbs]=0;
            }
            else if(mark[nbs]==0) return true;
        }
        mark[v]=1;
    }
    return false;
}





void DFS_UTIL(ll u){
        vis[u]=true;

        for(auto it=G[u].begin();it!=G[u].end();it++){
                if(vis[*it]==false)
                {
                DFS_UTIL(*it);
                }
        }
}

*/

map<int, int> par, rk;
void make_set(int n) // the rank and parent
{
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    rk[i] = 1;
  }
}

int find(int x) // with PATH COMPRESSION
{
  if (x == par[x])
    return x;
  int p = find(par[x]);
  par[x] = p;
  return p;
}

int uni(int x, int y) // with UNION BY rank
{
  int p1 = find(x);
  int p2 = find(y);
  if (p1 == p2)
    return 0;
  if (rk[p1] >= rk[p2]) {
    par[p2] = p1;
    rk[p1] += rk[p2];
  } else {
    par[p1] = p2;
    rk[p2] += rk[p1];
  }
  return 1;
}
/*

bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) {return (a.second
< b.second);}


void bfs(int o,int dp[],int n){
        queue<int> q;
        q.push(o);
        dp[o]=0;
        bool visited[n+1]={0};
        visited[o]=true;
        while(q.empty()==false){
                int a=q.front();
                q.pop();
                for (int u : G[a]) {
            if(visited[u]==false){
                visited[u]=true;
                q.push(u);

                dp[u]=dp[a]+1;
            }
        }
        }
}

*/

map<ll, ll> mpx, mpx2;
int main() {
  fifo;
  int n;
  ll k;
  cin >> n >> k;
  // make_set(n);
  int v[n + 1];
  forn1(i, n) {
    cin >> v[i];
    mpx[i] = v[i];
  }
  int z = 1, p;
  vi x, y;
  vector<bool> vis(n + 1, false);
  vis[1] = true;
  ll count = 0, count1 = 0, count2 = 0;
  ll sum[n + 2];
  sum[1] = 0;

  while (true) {
    if (vis[mpx[z]]) {
      p = mpx[z];
      count1 = sum[p];
      x.pb(z);
      count2 = count + 1 - sum[p];
      break;
    }
    count++;
    x.pb(z);
    sum[mpx[z]] = count;
    vis[mpx[z]] = true;
    z = mpx[z];
  }
  // forn(i,x.size())
  // cout<<x[i]<<" ";
  // k=k-count1;
  bool b = 0;
  vi rm;
  for (int i = 0; i < x.size(); i++) {
    if (x.at(i) == p)
      b = 1;
    if (!b)
      rm.push_back(x.at(i));
    else
      y.push_back(x.at(i));
  }

  if (k < rm.size()) {
    cout << rm.at(k) << "\\n";
  } else {
    k -= rm.size();
    cout << y.at(k % y.size()) << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 2e18
#define bg begin()
#define en end()
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 100000000007
#define vi vector<int>
#define eb emplace_back
#define lb(v, a) lower_bound(v.begin(), v.end(), a)
#define ub(v, a) upper_bound(v.begin(), v.end(), a)
#define bs binary_search
#define vll vector<long long int>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define forv(v) for (auto it = v.begin(); it != v.end(); it++)
#define mem(a, val) memset(a, val, sizeof(a))
#define Sort(vec) sort(vec.begin(), vec.end())
#define RSort(vec) sort(vec.rbegin(), vec.rend())
#define vin(v, n)                                                              \\
  for (int i = 0; i < n; i++) {                                                \\
    cin >> x;                                                                  \\
    v.pb(x);                                                                   \\
    mpx[x] += 1;                                                               \\
  }
#define fifo                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PI 3.141592653589793238462643383279502884L
ll Ceil(ll a, ll b) { return a / b + (a % b != 0); }
ll fastModExp(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}
ll GC(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GC(b, a % b);
}
// Chrnlgy: Binary Search,Greedy,dp,dfs and similar

ll inv_gc(ll a, ll b) { return a / GC(a, b) * b; }

/*
ll xo,yo,z;
void exten_euclid(ll a,ll b)
{	ll temp;
        if(b==0)
        {
                z=a;
                xo=1;
                yo=0;
        }
        else
        {
                exten_euclid(b,a%b);
                temp=xo;
                xo=yo;
                yo=temp-(a/b)*yo;
        }
return;
}



//     O(sqrtn)

vector<ll> factorize(ll n) {
    vector<ll> res;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            res.push_back(i);
            n /= i;
        }
    }
    if (n != 1) {
        res.push_back(n);
    }
    return res;
}

*************FACTORIZE IN 0(LOG(N)) PROVIDED N IS INTEGER**************

int minPrime[100000];

void modified_sieve(int n){
for (int i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {         //If i is prime
        for (int j = i * i; j <= n; j += i) {
            if (minPrime[j] == 0) {
                minPrime[j] = i;
            }
        }
    }
}
for (int i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
        minPrime[i] = i;
    }
}
}

vector<int> factorize(int n) {
    vector<int> res;
    while (n != 1) {
        res.push_back(minPrime[n]);
        n /= minPrime[n];
    }
    return res;
}

*************************************************
void sieve(int N) {
        bool isPrime[N+1];
        for(int i = 0; i <= N;++i) {
            isPrime[i] = true;
        }
        isPrime[0] = false;
        isPrime[1] = false;
        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }


ll binomialCoeff(ll n, ll k)
{
     ll res = 1;
        if(n<k)
        return 0;

    // Since C(n, k) = C(n, n-k)
    if ( k > n - k )
        k = n - k;

    // Calculate value of
    // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
    for (ll i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}


bool dfs_cycle(int root){
vis[root]=1;
bool temp=false;
for(int i=0;i<G[root].size();i++)
{
int v=G[root][i];
if(vis[v]==0)
temp|=dfs_cycle(v);
else if(vis[v]==1)
return true;
}
vis[root]=2;
return temp;
}

vector<vector<int> > G;
vector<int> vis;
bool is_cyclic(int n){
    vector<int> mark(2000,-1);
    queue<int> q;
    q.push(n);
    mark[n]=0;
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;G[v].size();i++){
            int nbs=G[v][i];
            if(mark[nbs]==-1){
                q.push(nbs);
                mark[nbs]=0;
            }
            else if(mark[nbs]==0) return true;
        }
        mark[v]=1;
    }
    return false;
}





void DFS_UTIL(ll u){
        vis[u]=true;

        for(auto it=G[u].begin();it!=G[u].end();it++){
                if(vis[*it]==false)
                {
                DFS_UTIL(*it);
                }
        }
}

*/

map<int, int> par, rk;
void make_set(int n) // the rank and parent
{
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    rk[i] = 1;
  }
}

int find(int x) // with PATH COMPRESSION
{
  if (x == par[x])
    return x;
  int p = find(par[x]);
  par[x] = p;
  return p;
}

int uni(int x, int y) // with UNION BY rank
{
  int p1 = find(x);
  int p2 = find(y);
  if (p1 == p2)
    return 0;
  if (rk[p1] >= rk[p2]) {
    par[p2] = p1;
    rk[p1] += rk[p2];
  } else {
    par[p1] = p2;
    rk[p2] += rk[p1];
  }
  return 1;
}
/*

bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) {return (a.second
< b.second);}


void bfs(int o,int dp[],int n){
        queue<int> q;
        q.push(o);
        dp[o]=0;
        bool visited[n+1]={0};
        visited[o]=true;
        while(q.empty()==false){
                int a=q.front();
                q.pop();
                for (int u : G[a]) {
            if(visited[u]==false){
                visited[u]=true;
                q.push(u);

                dp[u]=dp[a]+1;
            }
        }
        }
}

*/

map<ll, ll> mpx, mpx2;
int main() {
  fifo;
  int n;
  ll k;
  cin >> n >> k;
  // make_set(n);
  int v[n + 1];
  forn1(i, n) {
    cin >> v[i];
    mpx[i] = v[i];
  }
  int z = 1, p;
  vector<bool> vis(n + 1, false);
  vis[1] = true;
  ll count = 0, count1 = 0, count2 = 0;
  ll sum[n + 2];
  sum[1] = 0;
  while (true) {
    if (vis[mpx[z]]) {
      p = mpx[z];
      count1 = sum[p];
      count2 = count + 1 - sum[p];
      break;
    }
    count++;
    sum[mpx[z]] = count;
    vis[mpx[z]] = true;
    z = mpx[z];
  }
  k = k - count1;
  ll o = k % count2;
  int q = p;
  vi ans;
  while (mpx[q] != p) {
    ans.pb(q);
    q = mpx[q];
  }
  ans.pb(q);
  // forn(i,ans.size())
  // cout<<ans[i]<<" ";
  cout << ans[o] << endl;
  return 0;
}