Language: C++

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9+7)
#define MAX int(2 * 1e5 +5)
#define debug(x)  cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)
template<class T> bool chmax(T &a,  T b) {if(a<b){a=b; return 1;} return 0;}
template<class T> bool chmin(T &a,  T b) {if(a>b){a=b; return 1;} return 0;}
bool Add(int &a,int b){a = (a + b) % MOD;}
void myprint(int* A,int A_num){
   REP(i,A_num) cout << A[i] << " ";
   cout << endl;
}


const int n_max = 1004;
vector<int> G[n_max];
bool vis[n_max];

bool zan[n_max];
bool check[n_max];


vector<int> dfs(int v){
    vis[v] = true;
    check[v] = true;
    vector<int> res;
    itrfor(itr,G[v]){
        if( vis[*itr] == true){
            res.push_back(v);
            return res;
        }
        else{
            res = dfs(*itr);
            if(res.size() != 0){
                res.push_back(v);
                return res;
            }
        }
    }

    vis[v] = false;
    return res;
}

signed main(){
    int n,m;
    cin >> n >> m;
    REP(i,n+1) vis[i] = false;
    REP(i,m){
        int a,b;
        cin >> a >> b;
        G[a].push_back(b);
    }
    REP(i,n+1) check[i] = false;
    vector<int> heiro;
    FOR(i,1,n+1){
        if(check[i] == false){
            heiro = dfs(i);
            if(heiro.size() != 0) break;
        }
    }

    if(heiro.size() == 0){
        cout << -1 << endl;
        return 0;
    }

    Reverse(heiro);

    
    heiro.push_back(heiro[0]);

    vector<int> new_heiro;
    while(1){
        REP(i,n+1) zan[i] = false;
        itrfor(itr,heiro) zan[*itr] = true;
        bool flag = true;
        new_heiro.clear();
        itrfor(itr,heiro){
            if(next(itr) == heiro.end()) break;
            bool hoge = false;#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9+7)
#define MAX int(2 * 1e5 +5)
#define debug(x)  cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)
template<class T> bool chmax(T &a,  T b) {if(a<b){a=b; return 1;} return 0;}
template<class T> bool chmin(T &a,  T b) {if(a>b){a=b; return 1;} return 0;}
bool Add(int &a,int b){a = (a + b) % MOD;}
void myprint(int* A,int A_num){
   REP(i,A_num) cout << A[i] << " ";
   cout << endl;
}


const int n_max = 1004;
vector<int> G[n_max];
bool vis[n_max];

bool zan[n_max];
bool check[n_max];


vector<int> dfs(int v){
    vis[v] = true;
    check[v] = true;
    vector<int> res;
    itrfor(itr,G[v]){
        if( vis[*itr] == true){
            res.push_back(v);
            return res;
        }
        else{
            res = dfs(*itr);
            if(res.size() != 0){
                res.push_back(v);
                return res;
            }
        }
    }

    vis[v] = false;
    return res;
}

signed main(){
    int n,m;
    cin >> n >> m;
    REP(i,n+1) vis[i] = false;
    REP(i,m){
        int a,b;
        cin >> a >> b;
        G[a].push_back(b);
    }
    REP(i,n+1) check[i] = false;
    vector<int> heiro;
    FOR(i,1,n+1){
        if(check[i] == false){
            heiro = dfs(i);
            if(heiro.size() != 0) break;
        }
    }

    if(heiro.size() == 0){
        cout << -1 << endl;
        return 0;
    }

    Reverse(heiro);

    
    heiro.push_back(heiro[0]);

    vector<int> new_heiro;
    int cnt = 0;

    while(1){
        cnt ++ ;
        if(cnt == 100) break;
        REP(i,n+1) zan[i] = false;
        itrfor(itr,heiro) zan[*itr] = true;
        bool flag = true;
        new_heiro.clear();
        itrfor(itr,heiro){
            if(next(itr) == heiro.end()) break;
            bool hoge = false;
            itrfor(itr2,G[*itr]){
                if(zan[*itr2] == false) continue;
                if(*itr2 != *(next(itr)) ){
                    flag = false;
                    int from = *itr;
                    int to = *itr2;
                    if(from > to) swap(from,to);
                    bool flagg = true;
                    itrfor(itr3,heiro){
                        if( *itr3 == from){
                            flagg = false;
                        }
                        if(flagg) continue;
                        new_heiro.push_back(*itr3);
                        if( *itr3  == to){
                            hoge = true;
                            new_heiro.push_back(new_heiro[0]);
                            break;

                        }
                    }
                    hoge =true;
                    break;
                }
            }
            if(hoge) break;
        }
        if(flag) break;
        heiro.clear();
        itrfor(itr,new_heiro) heiro.push_back(*itr);
        if(flag) break;
    }
    if(heiro.size() <= 1){
        cout << -1 << endl;
        return 0;
    }
    cout << heiro.size() - 1 << endl;
    REP(i,heiro.size() - 1){
        cout << heiro[i] << endl;
    }


}
            itrfor(itr2,G[*itr]){
                if(zan[*itr2] == false) continue;
                if(*itr2 != *(next(itr)) ){
                    flag = false;
                    int from = *itr;
                    int to = *itr2;
                    if(from > to) swap(from,to);
                    bool flagg = true;
                    itrfor(itr3,heiro){
                        if( *itr3 == from){
                            flagg = false;
                        }
                        if(flagg) continue;
                        new_heiro.push_back(*itr3);
                        if( *itr3  == to){
                            hoge = true;
                            new_heiro.push_back(new_heiro[0]);
                            break;

                        }
                    }
                    hoge =true;
                    break;
                }
            }
            if(hoge) break;
        }
        if(flag) break;
        heiro.clear();
        itrfor(itr,new_heiro) heiro.push_back(*itr);
        if(flag) break;
    }
    if(heiro.size() <= 1){
        cout << -1 << endl;
        return 0;
    }
    cout << heiro.size() - 1 << endl;
    REP(i,heiro.size() - 1){
        cout << heiro[i] << endl;
    }


}

