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
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

public class kon {

    public static void main(String[] args) {
        InputStream in = System.in;
        PrintStream out = System.out;
        calculate(in, out);
    }

    public static int calculate(InputStream in, PrintStream out) {
        /** Read input */
        Scanner scanner = new Scanner(in);
        int n = Integer.parseInt(scanner.next());
        int m = Integer.parseInt(scanner.next());

        City[] cities = new City[n];
        for (int i = 0; i < n; i++) {
            cities[i] = new City(i + 1);
        }

        Connection[] connections = new Connection[m];
        for (int i = 0; i < m; i++) {
            scanner.nextLine();
            int a = Integer.parseInt(scanner.next());
            int b = Integer.parseInt(scanner.next());
            Connection connection = new Connection(cities[a - 1], cities[b - 1]);
            connections[i] = connection;
            cities[a - 1].out.add(connection);
            cities[b - 1].in.add(connection);
        }

        /** Find */

        List<List<Integer>> components = new SCCTarjan().scc(cities);
        List<List<Integer>> cyclesSet = new ArrayList<>();
        for (List<Integer> component: components) {
            if (component.size() > 1) {
                cyclesSet.add(component);
            }
        }

        if (cyclesSet.size() == 0) {
            out.println("NIE");
            return 0;
        }

        if (cyclesSet.size() > 1) {
            out.println(0);
            return 0;
        }

        boolean found = false;
        int cycle_cities[] = new int[0];
        int cycle_cities_length = 0;


        for (Integer cityNo: cyclesSet.get(0)) {
            City city = cities[cityNo - 1];
            for (Connection connection: city.out) {
                if (!connection.checkedCycle) {
                    int[] cycle = findMinimalCycle(connection);
                    if (cycle != null) {
                        radixsort(cycle);

                        if (found) {
                            cycle_cities_length = interesectCycles(cycle_cities, cycle_cities_length, cycle);
                        } else {
                            found = true;
                            cycle_cities = cycle;
                            cycle_cities_length = cycle.length;
                        }
                    }
                }
            }
        }

        /** Save result */
        writeResult(out, found, cycle_cities, cycle_cities_length);

        return 0;
    }

    private static void writeResult(PrintStream out, boolean found, int[] cities, int cities_length) {
        if (found) {
            out.println(cities_length);
            for (int i = 0; i < cities_length; i++) {
                out.print(cities[i] + " ");
            }
        } else {
            out.print("NIE");
        }
    }

    private static int interesectCycles(int[] intersection, int intersection_length, int[] other) {
        int i = 0;
        int j = 0;
        int k = 0;
        while (i < intersection_length && j < other.length) {
            while (j < other.length && other[j] < intersection[i]) {
                j++;
            }
            if (j < other.length && other[j] == intersection[i]) {
                intersection[k] = intersection[i];
                j++;
                k++;
            }
            i++;
        }

        return k;
    }



    private static int[] findMinimalCycle(Connection startConnection) {
        Queue<Element> queue = new LinkedList<>();
        queue.add(new Element(startConnection, null));
        Element element;

        while (!queue.isEmpty()) {
            element = queue.remove();
            Connection previous = element.connection;

            previous.visitedRepresentant = startConnection;

            for (Connection next: previous.to.out) {
                if (next.equals(startConnection) && startConnection.equals(next.visitedRepresentant)) {
                    return getCycle(new Element(next, element));
                }
                if (!startConnection.equals(next.visitedRepresentant)) {
                    next.visitedRepresentant = startConnection;
                    queue.add(new Element(next, element));
                }
            }
        }

        return null;
    }

    private static int[] getCycle(Element element) {
        List<Integer> cities = new ArrayList<>();
        Element el = element;
        while (el.previous != null) {
            el.connection.checkedCycle = true;
            cities.add(el.connection.to.no);
            el = el.previous;
        }

        int[] result = new int[cities.size()];
        int i = 0;
        for (Integer city: cities) {
            result[i++] = city;
        }
        return result;
    }


