1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <vector>

using namespace std;

int res[500005];
bool tempRes[500005];
bool isCycle;
int maxRes=0;
vector<int> result;
vector<vector<int> > g(1000000);

void DFSfindCycle(int v, int w, vector<int> & S, bool * visited)
{
    int u;

    visited[w] = true;
    S.push_back(w);
    for(int i = 0;i < g[w].size();++i)
    {
        u = g[w][i];

        if(u == v)
        {
            isCycle = true;
            ++maxRes;
            for(int i = 0;i < S.size();++i)
            {
                tempRes[S[i]]=1;
                ++res[S[i]];
                //cout<<S[i]+1<<" ";
            }
            //cout << endl;
        }
        if(!visited[u])
        {
            DFSfindCycle(v,u,S,visited);
        }
    }
    S.pop_back();
}


int main()
{
    ios_base::sync_with_stdio(0);
    int n,m,i,j,v1,v2,resultN=0,bad=0;
    bool * visited;
    vector<int> S,T;

    cin >> n >> m;
    for(i = 0; i < m; i++)
    {
        cin >> v1 >> v2;
        --v1;--v2;
        g[v1].push_back(v2);
    }

    visited = new bool [n];

    for(i = 0; i < n; i++)
    {
        //cout << i+1<<"!"<<endl;
        isCycle=0;
        for(j = 0; j < n; j++)
        {
            tempRes[j]=0;
            visited[j] = false;
        }

        DFSfindCycle(i,i,S,visited);
        if(!isCycle)
        {
            ++bad;
        }
        else
        {

            /*for(int i = 0;i < n;++i)
            {
                if(tempRes[i])
                    ++res[i];

            }*/
            while(!S.empty())
            {
                S.pop_back();
            }
        }
    }


    delete [] visited;

    if(bad == n)
    {
        cout << "NIE\n";
        return 0;
    }
    for(int i = 0;i<n;++i)
    {
        if(res[i] == maxRes)
        {
            ++resultN;
            result.push_back(i);
        }
    }

    cout << resultN << endl;
    for(int i = 0;i<result.size();++i)
    {
        cout << result[i]+1 << " ";
    }
    cout << endl;
    return 0;
}