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

using namespace std;

struct CT
{
	CT(int _s) : 
		s(2<<_s), c(new int[s]), x(new int[s])
	{
		Clear();
	}
	
	~CT()
	{
		delete [] c;
		delete [] x;
	}
	
	void Clear()
	{
		for(int i = 0; i < s; ++i)
			c[i] = x[i] = 0;
	}
	
	void Add(int _b,int _e)
	{
		b = _b;
		e = _e;
		Set(0, (s>>1)-1, 1);
	}
	
	void Set(int wb, int we, int v)
	{
		if(wb > e || we < b)
			return;
		if(b <= wb && we <= e)
		{
			x[v] += 1;
		}
		else
		{
			Set(wb,(wb+we)>>1, v<<1);
			Set(((wb+we+1)>>1), we, (v<<1) + 1);
		}
		c[v] = (x[v] > 0 ? we-wb+1 : (we == wb ? 0 : c[v<<1] + c[(v<<1)+1]));
	}
	
	bool IsCovered(int p)
	{
		return Cover(p, p);
	}
	
	int Cover(int _b, int _e)
	{
		b = _b;
		e = _e;
		return f(0, (s>>1)-1, 1);
	}
	
	int f(int wb, int we, int v)
	{
		if(wb > e || we < b)
			return 0;
		if(x[v] > 0)
			return min(e,we) - max(b,wb) + 1;
		if(b <= wb && we <= e)
			return min(we - wb + 1,c[v]);
		return f(wb,(wb+we)>>1,v<<1) + f(((wb+we+1)>>1),we,(v<<1) + 1);
	}
	
	int s;
	int *c,*x;
	int b,e;
} ExcludeIn(19), ExcludeOut(19);

#define MAXN 500001
int n, m;
vector<int> E[MAXN], RevE[MAXN];
int t[MAXN], s[MAXN];
int SccT = -1;
int SccCount[MAXN];
int InterestingScc = -1;
int in[MAXN], out[MAXN];

void Read();
void SccDfs1();
void SccDfs2();
bool ValidateSccs();
void ExcludingDfs(int root);
void CalcAndPrintResult();
int GetRoot();

int main()
{
	Read();
	SccDfs1();
	SccDfs2();
	if(ValidateSccs())
	{
		ExcludingDfs(GetRoot());
		CalcAndPrintResult();
	}
	return 0;
}

void Read()
{
	scanf("%d%d", &n, &m);
	for(int i = 0; i < m; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		E[a].push_back(b);
		RevE[b].push_back(a);
	}
}

void SccDfs1()
{
	for(int i = 1; i <= n; ++i)
		t[i] = -1;
	vector<int> Q;
	int T = -1;
	for(int i = 1; i <= n; ++i)
	{
		if(t[i] != -1)
			continue;
		t[i] = E[i].size();
		Q.push_back(i);
		while(!Q.empty())
		{
			int v = Q.back();
			while(--t[v] >= 0 && t[E[v][t[v]]] != -1)
			{}
			if(t[v] != -1)
			{
				int u = E[v][t[v]];
				Q.push_back(u);
				t[u] = E[u].size();
			}
			else
			{
				Q.pop_back();
				s[t[v] = ++T] = v;
			}
		}
	}
}

void SccDfs2()
{
	for(int i = 1; i <= n; ++i)
		t[i] = -1;
	std::vector<int> Q;
	for(int i = 1; i <= n; ++i)
	{
		int v0 = s[n-i];
		if(t[v0] != -1)
			continue;
		++SccT;
		t[v0] = RevE[v0].size();
		Q.push_back(v0);
		while(!Q.empty())
		{
			int v = Q.back();
			while(--t[v] >= 0 && t[RevE[v][t[v]]] != -1)
			{}
			if(t[v] != -1)
			{
				int u = RevE[v][t[v]];
				Q.push_back(u);
				t[u] = RevE[u].size();
			}
			else
			{
				Q.pop_back();
				t[v] = SccT;
				++SccCount[SccT];
			}
		}
	}
}

bool ValidateSccs()
{
	int complexCount = 0;
	for(int i = 0; i <= SccT; ++i)
		if(SccCount[i] > 1)
			++complexCount, InterestingScc = i;
	if(complexCount == 1)
		return true;
	if(complexCount == 0)
		puts("NIE");
	else
		puts("0");
	return false;
}

void ExcludingDfs(int root)
{
	for(int i = 1; i <= n; ++i)
		s[i] = -1;
	int inT = 0, outT = 0;
	vector<int> Q;
	vector<pair<int, int>> crossEdges;
	Q.push_back(root);
	s[root] = E[root].size();
	in[root] = ++inT;
	while(!Q.empty())
	{
		int v = Q.back();
		int nextAdjI = --s[v];
		if(nextAdjI < 0)
		{
			static bool shouldCheckRoot = true;
			if(shouldCheckRoot && outT == 0)
				for(int adj : E[v])
					if(t[adj] == InterestingScc)
					{
						shouldCheckRoot = false;
						if(adj != root)
						{
							for(int i = 1; i <= n; ++i)
								in[i] = out[i] = 0;
							ExcludeIn.Clear();
							ExcludeOut.Clear();
							ExcludingDfs(adj);
							return;
						}
						break;	
					}
			
			Q.pop_back();
			out[v] = ++outT;
			continue;
		}
		
		int u = E[v][nextAdjI];
		if(t[u] != InterestingScc)
			continue;
		
		if(in[u] == 0)
		{
			Q.push_back(u);
			s[u] = E[u].size();
			in[u] = ++inT;
		}
		else if(out[u] == 0)
		{
			ExcludeIn.Add(0, in[u]-1);
			ExcludeIn.Add(in[v]+1, n+1);
			ExcludeOut.Add(0, outT);
		}
		else
		{
			crossEdges.push_back({v, u});
		}
	}
	for(auto e : crossEdges)
		ExcludeOut.Add(out[e.second]+1, out[e.first]-1);
}

int GetRoot()
{
	for(int i = 1; i <= n; ++i)
	{
		if(t[i] == InterestingScc)
			return i;
	}
	return 0;
}

void CalcAndPrintResult()
{
	vector<int> result;
	for(int i = 1; i <= n; ++i)
		if(t[i] == InterestingScc
			&& !ExcludeIn.IsCovered(in[i])
			&& !ExcludeOut.IsCovered(out[i]))
		{
			result.push_back(i);
		}
	sort(result.begin(), result.end());
	printf("%lu\n", result.size());
	for(auto i : result)
		printf("%d ", i);
}