Language: C++

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define ALL(x) x.begin(),x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9+7;
const ll INF = 1e18;
const int MAX = 1e6;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {int to, cost;}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
bool visit[MAX]; // 訪問状況

int n, m; // 変数
ll k;
int a[MAX];

int main() {
// input
    cin >> n >> k;
    rep(i, n+1) {
        if (i > 0) cin >> a[i];
    }
// solve
    int cnt = 1; // ループに突入するテレポート回数
    int start = 1;
    visit[start] = true;
    int next = a[start];
    while (not visit[next]) {
        cnt++;
        visit[next] = true;
        next = a[next];
    }
    int loop_start = next; // ループの始点
    // printf("ループに突入するテレポート回数%d\n", cnt);
    // printf("ループの始点%d\n", next);
    int ans;
    if (cnt < k) {
        next = 1;
        int d = 0; // ループ始点までに訪問する都市の数
        while (next != loop_start) {
            d++;
            next = a[next];
        }
        int loop_size = cnt - d;
        // printf("ループの大きさ%d\n", loop_size);
        int pos = 1;
        rep(i, d) {
            pos = a[pos];
        }
        if (loop_size == 0) {
            ans = pos;
        } else {
            rep(i, (k-d)%loop_size) {
                pos = a[pos];
            }
            ans = pos;
        }
    } else {
        int pos = 1;
        rep(i, k) {
            pos = a[pos];
        }
        ans = pos;
    }
// output
    cout << ans << "\n";
}

Compiler output:
a.cc: In function 'int main()':
a.cc:35:5: error: reference to 'visit' is ambiguous
   35 |     visit[start] = 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:20:6: note:                 'bool visit [1000000]'
   20 | bool visit[MAX]; // 訪問状況
      |      ^~~~~
a.cc:37:16: error: reference to 'visit' is ambiguous
   37 |     while (not visit[next]) {
      |                ^~~~~
/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:20:6: note:                 'bool visit [1000000]'
   20 | bool visit[MAX]; // 訪問状況
      |      ^~~~~
a.cc:39:9: error: reference to 'visit' is ambiguous
   39 |         visit[next] = true;
      |         ^~~~~
/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:20:6: note:                 'bool visit [1000000]'
   20 | bool visit[MAX]; // 訪問状況
      |      ^~~~~