Language: C++

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)
vector<int> res;
int dfs(vector<vector<int>> &g, vector<int> &visited, int v, int t) {
	if (visited[v] < t) return -1;
	if (visited[v] == t) {
		if (res.size() > 0) return -1;
		return v;
	}
	visited[v] = t;
	REP(i, g[v].size()) {
		int k = dfs(g, visited, g[v][i], t);
		if (k >= 0) {
			res.push_back(v);
			if (v == k) return -1;
			return k;
		}
	}
	return -1;
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> g(n);
	vector<int> d(n);
	vector<pair<int, int>> e(m);
	REP(i, m) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		d[b]++;
		g[a].push_back(b);
		e[i] = {a, b};
	}
	queue<int> q;
	REP(i, n) {
		if (d[i] == 0) {
			q.push(i);
		}
	}
	while (!q.empty()) {
		int p = q.front();
		q.pop();
		REP(i, g[p].size()) {
			if (--d[g[p][i]] == 0) q.push(g[p][i]);
		}
	}
	if (*max_element(d.begin(), d.end()) == 0) {
		cout << -1 << endl;
		return 0;
	}
	vector<int> visited(n, 1e9 + 1);
	int cnt = 0;
	REP(i, n) {
		if (visited[i] == 1e9 + 1) {
			dfs(g, visited, i, cnt);
			cnt++;
			if (res.size() > 0) break;
		}
	}
	reverse(res.begin(), res.end());
	set<int> st(res.begin(), res.end());
	while (1) {

		bool f = false;
		REP(i, m) {
			if (st.count(e[i].first) && st.count(e[i].second)) {

				int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
				    b = find(res.begin(), res.end(), e[i].second) - res.begin();
				if ((a + 1) % res.size() == b) continue;
				vector<int> next;
				if (a < b) {
					for (int i = 0; i <= a; ++i)
						next.push_back(res[i]);
					for (int i = b; i < res.size(); ++i)
						next.push_back(res[i]);
				} else {
					for (int i = b; i <= a; ++i)
						next.push_back(res[i]);
				}
				res = next;
				st = set<int>(res.begin(), res.end());
				f = true;
			}
		}
		if (!f) break;
	}
	cout << res.size() << endl;
	for (auto a : res)
		cout << a + 1 << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:55:14: error: 'max_element' was not declared in this scope
   55 |         if (*max_element(d.begin(), d.end()) == 0) {
      |              ^~~~~~~~~~~
a.cc:68:9: error: 'reverse' was not declared in this scope
   68 |         reverse(res.begin(), res.end());
      |         ^~~~~~~
a.cc:76:45: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int&)'
   76 |                                 int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
      |                                         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/locale_facets.h:48,
                 from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:76:45: note:   '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   76 |                                 int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
      |                                         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:78:61: error: 'b' was not declared in this scope
   78 |                                 if ((a + 1) % res.size() == b) continue;
      |                                                             ^
a.cc:80:41: error: 'b' was not declared in this scope
   80 |                                 if (a < b) {
      |                                         ^