Language: C++


#include <vector>

#include <iostream>
#include <queue>
#include <set>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)

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<vector<int>> dist(n, vector<int>(n, 1e9 + 1)),
	    prev(n, vector<int>(n, -1));
	REP(i, n) {
		dist[i][i] = 0;
		q.push(i);
		while (!q.empty()) {
			int p = q.front();
			q.pop();
			for (auto a : g[p]) {
				if (dist[i][a] > dist[i][p] + 1) {
					q.push(a);
					dist[i][a] = dist[i][p] + 1;
					prev[i][a] = p;
				}
			}
		}
	}

	vector<int> res;
	REP(i, m) {
		if (dist[e[i].second][e[i].first] != 1e9 + 1) {
			int pos = e[i].first;
			while (pos != e[i].second) {
				res.push_back(pos);
				pos = prev[e[i].second][pos];
			}
			res.push_back(pos);
		}
	}
	reverse(res.begin(), res.end());
	set<int> st(res.begin(), res.end());
	{
		set<pair<int, int>> s(e.begin(), e.end());
		REP(i, res.size()) {
			assert(s.count({res[i], res[(i + 1) % res.size()]}));
		}
	}
	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:39:14: error: 'max_element' was not declared in this scope
   39 |         if (*max_element(d.begin(), d.end()) == 0) {
      |              ^~~~~~~~~~~
a.cc:73:9: error: 'reverse' was not declared in this scope
   73 |         reverse(res.begin(), res.end());
      |         ^~~~~~~
a.cc:78:25: error: 'assert' was not declared in this scope
   78 |                         assert(s.count({res[i], res[(i + 1) % res.size()]}));
      |                         ^~~~~~
a.cc:7:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    6 | #include <set>
  +++ |+#include <cassert>
    7 | using namespace std;
a.cc:86:45: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int&)'
   86 |                                 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:4:
/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, std::char_traits<_CharT> > >::__type std::find(istreambuf_iterator<_CharT, char_traits<_CharT> >, istreambuf_iterator<_CharT, char_traits<_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:86:45: note:   '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' is not derived from 'std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >'
   86 |                                 int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
      |                                         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:88:61: error: 'b' was not declared in this scope
   88 |                                 if ((a + 1) % res.size() == b) continue;
      |                                                             ^
a.cc:90:41: error: 'b' was not declared in this scope
   90 |                                 if (a < b) {
      |                                         ^