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
#include <cstdio>
#include <vector>
using namespace std;

const int MAX = 500010;

namespace DSU {
  int S[MAX];
  void init(int n) { for (int i=0;i<n;i++) S[i] = i; }
  int findset(int x) { return x == S[x] ? x : S[x] = findset(S[x]); }
  void link(int a, int b) {
    S[findset(a)] = findset(b);
  }
};

vector<int> G[MAX];
int vis[MAX];
int P[MAX];
int S[MAX];
int brige_cnt;
vector<int> post, res;
int n, m;

void dfs1(int u, int p) {
  P[u] = p;
  vis[u] = 1;
  for (int v : G[u])
    if (!vis[v]) {
      dfs1(v, u);
      DSU::link(v, u);
    } else if (vis[v] == 1) {
      brige_cnt++;
      S[u]++;
      S[P[v]]--;
    } else {
      int lca = DSU::findset(v);
      S[P[v]]--;
      S[lca]++;
    }
  vis[u] = 2;
  post.push_back(u);
}

int main() {
  scanf("%d %d", &n, &m);
  DSU::init(n);
  while(m--)
  {
    int a, b;
    scanf("%d %d", &a, &b);
    a--, b--;
    G[a].push_back(b);
  }

  for (int i=0;i<n;i++)
    if (!vis[i])
      dfs1(i, n);

  if (!brige_cnt) {
    printf("NIE\n");
    return 0;
  }

  for(int i : post)
    S[P[i]] += S[i];
  for (int i=0;i<n;i++)
    if (S[i] == brige_cnt)
      res.push_back(i);
  printf("%d\n", (int)res.size());
  for (int i : res) printf("%d ", i+1); printf("\n");

  return 0;
}