Language: C++

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,x,n) for(int i=x;i<n;i++)
#define mod 1000000007 //10^9+7
#define INF 1000000000000 //10^12
#define F first
#define S second
#define P pair<int,int>
#define all(vec) vec.begin(),vec.end()
int a[300000];
vector<int>visit(30000,-90);
vector<int>seq;
signed main(){
    int n,k;
    cin>>n>>k;
    rep(i,n){
        cin>>a[i+1];
    }
    int now=1,cnt=0;
    int ans=0;
    int c;
    while(1){
        if(visit[now]>=0){
            c=cnt-visit[now];
            ans=a[((k-visit[now])%c+1)%n];
            break;
        }
        visit[now]=cnt;
        now=a[now];
        cnt++;
    }
    int now2=now;
    while(1){
        seq.push_back(now2);
        now2=a[now2];
        if(now2==now)break;
    }
    ans=seq[(k-visit[now])%c];
    cout<<ans<<endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:25:12: error: reference to 'visit' is ambiguous
   25 |         if(visit[now]>=0){
      |            ^~~~~
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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(30000,-90);
      |            ^~~~~
a.cc:26:19: error: reference to 'visit' is ambiguous
   26 |             c=cnt-visit[now];
      |                   ^~~~~
/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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(30000,-90);
      |            ^~~~~
a.cc:27:23: error: reference to 'visit' is ambiguous
   27 |             ans=a[((k-visit[now])%c+1)%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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(30000,-90);
      |            ^~~~~
a.cc:30:9: error: reference to 'visit' is ambiguous
   30 |         visit[now]=cnt;
      |         ^~~~~
/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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(30000,-90);
      |            ^~~~~
a.cc:40:16: error: reference to 'visit' is ambiguous
   40 |     ans=seq[(k-visit[now])%c];
      |                ^~~~~
/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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(30000,-90);
      |            ^~~~~