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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//============================================================================
// Name        : .cpp
// Author      : Grzegorz Gajoch
// Description : 
//============================================================================

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>

using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(x, n) for(int x=0; x < (n); ++x)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define SIZE(n) (int)n.size()
#define ALL(c) c.begin(),c.end()
#define PB(n) push_back(n)
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<bool> VB;
#define MP make_pair
#define ST first
#define ND second
const int INF = 1000000001;

//#undef DEBUG
#ifdef DEBUG 
#define debug printf
#else
#define debug //
#endif

struct Node {
	int number;
	int visited;
    bool existing;
	vector< Node * > neighbours;
	Node() {
		this->number = -1;
		this->visited = false;
		this->neighbours.clear();
	}
};

int V, E;
vector< Node > G;

void read() {
	scanf("%d %d", &V, &E);
    Node dummy;
    dummy.visited = 0;
    dummy.existing = true;
    G.push_back(dummy);
	FOR(i, 1, V) {
        dummy.number = i;
        G.push_back(dummy);
    }
	int a, b;
	REP(i, E) {
		scanf("%d %d", &a, &b);
        G[a].neighbours.push_back(&G[b]);
	}
}

bool dfs(Node * node) { //true if cycle exist
    debug("dfs node %d\n",node->number);
    node->visited = 1;
    FOREACH(it, node->neighbours) {
        debug("neigh %d\n",(*it)->number);
        if( (*it)->existing ) {
            bool cyc = false;
            if ((*it)->visited == 1) {
                debug("cycle in %d\n", (*it)->number);
                return true;
            }
            if ((*it)->visited == 0) {
                if (dfs(*it))
                    return true;
            }
        }
    }
    node->visited = 2;
    return false;
}

void clean() {
    FOR(i, 1, V) {
        G[i].visited = false;
    }
}

bool do_dfs_all() {
    clean();
    bool res = false;
    FOR(i, 1, V) {
        if( G[i].existing ) {
            if( dfs(&G[i]) ) {
                res = true;
                return true; //cycle exists
            }
        }
    }
    return false;
}

vector<int> nodes_out;
int main(int argc, char **argv) {
	read();
    if( do_dfs_all() ) {
        debug("cycle exists\n");
    } else {
        printf("NIE\n");
        return 0;
    }
    FOR(i, 1, V) {
        debug("\n\n");
        debug("removing %d!\n", i);
        G[i].existing = false;
        if( do_dfs_all() ) {
            debug("%d: cycle exists\n", i);
        } else {
            nodes_out.push_back(i);
        }
        G[i].existing = true;
    }

    printf("%d\n",nodes_out.size());
    if( nodes_out.size() > 0 ) {
        FOREACH(it, nodes_out) {
            printf("%d ", *it);
        }
        printf("\n");
    }
    return 0;
}