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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

//na podstawie "Algorytmika Praktyczna" Piotra Stanczyka

typedef vector<int> VI;
typedef long long LL;

#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#include <list>

#define DBG(X)


template<class V, class E> struct Graph {
	struct Ed : E {
		int v; 
		Ed(E p, int w) : E(p), v(w) {}
	};
	struct Ve : V,vector<Ed> {};
	vector<Ve> g;
	Graph(int n=0) : g(n) {}
	void EdgeD(int b, int e, E d = E()) {g[b].PB(Ed(d,e));}
void Write() {
	REP(x, SIZE(g)) {
		cout << x << ":";
		FOREACH(it, g[x]) cout << " " << it->v;
		cout << endl;
	}
}
  VI vis;
  Graph<V,E> *sccRes;
  void SccDfs(int v,int nr, bool phase){
    g[v].t = 1;
	if(!phase) vis[v] = nr;
    FOREACH(it, g[v]) if (g[it->v].t == -1)
		SccDfs(it->v, nr, phase); else 
		if(!phase && nr > vis[it->v])
		sccRes->EdgeD(g[it->v].t, vis[it->v]=nr);
	if(phase) vis.PB(v); else g[v].t = nr;
  }
  Graph<V,E> Scc() {
    Graph<V,E> gt(SIZE(g)),res(SIZE(g)),*tab[]={this, & gt};
    gt.sccRes=&res; gt.vis.resize(SIZE(g), -1); vis.clear();
    REP(i,SIZE(g)) FOREACH(it,g[i]) gt.EdgeD(it->v,i);
    REP(i,2){
      FOREACH(it,tab[i]->g) it->t = -1;
      int comp=0,v;
      FORD(j,SIZE(g)-1,0)
        if (tab[i]->g[v=(i?vis[j]:j)].t == -1) tab[i]->SccDfs(v,comp++,1-i);
      if (i) res.g.resize(comp);
    }
    REP(i,SIZE(g)) g[i].t=gt.g[i].t;
    return res;
  }
};
struct Ve {}; 
struct Vs {
	int t;
};


vector<int> skladowe[1<<20];



int skladowa[1<<20];

int skladowe2[1<<20];

vector<pair<int,int>> Edges;

vector<int> Edge[1<<20];
vector<int> EdgeR[1<<20];

int main() {
	int n, m, s, b, e;
	Ve ed;
	scanf("%d%d", &n, &m);
	Graph<Vs, Ve> g(n);
	
	REP(x, m) {
		scanf("%d%d", &b, &e);
		b--; e--;
		g.EdgeD(b, e);
		Edge[b].push_back(e);
		EdgeR[e].push_back(b);
		Edges.push_back({b, e});
	}
	
	Graph<Vs, Ve> scc = g.Scc();
//	g.Write();
	int ileSkl = 0;
	REP(x,n) 
  {
    skladowe[g.g[x].t].push_back(x);
    ileSkl = max((unsigned)g.g[x].t, (unsigned)ileSkl);
  }
  ileSkl++;
  DBG(cout << "ileSkl " << ileSkl << endl;  )
  
  int ileSkl2 = 0;
  int ileSkl1 = 0;
  
  int rozmiarSkl = 0;
  
  for (int i = 0; i < ileSkl; i++)
  {
    if (skladowe[i].size() > 1)
    for (int j = 0; j < skladowe[i].size(); j++)
    {
      skladowa[skladowe[i][j]] = 1;
      DBG(cout << skladowe[i][j] << " ";)
    }
    
    DBG(cout << endl;)
  }
  
  int ktoraSkl = -1;
  for (int i = 0; i < ileSkl; i++)
  {
    ileSkl2 += skladowe[i].size() > 1 ? 1 : 0;
    ileSkl1 += skladowe[i].size() == 1 ? 1 : 0;
    if (rozmiarSkl < skladowe[i].size())
    {
      rozmiarSkl = skladowe[i].size();
      ktoraSkl = i;
    }
    
  }
  
  DBG(cout << "ktoraSkl " << ktoraSkl << endl;
  cout << "ileSkl " << ileSkl << endl;
  cout << "ileSkl1 " << ileSkl1 << endl;
  cout << "ileSkl2 " << ileSkl2 << endl;
  cout << "rozmiarSkl " << rozmiarSkl << endl;)
  
  if (ileSkl2 == 0)
  {
    printf("NIE\n");
    return 0;
  }

  if (ileSkl2 > 1)
  {
    printf("0\n");
    return 0;
  }
  
  //cykl prosty
  /*if (n - (ileSkl - 1) == rozmiarSkl)
  {
    cout << "cykl prosty" << endl;
    printf("%d\n", rozmiarSkl);
    sort(skladowe[ktoraSkl].begin(), skladowe[ktoraSkl].end());
    for (int i = 0 ; i < skladowe[ktoraSkl].size(); i++)
    {
      printf("%d ", skladowe[ktoraSkl][i]);
    }
    return 0;
  }*/
  
  vector<int> result;
  
  for (int i = 0; i < skladowe[ktoraSkl].size(); i++)
  {
    //for (int j = 0; j < skladowe[i].size(); j++)
    //cout << skladowe[i][j] << " ";
    //cout << endl;
    int m, s, b, e;
    Ve ed;
    //n = rozmiarSkl;
    
    DBG(cout << " rozmiarSkl" << rozmiarSkl << endl;
    cout << "skladowe[ktoraSkl].size() " << skladowe[ktoraSkl].size() << endl;)
    
    int skipv = skladowe[ktoraSkl][i];
    
    DBG(cout << " skipv " << skipv << endl;)
    
    Graph<Vs, Ve> g(n);
    
    for (int j = 0; j < Edges.size(); j++)
    {
      b = Edges[j].first;
      e = Edges[j].second;
      if (skladowa[b] && skladowa[e] && b != skipv && e != skipv)
      {
        DBG(cout << " dodane " << b << " " << e << endl;)
        g.EdgeD(b, e);
      }
    }
//    return 0;
    Graph<Vs, Ve> scc = g.Scc();
    

  	int ileSkl = 0;
  	
  	REP(x,n) 
    {
      skladowe2[g.g[x].t]++;
      ileSkl = max((unsigned)g.g[x].t, (unsigned)ileSkl);
    }
    
    ileSkl++;
    
    DBG(cout << "ileSkl " << ileSkl << endl;)
    
    int ileSkl2 = 0;
    int ileSkl1 = 0;
    
    int rozmiarSkl = 0;
    
    for (int i = 0; i < ileSkl; i++)
    {
      ileSkl2 += skladowe2[i] > 1 ? 1 : 0;
      skladowe2[i] = 0;
    }
    
    if (ileSkl2 == 0)
      result.push_back(skipv);
    
    
  }
  printf("%d\n", result.size());
  sort(result.begin(), result.end());
  
  for (int i = 0 ; i < result.size(); i++)
  {
    printf("%d ", result[i] + 1);
  }
  
	return 0;
}