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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
#include <cassert>
#include <bitset>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

list<int> adj[600000];
PII ti[600000];
vector<PII> back_edges;

int _time = 1;
void dfs(int v) {
  if (ti[v].st) return;
  ti[v].st = _time++;
  FOREACH(it, adj[v]) {
    dfs(*it);
    if (!ti[*it].nd) back_edges.pb(mp(v, *it));
  }
  ti[v].nd = _time++;
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  REP(i,M) {
    int a,b;
    scanf("%d%d", &a, &b);
    adj[--a].pb(--b);
  }
  REP(i,N) if (!ti[i].st) dfs(i);
  if (back_edges.size() == 0) {
    printf("NIE\n");
    return 0;
  }

  int max_high_in = 0, min_high_out = _time;
  int min_low_in = _time, max_low_out = 0;

  FOREACH(it, back_edges) {
    max_high_in = max(max_high_in, ti[it->nd].st);
    min_high_out = min(min_high_out, ti[it->nd].nd);
    min_low_in = min(min_low_in, ti[it->st].st);
    max_low_out = max(max_low_out, ti[it->st].nd);
  } 
  vector<int> result;
  REP(i,N) if (
    ti[i].st >= max_high_in
    && ti[i].nd <= min_high_out
    && ti[i].st <= min_low_in
    && ti[i].nd >= max_low_out
  ) result.pb(i);
  printf("%d\n", result.size());
  FOREACH(it, result)printf("%d ", *it + 1);
  printf("\n"); 
}