Language: C++

#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define SZ(x) ((int)(x).size())
#define unmap unordered_map
#define pq priority_queue
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 2e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pll dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
/*------------------------*/

ll n;
vll g[limit];
bool visit[limit];
ll ans = -1;

void bfs() {
    queue <pll> q;
    q.push({1, 0});
    visit[1] = true;

    while(!q.empty()) {
        auto t = q.front();
        q.pop();

        if (t.fi == 2) ans = t.se;

        for (auto it : g[t.fi]) if (!visit[it]) {q.push({it, t.se + 1}); visit[it] = true;}
    }
}

void solveProblem() {
    bfs();

    cout << ans << endl;
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> n;
    REP(i, 1, n) {
        ll v;
        cin >> v;
        g[i].pb(v);
    }
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}



Compiler output:
a.cc: In function 'void bfs()':
a.cc:64:5: error: reference to 'visit' is ambiguous
   64 |     visit[1] = 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:58:6: note:                 'bool visit [200005]'
   58 | bool visit[limit];
      |      ^~~~~
a.cc:72:38: error: reference to 'visit' is ambiguous
   72 |         for (auto it : g[t.fi]) if (!visit[it]) {q.push({it, t.se + 1}); visit[it] = 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:58:6: note:                 'bool visit [200005]'
   58 | bool visit[limit];
      |      ^~~~~
a.cc:72:74: error: reference to 'visit' is ambiguous
   72 |         for (auto it : g[t.fi]) if (!visit[it]) {q.push({it, t.se + 1}); visit[it] = 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:58:6: note:                 'bool visit [200005]'
   58 | bool visit[limit];
      |      ^~~~~