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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class kon {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        KON solver = new KON();
        solver.solve(1, in, out);
        out.close();
    }

    static class KON {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int M = in.nextInt();
            List<Integer> adj[] = new LinkedList[N];
            for (int i = 0; i < N; i++) adj[i] = new LinkedList<>();
            for (int i = 0; i < M; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt() - 1;
                adj[a].add(b);
            }

            int adjList[][] = new int[N][];
            for (int i = 0; i < N; i++) {
                adjList[i] = new int[adj[i].size()];
                int cnt = 0;
                for (Integer v : adj[i]) {
                    adjList[i][cnt++] = v;
                }
            }

            Integer graphNodes[] = new Integer[N];
            for (int i = 0; i < N; i++) graphNodes[i] = i;

            ElementaryCyclesSearch search = new ElementaryCyclesSearch(adjList, graphNodes);
            List elementaryCycles = search.getElementaryCycles();
            int counter[] = new int[N];
            for (Object cycle : elementaryCycles) {
                List c = (List) cycle;
                for (Integer v : (List<Integer>) c) {
                    counter[v]++;
                }
            }
            List<Integer> ans = new LinkedList<>();
            for (int i = 0; i < N; i++) {
                if (counter[i] == elementaryCycles.size()) ans.add(i);
            }
            if (elementaryCycles.size() == 0) {
                out.println("NIE");
            } else {
                out.println(ans.size());
                if (ans.size() > 0) {
                    for (Integer v : ans) {
                        out.print((v + 1) + " ");
                    }
                    out.println();
                }
            }

        }

    }

    static class StrongConnectedComponents {
        private int[][] adjListOriginal = null;
        private int[][] adjList = null;
        private boolean[] visited = null;
        private Vector stack = null;
        private int[] lowlink = null;
        private int[] number = null;
        private int sccCounter = 0;
        private Vector currentSCCs = null;

        public StrongConnectedComponents(int[][] adjList) {
            this.adjListOriginal = adjList;
        }

        public SCCResult getAdjacencyList(int node) {
            this.visited = new boolean[this.adjListOriginal.length];
            this.lowlink = new int[this.adjListOriginal.length];
            this.number = new int[this.adjListOriginal.length];
            this.visited = new boolean[this.adjListOriginal.length];
            this.stack = new Vector();
            this.currentSCCs = new Vector();

            this.makeAdjListSubgraph(node);

            for (int i = node; i < this.adjListOriginal.length; i++) {
                if (!this.visited[i]) {
                    this.getStrongConnectedComponents(i);
                    Vector nodes = this.getLowestIdComponent();
                    if (nodes != null && !nodes.contains(new Integer(node)) && !nodes.contains(new Integer(node + 1))) {
                        return this.getAdjacencyList(node + 1);
                    } else {
                        Vector[] adjacencyList = this.getAdjList(nodes);
                        if (adjacencyList != null) {
                            for (int j = 0; j < this.adjListOriginal.length; j++) {
                                if (adjacencyList[j].size() > 0) {
                                    return new SCCResult(adjacencyList, j);
                                }
                            }
                        }
                    }
                }
            }

            return null;
        }

        private void makeAdjListSubgraph(int node) {
            this.adjList = new int[this.adjListOriginal.length][0];

            for (int i = node; i < this.adjList.length; i++) {
                Vector successors = new Vector();
                for (int j = 0; j < this.adjListOriginal[i].length; j++) {
                    if (this.adjListOriginal[i][j] >= node) {
                        successors.add(new Integer(this.adjListOriginal[i][j]));
                    }
                }
                if (successors.size() > 0) {
                    this.adjList[i] = new int[successors.size()];
                    for (int j = 0; j < successors.size(); j++) {
                        Integer succ = (Integer) successors.get(j);
                        this.adjList[i][j] = succ.intValue();
                    }
                }
            }
        }

        private Vector getLowestIdComponent() {
            int min = this.adjList.length;
            Vector currScc = null;

            for (int i = 0; i < this.currentSCCs.size(); i++) {
                Vector scc = (Vector) this.currentSCCs.get(i);
                for (int j = 0; j < scc.size(); j++) {
                    Integer node = (Integer) scc.get(j);
                    if (node.intValue() < min) {
                        currScc = scc;
                        min = node.intValue();
                    }
                }
            }

            return currScc;
        }

        private Vector[] getAdjList(Vector nodes) {
            Vector[] lowestIdAdjacencyList = null;

            if (nodes != null) {
                lowestIdAdjacencyList = new Vector[this.adjList.length];
                for (int i = 0; i < lowestIdAdjacencyList.length; i++) {
                    lowestIdAdjacencyList[i] = new Vector();
                }
                for (int i = 0; i < nodes.size(); i++) {
                    int node = ((Integer) nodes.get(i)).intValue();
                    for (int j = 0; j < this.adjList[node].length; j++) {
                        int succ = this.adjList[node][j];
                        if (nodes.contains(new Integer(succ))) {
                            lowestIdAdjacencyList[node].add(new Integer(succ));
                        }
                    }
                }
            }

            return lowestIdAdjacencyList;
        }

        private void getStrongConnectedComponents(int root) {
            this.sccCounter++;
            this.lowlink[root] = this.sccCounter;
            this.number[root] = this.sccCounter;
            this.visited[root] = true;
            this.stack.add(new Integer(root));

            for (int i = 0; i < this.adjList[root].length; i++) {
                int w = this.adjList[root][i];
                if (!this.visited[w]) {
                    this.getStrongConnectedComponents(w);
                    this.lowlink[root] = Math.min(lowlink[root], lowlink[w]);
                } else if (this.number[w] < this.number[root]) {
                    if (this.stack.contains(new Integer(w))) {
                        lowlink[root] = Math.min(this.lowlink[root], this.number[w]);
                    }
                }
            }

            // found scc
            if ((lowlink[root] == number[root]) && (stack.size() > 0)) {
                int next = -1;
                Vector scc = new Vector();

                do {
                    next = ((Integer) this.stack.get(stack.size() - 1)).intValue();
                    this.stack.remove(stack.size() - 1);
                    scc.add(new Integer(next));
                } while (this.number[next] > this.number[root]);

                // simple scc's with just one node will not be added
                if (scc.size() > 1) {
                    this.currentSCCs.add(scc);
                }
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }

    static class SCCResult {
        private Set nodeIDsOfSCC = null;
        private Vector[] adjList = null;
        private int lowestNodeId = -1;

        public SCCResult(Vector[] adjList, int lowestNodeId) {
            this.adjList = adjList;
            this.lowestNodeId = lowestNodeId;
            this.nodeIDsOfSCC = new HashSet();
            if (this.adjList != null) {
                for (int i = this.lowestNodeId; i < this.adjList.length; i++) {
                    if (this.adjList[i].size() > 0) {
                        this.nodeIDsOfSCC.add(new Integer(i));
                    }
                }
            }
        }

        public Vector[] getAdjList() {
            return adjList;
        }

        public int getLowestNodeId() {
            return lowestNodeId;
        }

    }

    static class ElementaryCyclesSearch {
        private List cycles = null;
        private int[][] adjList = null;
        private Object[] graphNodes = null;
        private boolean[] blocked = null;
        private Vector[] B = null;
        private Vector stack = null;

        public ElementaryCyclesSearch(int[][] adjacencyList, Object[] graphNodes) {
            this.graphNodes = graphNodes;
            this.adjList = adjacencyList;
        }

        public List getElementaryCycles() {
            this.cycles = new Vector();
            this.blocked = new boolean[this.adjList.length];
            this.B = new Vector[this.adjList.length];
            this.stack = new Vector();
            StrongConnectedComponents sccs = new StrongConnectedComponents(this.adjList);
            int s = 0;

            while (true) {
                SCCResult sccResult = sccs.getAdjacencyList(s);
                if (sccResult != null && sccResult.getAdjList() != null) {
                    Vector[] scc = sccResult.getAdjList();
                    s = sccResult.getLowestNodeId();
                    for (int j = 0; j < scc.length; j++) {
                        if ((scc[j] != null) && (scc[j].size() > 0)) {
                            this.blocked[j] = false;
                            this.B[j] = new Vector();
                        }
                    }

                    this.findCycles(s, s, scc);
                    s++;
                } else {
                    break;
                }
            }

            return this.cycles;
        }

        private boolean findCycles(int v, int s, Vector[] adjList) {
            boolean f = false;
            this.stack.add(new Integer(v));
            this.blocked[v] = true;

            for (int i = 0; i < adjList[v].size(); i++) {
                int w = ((Integer) adjList[v].get(i)).intValue();
                // found cycle
                if (w == s) {
                    Vector cycle = new Vector();
                    for (int j = 0; j < this.stack.size(); j++) {
                        int index = ((Integer) this.stack.get(j)).intValue();
                        cycle.add(this.graphNodes[index]);
                    }
                    this.cycles.add(cycle);
                    f = true;
                } else if (!this.blocked[w]) {
                    if (this.findCycles(w, s, adjList)) {
                        f = true;
                    }
                }
            }

            if (f) {
                this.unblock(v);
            } else {
                for (int i = 0; i < adjList[v].size(); i++) {
                    int w = ((Integer) adjList[v].get(i)).intValue();
                    if (!this.B[w].contains(new Integer(v))) {
                        this.B[w].add(new Integer(v));
                    }
                }
            }

            this.stack.remove(new Integer(v));
            return f;
        }

        private void unblock(int node) {
            this.blocked[node] = false;
            Vector Bnode = this.B[node];
            while (Bnode.size() > 0) {
                Integer w = (Integer) Bnode.get(0);
                Bnode.remove(0);
                if (this.blocked[w.intValue()]) {
                    this.unblock(w.intValue());
                }
            }
        }

    }
}