Language: C++

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int N = 1e5 + 5;
int n, m, prev[N], last[N], target[N], t, prev2[N], last2[N], target2[N], s[N], e[N], 
st, ed, ans;
void add(int x, int y)
{
	t++;
	prev[t] = last[x];
	target[t] = y;
	last[x] = t;
	prev2[t] = last[y];
	target2[t] = x;
	last2[y] = t;
}

struct node
{
	int id, v;
	friend bool operator < (node a, node b)
	{
		return a.id < b.id;
	}
};
priority_queue <node> q;

bool visit[N];
void dfss(int x)
{
	visit[x] = 1;
	int ptr = last[x];
	while (ptr)
	{
		int xx = target[ptr];
		if (!visit[xx])
		{
			s[xx] = s[x] + 1;
			dfss(xx);
		}
		else
		{
			node xxx;
			xxx.id = xx;
			xxx.v = s[x] - s[xx] + 1;
			q.push(xxx);
		}
		ptr = prev[ptr];
	}
}

void dfse(int x)
{
	visit[x] = 1;
	int ptr = last2[x];
	while (ptr)
	{
		int xx = target2[ptr];
		if (!visit[xx])
		{
			e[xx] = e[x] + 1;
			dfse(xx);
		}
		ptr = prev2[ptr];
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
	}
	cin >> st >> ed;
	dfss(st);
	if (!visit[ed])
	{
		cout << -1 << endl;
		return 0;
	}
	memset(visit, 0, sizeof(visit));
	dfse(ed);
	ans = 1e9;
	for (int i = 1; i <= n; i++)
	{
		node x = q.top();
		while (x.id == i && !q.empty())
		{
			if ((s[i] + e[i]) % 3 == 0 && ans > s[i] + e[i]) ans = s[i] + e[i];
			else if ((s[i] + e[i] + x.v) % 3 == 0 && ans > s[i] + e[i] + x.v) ans = s[i] + e[i] + x.v;
			else if ((s[i] + e[i] + 2 * x.v) % 3 == 0 && ans > s[i] + e[i] + 2 * x.v) ans = s[i] + e[i] + 2 * x.v;
			else if ((s[i] + e[i] + 3 * x.v) % 3 == 0 && ans > s[i] + e[i] + 3 * x.v) ans = s[i] + e[i] + 3 * x.v;
			q.pop();
			x = q.top();
		}
	}
	if (ans < 1e8) cout << ans / 3 << endl;
	else cout << -1 << endl;
	return 0;
}


Compiler output:
a.cc: In function 'void add(int, int)':
a.cc:13:9: error: reference to 'prev' is ambiguous
   13 |         prev[t] = last[x];
      |         ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:8:11: note:                 'int prev [100005]'
    8 | int n, m, prev[N], last[N], target[N], t, prev2[N], last2[N], target2[N], s[N], e[N],
      |           ^~~~
a.cc: In function 'void dfss(int)':
a.cc:51:23: error: reference to 'prev' is ambiguous
   51 |                 ptr = prev[ptr];
      |                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:8:11: note:                 'int prev [100005]'
    8 | int n, m, prev[N], last[N], target[N], t, prev2[N], last2[N], target2[N], s[N], e[N],
      |           ^~~~