    private static void radixsort(int[] input) {
        final int RADIX = 10;
        // declare and initialize bucket[]
        List<Integer>[] bucket = new ArrayList[RADIX];
        for (int i = 0; i < bucket.length; i++) {
            bucket[i] = new ArrayList<Integer>();
        }

        // sort
        boolean maxLength = false;
        int tmp = -1, placement = 1;
        while (!maxLength) {
            maxLength = true;
            // split input between lists
            for (Integer i : input) {
                tmp = i / placement;
                bucket[tmp % RADIX].add(i);
                if (maxLength && tmp > 0) {
                    maxLength = false;
                }
            }
            // empty lists into input array
            int a = 0;
            for (int b = 0; b < RADIX; b++) {
                for (Integer i : bucket[b]) {
                    input[a++] = i;
                }
                bucket[b].clear();
            }
            // move to next digit
            placement *= RADIX;
        }
    }

    private static class Element {
        Connection connection;
        Element previous;

        private Element(Connection connectgion, Element previous) {
            this.connection = connectgion;
            this.previous = previous;
        }
    }

    private static class Connection {
        City from;
        City to;
        Connection visitedRepresentant = null;
        boolean checkedCycle = false;

        private Connection(City from, City to) {
            this.from = from;
            this.to = to;
        }
    }

    private static class City {
        int no;
        List<Connection> in = new ArrayList<>();
        List<Connection> out = new ArrayList<>();

        private City(int no) {
            this.no = no;
        }
    }

    /** ================================================================================== */

    public static class Call {
        int u;
        boolean isComponentRoot;
        int c;

        public Call(int u) {
            this.u = u;
            this.c = -1;
        }
    }

    public static class SCCTarjan {

        City[] graph;
        boolean[] visited;
        Stack<Integer> stack;
        int time;
        int[] lowlink;
        List<List<Integer>> components;

        public List<List<Integer>> scc(City[] graph) {
            int n = graph.length;
            this.graph = graph;
            visited = new boolean[n];
            stack = new Stack<>();
            time = 0;
            lowlink = new int[n];
            components = new ArrayList<>();

            for (int u = 0; u < n; u++)
                if (!visited[u]) {
                    dfs_iterative(u);
                }
            return components;
        }

        void dfs_iterative(int u) {
            Stack<Call> calls = new Stack<>();

            Call call = new Call(u);
            calls.push(call);

            boolean wasCall = false;

            while (!calls.isEmpty()) {
                call = calls.pop();
                wasCall = false;

                if (call.c == -1) {
                    lowlink[call.u] = time++;
                    visited[call.u] = true;
                    stack.add(call.u);
                    call.isComponentRoot = true;
                    call.c = 0;
                };

                while (!wasCall && call.c < graph[call.u].out.size()) {
                    Connection con = graph[call.u].out.get(call.c);

                    int v = con.to.no - 1;
                    if (!visited[v]) {
                        calls.push(call);
                        calls.push(new Call(v));
                        wasCall = true;
                        break;
                    }
                    if (lowlink[call.u] > lowlink[v]) {
                        lowlink[call.u] = lowlink[v];
                        call.isComponentRoot = false;
                    }

                    call.c++;
                }


                if (!wasCall && call.isComponentRoot) {
                    List<Integer> component = new ArrayList<>();
                    while (true) {
                        int x = stack.pop();
                        component.add(x + 1);
                        lowlink[x] = Integer.MAX_VALUE;
                        if (x == call.u)
                            break;
                    }
                    components.add(component);
                }
            }
        }

        void dfs(int u) {

            lowlink[u] = time++;
            visited[u] = true;
            stack.add(u);
            boolean isComponentRoot = true;

            for (Connection c : graph[u].out) {
                int v = c.to.no - 1;
                if (!visited[v]) {
                    dfs(v);
                }
                if (lowlink[u] > lowlink[v]) {
                    lowlink[u] = lowlink[v];
                    isComponentRoot = false;
                }
            }

            if (isComponentRoot) {
                List<Integer> component = new ArrayList<>();
                while (true) {
                    int x = stack.pop();
                    component.add(x + 1);
                    lowlink[x] = Integer.MAX_VALUE;
                    if (x == u)
                        break;
                }
                components.add(component);
            }
        }

    }

}