Language: C++

#include <bits/stdc++.h>//Atcorder 142 
#include <vector>
#include <iostream>
#include <utility>
#define chmin(x, y) x = min(x , y);
#define chmax(x, y) x = max(x, y);
#define rep(i, j) for (int (i)=0;(i)<(j);(i)++)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
using namespace std;
typedef long long int ll;

const ll INF = 1e12;

int N, M;
vector<vector<int>> Graph[1001];
//bfsする。
vector<int> bfs(int sv){
    vector<int> dist(N, INF), pre(N, -1);
    vector<int> q;
    dist[sv] = 0;
    q.push_back(sv);
    //bfs
    while(!q.empty()){
        int v = q.front(); q.pop();
        for (int u : Graph[v]){
            if(dist[u] != INF) continue;//訪問済み
            pre[u] = v;//u からどこ
            dist[u] = dist[v]+1;//dist[u]はどの位の距離でsvからuに行けたのか示す
            q.push_back(u);
        }
    }
    //最小サイクルを探す.distにsvからのコストがあるので、dist[i]でGraph[i]にsvへの辺があるなら、それはサイクルであり、minをとる。
    pair<int ,int > best(INF, -1);
    rep(i,N){
        if(i == sv) continue;
        for(int u: Graph[i]){
            if(u == sv){
            best = min(best, make_pair(dist[u],u);//最小サイクルGET。dist[u]がコストであり、uがsvに至る直前の辺
            }
        }
    }
    if (best.first == INF) return vector<int>(n+1);
    int v = best.second;
    vector<int> res;
    //早速グラフをゲット！
    while (v != -1){
        res.push_back(v);
        v = pre[v]
    }
    return res;//svを親ノードにした最小グラフ（サイクル）を入手
}


void input() {
    cin >> N >> M;
    rep(i, M) {
        int a, b;cin >> a >> b;a--;b--;
        Graph[a].push_back(b);
    }
}

//bfsベクトルの中に処理系を設置する。nowに与えられたグラフを収納
void solve () {
    input();
    vector<int> ans;
    rep(i, n){
        vector<int> now = bfs(i);
        if(now.size() < ans.size()) ans = now;
    }
    if(ans.size() == N+1){
        cout <<-1 << endl;
        return ;
    }
    cout << ans.size() << endl;
    for (int v:ans){
        cout << v << endl;
    }
    return;
}

int main () {
    solve();
    return 0;
}



Compiler output:
a.cc: In function 'std::vector<int> bfs(int)':
a.cc:19:25: warning: overflow in conversion from 'll' {aka 'long long int'} to 'std::vector<int>::value_type' {aka 'int'} changes value from '1000000000000' to '-727379968' [-Woverflow]
   19 |     vector<int> dist(N, INF), pre(N, -1);
      |                         ^~~
a.cc:25:30: error: 'class std::vector<int>' has no member named 'pop'
   25 |         int v = q.front(); q.pop();
      |                              ^~~
a.cc:26:29: error: cannot convert 'std::vector<int>' to 'int' in initialization
   26 |         for (int u : Graph[v]){
      |                             ^
a.cc:37:27: error: cannot convert 'std::vector<int>' to 'int' in initialization
   37 |         for(int u: Graph[i]){
      |                           ^
a.cc:39:50: error: expected ')' before ';' token
   39 |             best = min(best, make_pair(dist[u],u);//最小サイクルGET。dist[u]がコストであり、uがsvに至る直前の辺
      |                       ~                          ^
      |                                                  )
a.cc:43:47: error: 'n' was not declared in this scope
   43 |     if (best.first == INF) return vector<int>(n+1);
      |                                               ^
a.cc:49:19: error: expected ';' before '}' token
   49 |         v = pre[v]
      |                   ^
      |                   ;
   50 |     }
      |     ~              
a.cc: In function 'void input()':
a.cc:59:27: error: no matching function for call to 'std::vector<std::vector<int> >::push_back(int&)'
   59 |         Graph[a].push_back(b);
      |         ~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; value_type = std::vector<int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'int' to 'const std::vector<std::vector<int> >::value_type&' {aka 'const std::vector<int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; value_type = std::vector<int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'int' to 'std::vector<std::vector<int> >::value_type&&' {aka 'std::vector<int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc: In function 'void solve()':
a.cc:67:12: error: 'n' was not declared in this scope
   67 |     rep(i, n){
      |            ^
a.cc:7:39: note: in definition of macro 'rep'
    7 | #define rep(i, j) for (int (i)=0;(i)<(j);(i)++)
      |                                       ^