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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
using namespace std;

const int WHITE = 0, GREY = 1, BLACK = 2;
typedef pair<int, int> PAIR;

struct Graph {
    int size;
    int time;
    bool cycle_found;
    int start_d, end_d, start_f, end_f;
    vector<list<int> > adj;
    vector<int> color, d, f, parent, tree;
    vector<PAIR>  excludes;

    Graph(int n) {
        size = n;
        adj.resize(n);
        d.resize(n, 10 * n);
        f.resize(n, 10 * n);
        parent.resize(n, -1);
        color.resize(n, WHITE);
        tree.resize(n, -1);
    }

    void add(int a, int b) {
        adj[a].push_back(b);
    }

    void dfs() {
        time = 0;
        for(int u = 0; u < size; ++u) {
            if(color[u] == WHITE) {
                visit(u);
            }
        }
    }

    void visit(int root) {
        stack<int> st;
        st.push(root);
        while(!st.empty()) {
            int u = st.top();
            if(color[u] == WHITE) {
                color[u] = GREY;
                d[u] = time++;
                for(int v : adj[u]) {
                    if(color[v] == WHITE) {
                        parent[v] = u;
                        st.push(v);
                    }
                }
            } else {
                color[u] = BLACK;
                f[u] = time++;
                st.pop();
                continue;
            }
        }
    }

    void dfs2(Graph &G) {
        time = 0;
        start_d = -1;
        end_d = 10 * size;
        start_f = -1;
        end_f = 10 * size;
        cycle_found = false;
        vector<int> nodes;
        for(int i = 0; i < size; ++i) {
            nodes.push_back(i);
        }
        sort(nodes.begin(), nodes.end(), [&G](int a, int b)->bool { return G.f[a] > G.f[b]; });
        for(int u : nodes) {
            if(color[u] == WHITE) {
                visit2(u);
            }
        }
    }

    void visit2(int root) {
        stack<int> st;
        st.push(root);
        vector<int> end_f_candidates;
        while(!st.empty()) {
            int u = st.top();
            if(color[u] == WHITE) {
                //cout << "enter " << u << endl;
                color[u] = GREY;
                d[u] = time++;
                tree[u] = root;
                for(int v : adj[u]) {
                    //cout << "adj " << u << " " << v << " col[v]=" << color[v] << endl;
                    if(color[v] == WHITE) {
                        parent[v] = u;
                        st.push(v);
                    } else if(color[v] == GREY) {
                        cycle_found = true;
                        start_d = max(start_d, d[v]);
                        end_d = min(end_d, d[u]);
                        start_f = max(start_f, d[u]);
                        end_f_candidates.push_back(v);
                    } else {
                        if(tree[u] == tree[v]) {
                            int anc = find_grey_ancestor(v);
                            if(d[anc] + 1 < d[v]) {
                                PAIR ex = make_pair(d[anc] + 1, d[v] - 1);
                                excludes.push_back(ex);
                                //cout << "excl " << ex.first << " " << ex.second << "\n";
                            }
                        }
                    }
                }
            } else if(color[u] == GREY) {
                color[u] = BLACK;
                f[u] = time++;
                st.pop();
                //cout << u << " d=" << d[u] << " f=" << f[u] << endl;
                continue;
            } else {
                st.pop();
            }
        }
        for(int cand : end_f_candidates) {
            end_f = min(end_f, f[cand]);
        }
    }

    int find_grey_ancestor(int v) {
        stack<int> st;
        int par;
        while(true) {
            par = parent[v];
            if(color[par] == GREY) {
                while(!st.empty()) {
                    int u = st.top();
                    st.pop();
                    parent[u] = par;
                }
                return par;
            } else {
                st.push(v);
                v = par;
            }
        }
        return -1;
    }

    vector<int>* calc_results() {
        sort(excludes.begin(), excludes.end(), [](PAIR a, PAIR b)->bool {
            if(a.first < b.first) return true;
            if(a.first > b.first) return false;
            return a.second <= b.second;
        });
        stack<PAIR> st;
        for(PAIR curr: excludes) {
            if(st.empty()) {
                st.push(curr);
                continue;
            }
            PAIR top = st.top();
            if(curr.first < top.second) {
                st.pop();
                PAIR merged = make_pair(top.first, max(top.second, curr.second));
                st.push(merged);
            } else {
                st.push(curr);
            }
        }
        vector<bool> is_ok;
        is_ok.resize(size, true);
        vector<int> rev_d;
        rev_d.resize(2 * size);
        for(int v = 0; v < size; ++v) {
            rev_d[d[v]] = v;
        }
        while(!st.empty()) {
            PAIR curr = st.top();
            st.pop();
            //cout << "final excl " << curr.first << " " << curr.second << "\n";
            for(int dv = curr.first; dv <= curr.second; ++dv) {
                int v = rev_d[dv];
                is_ok[v] = false;
            }
        }

        vector<int> *results = new vector<int>;
        //cout << "startd/endd " << start_d << " " << start_f << endl;
        for(int v = 0; v < size; ++v) {
            if(is_ok[v] && d[v] >= start_d && d[v] <= end_d && f[v] >= start_f && f[v] <= end_f) {
                results->push_back(v);
            }
        }
        return results;
    }
};


int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    Graph G(n), Gt(n);
    for(int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        G.add(a, b);
        Gt.add(b, a);
    }
    G.dfs();
    Gt.dfs2(G);
    if(!Gt.cycle_found) {
        cout << "NIE\n";
    } else {
        vector<int> *results = Gt.calc_results();
        cout << results->size() << "\n";
        for(int u : (*results)) {
            cout << (u + 1) << " ";
        }
        if(results->size() > 0) {
            cout << "\n";
        }
        delete results;
    }
    return 0;
}