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
117
#include <iostream>
#include <list>
 
using namespace std;
 
// Graph class represents a directed graph using adjacency list representation
class Graph
{
    list<int> *adj;    // Pointer to an array containing adjacency lists
    void DFSUtil(int v, bool visited[]);  // A function used by DFS
public:
    int V;    // No. of vertices
    Graph(int V);   // Constructor
    void addEdge(int v, int w);   // function to add an edge to graph
    void DFS(int v);    // DFS traversal of the vertices reachable from v
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
int ile = 0;
bool *required;
bool *DFSed;

void Graph::DFSUtil(int v, bool visited[])
{
    visited[v] = true;
    //cout<<"DFS("<<v<<")"<<endl;
    DFSed[v]=1;
    // Mark the current node as visited and print it
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[v].begin(); i != adj[v].end(); ++i){
        if(visited[*i]){ // mamy cykl
            for (int j = 0; j < V; ++j)
            {
                 required[j]=required[j]&visited[j];
                 //if(visited[j])cout<<j<<" ";
            }
            //cout<<endl;
            ile++;
        }
        else{
            DFSUtil(*i, visited);
            }
    }
    visited[v]=false;
}

// DFS traversal of the vertices reachable from v. It uses recursive DFSUtil()
void Graph::DFS(int v)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++) visited[i] = false;
 
    // Call the recursive helper function to print DFS traversal
    DFSUtil(v, visited);
}
 
int n,m, v1, v2;
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n>>m;
    // Create a graph given in the above diagram
    Graph g(n);
    for (int i = 0; i < m; ++i)
    {
       cin>>v1>>v2;
        v1--;
        v2--;
        g.addEdge(v1, v2);
    }


    required = new bool[g.V];
    DFSed = new bool[g.V];
    for(int i = 0; i < g.V; i++){
        required[i] = 1;
        DFSed[i]=0;
    }

    
    for (int i = 0; i < n; ++i)
    {
        //if(!DFSed[i]){
             g.DFS(i);
            // cout<<"DFS("<<i<<endl;
         //}
    }

    int ilef=0;
    for (int i = 0; i < g.V; ++i)
    {
        if (required[i])
        {
            ilef++;
        }
    }

    if(ile>0){
        cout<<ilef<<endl;
        for(int i = 0; i < g.V; i++) if(required[i]) cout<<i+1<<" "; 
    }
    else cout<<"NIE"<<endl;
 
    return 0;
}