Compiler output:
a.cc:99:31: error: stray '#' in program
   99 |             bool hoge = false;#include <bits/stdc++.h>
      |                               ^
a.cc: In function 'bool Add(llong&, llong)':
a.cc:26:42: warning: no return statement in function returning non-void [-Wreturn-type]
   26 | bool Add(int &a,int b){a = (a + b) % MOD;}
      |                                          ^
a.cc: In function 'int main()':
a.cc:99:32: error: 'include' was not declared in this scope
   99 |             bool hoge = false;#include <bits/stdc++.h>
      |                                ^~~~~~~
a.cc:99:41: error: 'bits' was not declared in this scope
   99 |             bool hoge = false;#include <bits/stdc++.h>
      |                                         ^~~~
a.cc:99:46: error: 'stdc' was not declared in this scope; did you mean 'std'?
   99 |             bool hoge = false;#include <bits/stdc++.h>
      |                                              ^~~~
      |                                              std
a.cc:100:1: error: expected primary-expression before 'using'
  100 | using namespace std;
      | ^~~~~
a.cc:108:1: error: a template declaration cannot appear at block scope
  108 | template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
      | ^~~~~~~~
a.cc:122:1: error: a template declaration cannot appear at block scope
  122 | template<class T> bool chmax(T &a,  T b) {if(a<b){a=b; return 1;} return 0;}
      | ^~~~~~~~
a.cc:124:23: error: a function-definition is not allowed here before '{' token
  124 | bool Add(int &a,int b){a = (a + b) % MOD;}
      |                       ^
a.cc:125:31: error: a function-definition is not allowed here before '{' token
  125 | void myprint(int* A,int A_num){
      |                               ^
a.cc:139:23: error: a function-definition is not allowed here before '{' token
  139 | vector<int> dfs(int v){
      |                       ^
a.cc:161:12: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  161 | signed main(){
      |            ^~
a.cc:161:12: note: remove parentheses to default-initialize a variable
  161 | signed main(){
      |            ^~
      |            --
a.cc:161:12: note: or replace parentheses with braces to value-initialize a variable
a.cc:161:14: error: a function-definition is not allowed here before '{' token
  161 | signed main(){
      |              ^