Language: C++

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
constexpr int N_MAX = 1123;

int n, m;
vector<int> graph[N_MAX];
vector<Pii> edges;
vector<bool> used(N_MAX, false), in_loop(N_MAX, true);
deque<int> history;

inline void show_history() {
    int l = history.size();
    cout << l << endl;
    for(int i=0;i<l;++i) {
        cout << history[i]+1 << endl;
    }
}

inline bool check() {
    vector<int> indeg(N_MAX, 0), outdeg(N_MAX, 0);
    for(int i: history) {
        for(int j: graph[i]) {
            ++outdeg[i];
            ++indeg[j];
        }
    }

    for(int x: history) {
        if(outdeg[x] != 1 || indeg[x] != 1) return false;
    }
    return true;
}

inline bool dfs(int node) {
    used[node] = true;
    in_loop[node] = true;
    history.push_back(node);
    for(int child: graph[node]) {
        if(in_loop[child]) {
            while(history.front() != child) {
                in_loop[history.front()] = false;
                history.pop_front();
            }
            return true;
        }
        if(dfs(child)) return true;
    }
    history.pop_back();
    in_loop[node] = false;
    return false;
}

int main() {
    std::ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m;
    int a, b;
    for(int i=0;i<m;++i){
        cin >> a >> b;
        --a; --b;
        graph[a].push_back(b);
        edges.emplace_back(a, b);
    }

    // 閉路探し (閉路がなければ終わり)
    {
        int i = 0;
        for(;i<n;++i) {
            if(used[i]) continue;
            in_loop = vector<bool>(N_MAX, false);
            if(dfs(i)) break;
        }
        if(i == n) {
            cout << -1 << endl;
            return 0;
        }
    }

    // 閉路縮約
    while(!check()) {
        // 関係ないノードとエッジ消す
        for(int i=0;i<n;++i) graph[i].clear();
        for(Pii e: edges) {
            if(in_loop[e.first] && in_loop[e.second]) {
                graph[e.first].push_back(e.second);
            }
        }

        // cerr << "history";
        for(int x: history) // cerr << " " << x;
        // cerr << endl;
        for(int i=0;i<n;++i) {
            // cerr << i << ": ";
            for(int j: graph[i]) // cerr << j << " ";
            // cerr << endl;
        }

        // 注目する辺を選ぶ
        Pii e = {-1, -1};
        vector<int> selected[n];
        for(int i=0;i<n;++i) {
            selected[i].assign(graph[i].size(), 0);
        }
        for(int i=0;i<history.size();++i) {
            int a = history[i], b = history[(i+1)%history.size()];
            for(int j=0;j<graph[a].size();++j) {
                if(graph[a][j] == b) {
                    selected[a][j] = 1;
                }
            }
        }
        for(int i=0;i<n;++i) {
            for(int j=0;j<selected[i].size();++j) {
                if(selected[i][j] == 0) {
                    e.first = i;
                    e.second = graph[i][j];
                    break;
                }
            }
            if(e.first != -1) break;
        }
        // cerr << "e " << e.first << "->" << e.second << endl;
        
        if(e.second != -1) while(history.front() != e.second) {
            in_loop[history.front()] = false;
            history.pop_front();
        }
        if(e.first != -1) while(history.back() != e.first) {
            in_loop[history.back()] = false;
            history.pop_back();
        }
    }

    if(history.empty()) {
        cout << -1 << endl;
    } else {
        show_history();
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:122:9: error: expected primary-expression before '}' token
  122 |         }
      |         ^