Language: C++

#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define f(i,x,n) for(int i=x;i<n;i++) 
#define all(c) c.begin(),c.end()
const int MOD = 1e9 + 7, N = 5e5 + 10;
const int LINF = LLONG_MAX;
int n,k;
vector<int>adj[N];
int mark[N];
vector<int>q(N);
vector<int>parent(N,-1);
vector<int>visit(N,0);
vector<int>cycle;
int bfs(int x){
    queue<int>q;
    visit[x] = 1;
    q.push(x);
    parent[x] = x;
    while(!q.empty()){
        int a = q.front();
        q.pop();
        for(auto s : adj[a]){
            if(!visit[s]){
                visit[s] = 1;
                q.push(s);
                parent[s] = a;
            }
            else{
                return s;
            }
        }
    }
}
void cycles(){
    int start = bfs(1);
    cycle.push_back(start);
    int c = adj[start][0];
    while(c != start){
        cycle.push_back(c);
        c = adj[c][0];
    }
    cycle.push_back(start);
    // reverse(all(cycle));
    // for(auto s : cycle){
    //     cout << s << " ";
    // }
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    f(i,1,n+1){
        cin >> q[i];
    }
    f(i,1,n+1){
        adj[i].push_back(q[i]);
    }
    cycles();
    k++;
    int a = k%cycle.size();
    cout << cycle[a];
    return 0;
}
 

Compiler output:
a.cc: In function 'long long int bfs(long long int)':
a.cc:17:5: error: reference to 'visit' is ambiguous
   17 |     visit[x] = 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:13:12: note:                 'std::vector<long long int> visit'
   13 | vector<int>visit(N,0);
      |            ^~~~~
a.cc:24:17: error: reference to 'visit' is ambiguous
   24 |             if(!visit[s]){
      |                 ^~~~~
/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(N,0);
      |            ^~~~~
a.cc:25:17: error: reference to 'visit' is ambiguous
   25 |                 visit[s] = 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(N,0);
      |            ^~~~~
a.cc:34:1: warning: control reaches end of non-void function [-Wreturn-type]
   34 | }
      | ^