Language: C++

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MOD=(ll)1e9+7;
const ll MOD2=(ll)998244353;
const ll INF=(ll)1e18+10;
const double PI = 2.0*acos(0);

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int dy[]={-1,1,0,0};
int dx[]={0,0,-1,1};

ll N,K;
int A[200010];
int pre[200010];
int visit[20010];

int backfrom,backto;

void dfs(int n){
    if(pre[A[n]]>=0){
        backfrom=n;
        backto=A[n];
        return;
    }
    visit[A[n]]=visit[n]+1;
    pre[A[n]]=n;
    dfs(A[n]);
}

void solve(int ca){
    cin>>N>>K;

    memset(pre,-1, sizeof(pre));
    for (int i = 0; i < N; ++i) {
        cin>>A[i]; --A[i];
    }
    visit[0]=0;
    dfs(0);

    if(visit[backfrom]>=K){
        for (int i = 0; i < N; ++i) {
            if(visit[i]==K){
                cout<<i+1<<endl;
                return;
            }
        }
    }

    K-=visit[backto]-1;
    ll tmp=K%(visit[backfrom]-visit[backto]+1);
    if(tmp==0){
        cout<<backfrom+1<<endl;
        return;
    }

    tmp=visit[backto]-1+tmp;

    for (int i = 0; i < N; ++i) {
        if(visit[i]==tmp){
            cout<<i+1<<endl;
            return;
        }
    }

}

//#define MULTI
int main() {
#ifdef MULTI
    int N; cin>>N;
    for (int i = 0; i < N; ++i) {
        solve(i+1);
    }
#else
    solve(0);
#endif
    return 0;
}


Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:27:5: error: reference to 'visit' is ambiguous
   27 |     visit[A[n]]=visit[n]+1;
      |     ^~~~~
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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:27:17: error: reference to 'visit' is ambiguous
   27 |     visit[A[n]]=visit[n]+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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc: In function 'void solve(int)':
a.cc:39:5: error: reference to 'visit' is ambiguous
   39 |     visit[0]=0;
      |     ^~~~~
/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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:42:8: error: reference to 'visit' is ambiguous
   42 |     if(visit[backfrom]>=K){
      |        ^~~~~
/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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:44:16: error: reference to 'visit' is ambiguous
   44 |             if(visit[i]==K){
      |                ^~~~~
/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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:51:8: error: reference to 'visit' is ambiguous
   51 |     K-=visit[backto]-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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:52:15: error: reference to 'visit' is ambiguous
   52 |     ll tmp=K%(visit[backfrom]-visit[backto]+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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:52:31: error: reference to 'visit' is ambiguous
   52 |     ll tmp=K%(visit[backfrom]-visit[backto]+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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:58:9: error: reference to 'visit' is ambiguous
   58 |     tmp=visit[backto]-1+tmp;
      |         ^~~~~
/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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~
a.cc:61:12: error: reference to 'visit' is ambiguous
   61 |         if(visit[i]==tmp){
      |            ^~~~~
/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:17:5: note:                 'int visit [20010]'
   17 | int visit[20010];
      |     ^~~~~