Language: C++

/**
*    created: 10.05.2020 22:07:11
**/

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

int main() {
    ll n, k;
    cin >> n >> k;
    vector<int> a(n+1);
    rep(i, n) cin >> a[i+1];
    set<int> next(1,1);
    vector<int> nex;
    int s = 1, i = 1, ans;
    while(1)
    {
        i++;
        nex.push_back(nex.back());
        next.insert(a[nex.back()]);
        if(next.size>s){
            s = next.size();
        }else{

            int f = 0;
            vector<int> nextt;
            while(1){
                f++;
                if(f==1){
                    nextt.push_back(a[1]);    
                }else{
                    nextt.push_back(a[nextt.back()]);
                
                    }
                if(nextt.back()==nex.back()){
                    break;
                }
            }
            int d = i - f;
            d = (k-i) % d;
            vector<int> nn(1,nex.back());
            rep(g,d){
                nn.push_back(a[nn.back()]);
                ans = nn.back();
            }
            break;
        }
        
        if(i==k){
            ans = next.back();
            break;
        }else{
        continue;
        }
    }
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:24:17: error: invalid use of member function 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::size() const [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; size_type = long unsigned int]' (did you forget the '()' ?)
   24 |         if(next.size>s){
      |            ~~~~~^~~~
      |                     ()
a.cc:53:24: error: 'class std::set<int>' has no member named 'back'
   53 |             ans = next.back();
      |                        ^~~~
In file included from /usr/include/c++/14/set:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:158,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_set.h: In instantiation of 'std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]':
a.cc:16:22:   required from here
   16 |     set<int> next(1,1);
      |                      ^
/usr/include/c++/14/bits/stl_set.h:195:38: error: no matching function for call to 'std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_M_insert_range_unique(int&, int&)'
  195 |         { _M_t._M_insert_range_unique(__first, __last); }
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152:
/usr/include/c++/14/bits/stl_tree.h:1096:9: note: candidate: 'template<class _InputIterator> std::__enable_if_t<((bool)std::is_same<_Val, typename std::iterator_traits<_InputIterator>::value_type>::value)> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_range_unique(_InputIterator, _InputIterator) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = std::less<int>; _Alloc = std::allocator<int>]'
 1096 |         _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1096:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_tree.h: In substitution of 'template<class _InputIterator> std::__enable_if_t<((bool)std::is_same<int, typename std::iterator_traits< <template-parameter-1-1> >::value_type>::value), void> std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_M_insert_range_unique(_InputIterator, _InputIterator) [with _InputIterator = int]':
/usr/include/c++/14/bits/stl_set.h:195:31:   required from 'std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]'
  195 |         { _M_t._M_insert_range_unique(__first, __last); }
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
a.cc:16:22:   required from here
   16 |     set<int> next(1,1);
      |                      ^
/usr/include/c++/14/bits/stl_tree.h:1095:58: error: no type named 'value_type' in 'struct std::iterator_traits<int>'
 1095 |         __enable_if_t<__same_value_type<_InputIterator>::value>
      |                                                          ^~~~~
/usr/include/c++/14/bits/stl_set.h: In instantiation of 'std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]':
a.cc:16:22:   required from here
   16 |     set<int> next(1,1);
      |                      ^
/usr/include/c++/14/bits/stl_tree.h:1105:9: note: candidate: 'template<class _InputIterator> std::__enable_if_t<((bool)(! std::is_same<_Val, typename std::iterator_traits<_InputIterator>::value_type>::value))> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_range_unique(_InputIterator, _InputIterator) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = std::less<int>; _Alloc = std::allocator<int>]'
 1105 |         _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1105:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_tree.h: In substitution of 'template<class _InputIterator> std::__enable_if_t<((bool)(! std::is_same<int, typename std::iterator_traits< <template-parameter-1-1> >::value_type>::value)), void> std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_M_insert_range_unique(_InputIterator, _InputIterator) [with _InputIterator = int]':
/usr/include/c++/14/bits/stl_set.h:195:31:   required from 'std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]'
  195 |         { _M_t._M_insert_range_unique(__first, __last); }
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
a.cc:16:22:   required from here
   16 |     set<int> next(1,1);
      |                      ^
/usr/include/c++/14/bits/stl_tree.h:1104:59: error: no type named 'value_type' in 'struct std::iterator_traits<int>'
 1104 |         __enable_if_t<!__same_value_type<_InputIterator>::value>
      |                                                           ^~~~~