Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <string>
#include <set>
#include <unordered_map>

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T>
using Vvec = vector<vector<T> >;

class UnionFind{ //連結判定
private:
    std::vector<int> parent; //parent[i] = parent of node i
    std::vector<int> rank;
    std::vector<int> size;
public:
    //constructor
    UnionFind(int N){
        for(int i=0; i<N; i++) parent.push_back(i); //parent = [0,1,..,N-1]
        for(int i=0; i<N; i++) rank.push_back(0); //rank = [0,0,..,0]
        for(int i=0; i<N; i++) size.push_back(1);
    }
    //initialize
    void init(int N){
        parent.clear();
        rank.clear();
        size.clear();
        for(int i=0; i<N; i++) parent.push_back(i); //parent = [0,1,..,N-1]
        for(int i=0; i<N; i++) rank.push_back(0); //rank = [0,0,..,0]
        for(int i=0; i<N; i++) size.push_back(1);
    }
    int root(int a){
        if(parent[a] == a) return a;
        return parent[a] = root(parent[a]); //親が自分自身になるまで再帰を呼び出す & 経路圧縮 
    }
    void unite(int a, int b){
        int rootA = root(a);
        int rootB = root(b);
        if(rootA == rootB) return; //ノードa, bがすでに連結しているとき
        //次数の大きい方を親として連結
        if(rank[rootA] < rank[rootB]){
            size[rootB] += size[rootA];
            parent[rootA] = rootB;
        }
        else{
            size[rootA] += size[rootB];
            parent[rootB] = rootA;
            if(rank[rootA] == rank[rootB]) rank[rootA]++;
        }
    }
    bool same(int a, int b){
        return root(a) == root(b); //ノードa, bが同じ木に属するかどうか
    }
    int get_size(int a){
        return size[root(a)];
    }
};

int dfs(Vvec<int>& edge, vector<int>& vis, int from, int tmp, vector<int>& ans)
{
    // cout << "here is " << tmp << endl;
    if(vis[tmp]){return tmp;}

    
    if(from != -1) vis[tmp] = vis[from]+1;
    else vis[tmp] = 1;
    int ret = -1;
    bool flag = false;

    int ord = 0;
    for(int& to: edge) ord = max(ord, vis[to]);
    if(ord!=0){
    for(int& to: edge[tmp]){
        if(vis[to] == ord){ans.push_back(tmp); return to;}
    }
    }
    for(int& to: edge[tmp]){
        if(to != from){
            flag = true;
            ret = dfs(edge, vis, tmp, to, ans);
            // cout << "\t" << to << " " << ret << endl;
            if(ret == -2) return ret;
            if(ret != -1){
                ans.push_back(tmp);
                if(tmp == ret) return -2;
                else return ret;
            }
        }
    }

    vis[tmp] = false;
    if(flag) return -1;
    return ret;
}

int main()
{
    int N, M;
    cin >> N >> M;
    Vvec<int> edge(N);
    for(int i=0; i<M; i++){
        int u, v;
        scanf(" %d %d", &u, &v); u--; v--;
        edge[u].push_back(v);
    }

    if(N==1){cout << -1 << endl; return 0;}

    // UnionFind uf(N);
    // for(int i=0; i<N; i++){
    //     for(int& v: edge[i]) uf.unite(i, v);
    // }

    // unordered_map<int, int> mp;
    // for(int i=0; i<N; i++){
    //     auto itr = mp.find(uf.root(i));
    //     if(itr == mp.end()) mp[uf.root(i)];
    // }

    vector<int> ans;
    for(int i=0; i<N; i++){
        if(ans.size() != 0) break;
        vector<int> vis(N, 0);
        int ret = dfs(edge, vis, -1, 0, ans);
    }

    if(ans.size() == 0){cout << -1 << endl; return 0;}
    cout << ans.size() << endl;
    for(int& v: ans) cout << v+1 << endl;
}

Compiler output:
a.cc: In function 'int dfs(Vvec<int>&, std::vector<int>&, int, int, std::vector<int>&)':
a.cc:77:18: error: invalid initialization of reference of type 'int&' from expression of type 'std::vector<int>'
   77 |     for(int& to: edge) ord = max(ord, vis[to]);
      |                  ^~~~