Language: C++

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

vector<int> adj[MAX];
int visit[MAX];
int graph[MAX][MAX];

vector<int> dfs(int v, int root)
{
	vector<int> ans;
	visit[v] = 1;
	if (v != root)
	{
		for (int i : adj[v])
		{
			if (i != root && visit[i]) return ans;
		}
		if (graph[v][root])
		{
			ans.push_back(v);
			return ans;
		}
	}
	for (int i : adj[v])
	{
		vector<int> res = dfs(i, root);
		if (!res.empty())
		{
			int flag = 1;
			for (int j : res)
			{
				if (graph[v][j] && i != j)
				{
					flag = 0;
					break;
				}
			}
			if (!flag) continue;
			res.push_back(v);
			return res;
		}
	}
	visit[v] = 0;
	return ans;
}

int main(void)
{
	int N, M;
	cin >> N >> M;
	for (int i = 0; i < M; i++)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		adj[u].push_back(v);
		graph[u][v] = 1;
	}
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++) visit[j] = 0;
		vector<int> res = dfs(i, i);
		reverse(res.begin(), res.end());
		if (!res.empty())
		{
			cout << res.size() << endl;
			for (int j : res) cout << j + 1 << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}


Compiler output:
a.cc: In function 'std::vector<int> dfs(int, int)':
a.cc:12:9: error: reference to 'visit' is ambiguous
   12 |         visit[v] = 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:6:5: note:                 'int visit [1010]'
    6 | int visit[MAX];
      |     ^~~~~
a.cc:17:42: error: reference to 'visit' is ambiguous
   17 |                         if (i != root && visit[i]) return ans;
      |                                          ^~~~~
/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:6:5: note:                 'int visit [1010]'
    6 | int visit[MAX];
      |     ^~~~~
a.cc:44:9: error: reference to 'visit' is ambiguous
   44 |         visit[v] = 0;
      |         ^~~~~
/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:6:5: note:                 'int visit [1010]'
    6 | int visit[MAX];
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:63:45: error: reference to 'visit' is ambiguous
   63 |                 for (int j = 0; j < N; j++) visit[j] = 0;
      |                                             ^~~~~
/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:6:5: note:                 'int visit [1010]'
    6 | int visit[MAX];
      |     ^~~~~