Language: C++

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

vector<bool>visit;
vector<ll> v;
vector<int> s;
int n;
ll k;
int push(int i){
    s.push_back(i);
    visit[i] = true;
    if(visit[v[i]] == false){
        return push(v[i]);
    }
    else {
        return v[i];
    }
}
int main(void){
    cin>>n>>k;
    v.resize(n);
    visit.resize(n);
    rep(i, n) {
        int a;
        cin>>a;
        --a;
        v[i] = a;
    }
    rep(i, n) visit[i] = false;
    int g = push(0);
    int gidx;
    rep(i, s.size()){
        if(s[i] == g) gidx = i;
    }
    int ans;
    int m = s.size();
    if(k<=gidx) ans = s[k];
    else {
        ll div = (k-(ll)gidx)%((ll)m-(ll)gidx);
        ans = s[gidx+(int)div];
    }
    cout<<ans+1<<endl;
    return 0;
}

Compiler output:
a.cc: In function 'int push(int)':
a.cc:13:5: error: reference to 'visit' is ambiguous
   13 |     visit[i] = true;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:13: note:                 'std::vector<bool> visit'
    6 | vector<bool>visit;
      |             ^~~~~
a.cc:14:8: error: reference to 'visit' is ambiguous
   14 |     if(visit[v[i]] == false){
      |        ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:13: note:                 'std::vector<bool> visit'
    6 | vector<bool>visit;
      |             ^~~~~
a.cc: In function 'int main()':
a.cc:24:5: error: reference to 'visit' is ambiguous
   24 |     visit.resize(n);
      |     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:13: note:                 'std::vector<bool> visit'
    6 | vector<bool>visit;
      |             ^~~~~
a.cc:31:15: error: reference to 'visit' is ambiguous
   31 |     rep(i, n) visit[i] = false;
      |               ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:13: note:                 'std::vector<bool> visit'
    6 | vector<bool>visit;
      |             ^~~~~
a.cc: In function 'int push(int)':
a.cc:12:16: warning: control reaches end of non-void function [-Wreturn-type]
   12 |     s.push_back(i);
      |     ~~~~~~~~~~~^~~