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
#include <algorithm>
#include <cassert>
#include <cctype>
#include <random>
#include <unistd.h>
#include <utility>
#include <vector>
using namespace std;

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

class Output {
 public:
  void Flush();
  Output():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {}
  ~Output() { Flush(); }
  void Put(char c);
  void Put(unsigned x);
  void Put(int x);
  void Put(const char*s);

  void operator()() {}
  template<class Arg, class... Args> void operator()(const Arg &arg, const Args &... args) {
    Put(arg);
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *BUFLIMIT;
};

void Output::Flush() {
  char *p = buffer;
  while(p < bufpos) {
    p += write(1, p, bufpos-p);
  }
  bufpos = buffer;
}

inline void Output::Put(char c) {
  *bufpos = c;
  ++bufpos;
  if(bufpos >= BUFLIMIT) Flush();
}

void Output::Put(unsigned x) {
  char *old = bufpos;
  do {
    *bufpos = char('0' + x % 10u);
    x /= 10u;
    ++bufpos;
  } while(x);
  reverse(old, bufpos);
  if(bufpos >= BUFLIMIT) Flush();
}

void Output::Put(int x) {
  if(x<0) {
    Put('-'); Put(-static_cast<unsigned>(x));
  } else {
    Put(static_cast<unsigned>(x));
  }
}

void Output::Put(const char*s) {
  while(*s) Put(*s++);
}

Input IN;
Output OUT;

// ----------

struct EmptyIntersection {};

// 44 b
struct Node {
  int degree;
  Node **neighbors;
  Node *parent;
  int depth;
  int low;
  int nextNeighbor;
  Node *shortcut;

  Node *fuParent;
  int fuRank;
  Node *ancestor;

  bool onPath;
  bool onIntersection;
  Node() : degree{0}, neighbors{nullptr}, parent{nullptr},
           depth{-1}, low{-1}, nextNeighbor{0}, shortcut{nullptr},
           fuParent{nullptr}, fuRank{0}, ancestor{nullptr},
           onPath(false), onIntersection(false) {}
};

vector<Node> nodes; // 22 MB
vector<pair<Node*,Node*>> edges; // 8 MB
vector<Node*> neighborStorage;   // 4 MB
Node *cycleBottom;
Node *cycleTop;

void ReadInput() {
  int n,m;
  IN(n,m);
  nodes.resize(n);
  edges.resize(m);
  for (auto &e : edges) {
    int a, b;
    IN(a,b);
    e.first = &nodes[a-1];
    e.second = &nodes[b-1];
    ++e.first->degree;
  }
}

void ComputeNeighbors() {
  neighborStorage.resize(edges.size());

  // Prepare to fill backwards.
  Node **nextStorage = &neighborStorage[0];
  for (Node &node : nodes) {
    nextStorage += node.degree;
    node.neighbors = nextStorage;
  }
  assert(nextStorage == &neighborStorage[0] + neighborStorage.size());

  // Fill.
  for (const auto &e : edges) {
    *(--e.first->neighbors) = e.second;
  }
}

inline Node *FuFind(Node *p) {
  Node *q = p;
  while (q->fuParent) q = q->fuParent;
  while (p != q) {
    Node *next = p->fuParent;
    p->fuParent = q;
    p = next;
  }
  return q;
}

inline Node *FuUnion(Node *a, Node *b) {
  a = FuFind(a);
  b = FuFind(b);
  assert(a != b);
  if (a->fuRank == b->fuRank) {
    ++a->fuRank;
  } else if (a->fuRank < b->fuRank) {
    swap(a,b);
  }
  b->fuParent = a;
  return a;
}

inline Node *FindAncestor(Node *a) {
  return FuFind(a)->ancestor;
}

void ShiftCycleBottom(Node *p) {
  while (cycleBottom != p) {
    if (cycleBottom == cycleTop) {
      cycleBottom = nullptr;
      cycleTop = nullptr;
      break;
    }
    if (cycleBottom->shortcut && cycleBottom->shortcut->depth < p->depth) {
      p = cycleBottom->shortcut;
    }
    cycleBottom = cycleBottom->parent;
  }
}

void NewShortcut(Node *a, Node *b) {
  if (b->depth > cycleBottom->depth) {
    return;
  } else if (a->depth > cycleBottom->depth) {
    ShiftCycleBottom(b);
    if (!cycleBottom) throw EmptyIntersection{};
  } if (!a->shortcut || b->depth < a->shortcut->depth) {
    a->shortcut = b;
  }
}

void Walk(Node *current) {
  current->depth = 0;
  current->low = 0;
  current->onPath = true;
  current->ancestor = current;

  while (current) {
    if (current->nextNeighbor < current->degree) {
      Node *neighbor = current->neighbors[current->nextNeighbor];
      if (neighbor->depth == -1) {
        // Tree edge.
        neighbor->parent = current;
        neighbor->depth = current->depth + 1;
        neighbor->low = neighbor->depth;
        neighbor->onPath = true;
        neighbor->ancestor = neighbor;
        current = neighbor;
      } else if (neighbor->onPath) {
        // Back edge.
        if (!cycleBottom) {
          // First cycle.
          cycleBottom = current;
          cycleTop = neighbor;
        } else {
          // Another cycle.
          Node *commonAncestor = FindAncestor(cycleBottom);
          if (!commonAncestor ||
              commonAncestor->depth < cycleTop->depth ||
              commonAncestor->depth < neighbor->depth) {
            // Disjoint cycles.
            throw EmptyIntersection{};
          }
          if (neighbor->depth > cycleTop->depth) {
            cycleTop = neighbor;
          }
          ShiftCycleBottom(commonAncestor);
          if (!cycleBottom) {
            throw EmptyIntersection{};
          }
        }
        current->low = std::min(current->low, neighbor->depth);
        ++current->nextNeighbor;
      } else {
        // Cross edge.
        Node *commonAncestor = FindAncestor(neighbor);
        if (commonAncestor && neighbor->low <= commonAncestor->depth) {
          NewShortcut(neighbor, commonAncestor);
          current->low = std::min(current->low, neighbor->low);
        }
        ++current->nextNeighbor;
      }
    } else {
      // No more neighbors.
      Node *parent = current->parent;
      if (parent) {
        parent->low = std::min(parent->low, current->low);
        parent->nextNeighbor++;
        FuUnion(current, parent)->ancestor = parent;
      } else {
        FuFind(current)->ancestor = nullptr;
      }
      current->onPath = false;
      current = parent;
    }
  }
}

void Solve() {
  cycleBottom = nullptr;
  cycleTop = nullptr;
  for (Node &root : nodes) {
    if (root.depth == -1) Walk(&root);
  }
}

int MarkIntersection() {
  int res = 0;
  for(;;) {
    cycleBottom->onIntersection = true;
    ++res;
    if (!cycleBottom->parent) break;
    ShiftCycleBottom(cycleBottom->parent);
    if (!cycleBottom) break;
  }
  return res;
}

void PrintIntersection() {
  for (int i=0; i<int(nodes.size()); ++i) {
    if(nodes[i].onIntersection) {
      OUT(i+1, ' ');
    }
  }
  OUT('\n');
}

int main() {
  ReadInput();
  ComputeNeighbors();
  try {
    Solve();
    if (!cycleBottom) {
      OUT("NIE\n");
    } else {
      int res = MarkIntersection();
      OUT(res, '\n');
      PrintIntersection();
    }
  } catch (EmptyIntersection) {
    OUT("0\n\n");
  }
}