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
// Potyczki Algorytmiczne 2015
// runda 5
// KONtrmanifestacja
// Tomasz Pastusiak

#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
#include <set>
#include <cmath>
#include <vector>
#include <string>

using namespace std;

// cities are numbered 1 ... n
#define MAXN 500001
//#define MAXM 1000000

#define LL long long int

int n,m;

vector<int> g[MAXN];
vector<int> gt[MAXN];
int p[MAXN]; // parent
int scc_id[MAXN]; //strongly connected component ID for city
int scc_id_t[MAXN];
int sccs_size[MAXN];
int processedOrder[MAXN];

int cycleLen;
int cycleVerts[MAXN];
int t[MAXN]; // used in topo sort

bool mandatory[MAXN];

// SCC start (Stańczyk)
int nr;

void SccSDfs(vector<int> *graph, int *t, int v){
	if(t[v] == -1){
		t[v] = nr;
		for(int i=0; i<graph[v].size() ;++i){
			SccSDfs(graph, t, graph[v][i]);
		} // for each child
		if(nr < 0) t[v] = -(--nr) - 3;
	} // if not visited yet
}

void SccS(){
	for(int i=1; i <= n ;++i){
		scc_id[i] = scc_id_t[i] = -1;
		for(int v=0; v<g[i].size(); ++v) gt[v].push_back(i);
	}
	nr = -2;

	for(int i=1; i <= n; ++i){
		SccSDfs(gt, scc_id_t, i);
		processedOrder[scc_id_t[i]] = i;
	}
	
	nr = 0;
	for(int i=n-1; i>=0; --i){
		SccSDfs(g, scc_id, processedOrder[i]);
		nr++;
	}
}
// SCC end

int cycleSCC;
int cycleStart;

void getCycleR(int v){
	for(int i=0; i<g[v].size() ;++i){
		int child = g[v][i];
		if(p[child] == -1){
			p[child] = v;
			if(child == cycleStart){
				return;
			}
			if(scc_id[child] == cycleSCC){
				getCycleR(child);
			}
		} // if child not visited yet
	} // for each child
}

void getCycle(int v){
	cycleStart = v;
	cycleSCC = scc_id[v];
	
	for(int i=1; i<= n; ++i) p[i] = -1;
	
	getCycleR(cycleStart);
	
	cycleVerts[0] = cycleStart;
	cycleLen = 1;
	int currentVert = p[cycleStart];
	while(currentVert != cycleStart){
		cycleVerts[cycleLen++] = currentVert;
		currentVert = p[currentVert];
	}
}

// TOPO begin
int topo;
int topoSCC;

void TopoDfs(int v){
	if(!t[v] && scc_id[v] == topoSCC){
		t[v] = 1;
		
		for(int i=0; i<g[v].size() ;++i){
			TopoDfs(g[v][i]);
		}
		
		t[v] = --topo;
	}
}

void TopoSort(int scc){
	topoSCC = scc;
	
	for(int i=1; i<=n ;++i){
		t[i] = 0;
	}
	topo = n;
		
	for(int i=n; i>=1; --i){
		TopoDfs(i);
	}
}

bool AcyclicD(int scc){
	TopoSort(scc);
	
	for(int i=1; i <= n; ++i){
		for(int v=0; v<g[i].size() ;++v){
			if(scc_id[i] != scc || scc_id[g[i][v]] != scc) continue;
			if(t[i] >= t[g[i][v]]) return false;
		}
	}
	
	return true;
}
// TOPO end

int main(){
	ios_base::sync_with_stdio(false);
	
	int a,b;
	cin >> n >> m;
	
	for(int i=0; i<m ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
	}
	
	// DATA LOADED
	
	SccS();
	
	/*											for(int i=1; i <= n ; ++i){
													cout << "City: " << i << " scc ID: " << scc_id[i] << endl;
												}
												
												cout << "--------" << endl;
	*/
	
	for(int i=0; i<n+1 ; ++i){
		sccs_size[i] = 0;
		mandatory[i] = false;
	}
	
	for(int i=1; i<=n ; ++i){
		sccs_size[scc_id[i]]++;
	}
	
	int numberOfProperSccs = 0; // "proper" Scc contains at least 2 cities
	int sccNoticed = -1;
	
		
	for(int i=0; i<n+1 ; ++i){
		if(sccs_size[i] >= 2){
			numberOfProperSccs++;
			sccNoticed = i;
		}
	}
	
	if(numberOfProperSccs == 0){
		cout << "NIE" << endl; // no sccs mean no cycles at all
		return 0;
	}
	else if(numberOfProperSccs > 1){
		cout << 0 << endl << endl; // if there are at least 2 sccs, there are at least 2 independent cycles
		return 0;
	}
	
	// if there is only one scc, we don't know...
	// lets do some brute force
	// find a cycle in scc (the smaller the better)
	// for every city in this cycle.... remove it, check if there are any cycles in scc without this city, if there are: city is NOT mandatory, if there is no cycle, city is MANDATORY
	
	// another (heuristic?)
	// until there are not marked cities:
	// start at some not marked city, do DFS search for cycle but favorizing edges to not-marked cities
	// when cycle found, mark every it's city..
	// it is almost 100% wrong algo....
	
	
	int cycleStartingPoint;
	
	for(int i=1; i<=n ; ++i){
		if(scc_id[i] == sccNoticed) 
			cycleStartingPoint = i;
	}
	 
	getCycle(cycleStartingPoint);
	 
	/*												cout << "Suspicious cycle: " << endl;
													for(int i=0; i<cycleLen; ++i){
														cout << cycleVerts[i] << ' ';
													}
													 
													 cout << endl;
													 
													 cout << "--------" << endl;
	 */
	 
	 int mandatoryVertsCount = 0;
	 
	for(int i=0; i < cycleLen; ++i){
		scc_id[cycleVerts[i]] = -1; // remove it from scc temporarily
		if(AcyclicD(sccNoticed)) {
			mandatory[cycleVerts[i]] = true;  // if removing vert made graph acyclic, this vert is mandatory
			mandatoryVertsCount++;
		}
		scc_id[cycleVerts[i]] = sccNoticed; // put it back
	}
	 
	cout << mandatoryVertsCount << endl;
	
	for(int i=1; i<=n; ++i){
		if(mandatory[i])
			cout << i << ' ';
	}
	
	cout << endl;
	 
	return 0;
}