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
291
292
293
294
295
#include <cassert>
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
using namespace std;

struct Node {
	typedef bool has_new_line;
	int id;
	int label = 0;
	vector< int > neighbors;
	Node() {};
};
typedef vector< Node > graph_t;


ostream& operator<< (ostream&, Node const&);
template< typename T > ostream& operator<< (ostream&, vector< T > const&);

graph_t reverse_graph(graph_t const& G)
{
	graph_t R( G.size() );

	for( int i = 0; i < G.size(); ++i ) {
		R[i].id = G[i].id;
		R[i].label = 0;
		for( int n = 0; n < G[i].neighbors.size(); ++n ) {
			int dst = G[i].neighbors[n];
			R[dst].neighbors.push_back( i );
		}	
	}

	return R;
}

void dfs( vector<int>& history, graph_t& G, vector< int >& stack, int& count )
{
	while( stack.size() ) {
		// ostatni ze stosu, jeszcze nie sciagam
		int n_idx = stack.back();

		// labeluje, jesli jeszcze nie zalabelowany
		Node& n = G[n_idx];
		if( n.label == 0 ) {
			n.label = count;
			count += 1;
		}

		// sprawdzam, czy mam sasiada z label == 0
		int next = 0;
		while( (next == 0) && (n.neighbors.size() > 0) ) {
			next = n.neighbors.back(); n.neighbors.pop_back();
			if( G[next].label != 0 ) {
				next = 0;
			}
		}	

		// jesli mam nieodwiedzonego sasiada, to wkladam go na stos
		// jesli nie, to zapamietuje slepy zaulek w kolejce
		if( next > 0 ) {
			stack.push_back( next );
		}
		else {
			assert( stack.back() == n_idx );
			stack.pop_back();
			history.push_back( n_idx );
		}
	}
}

//* zwraca wierzcholki scc (jesli jest jeden scc)
//  lub pusty vector (jesli liczba scc != 1)
//* argumenty przez kopie, bo niszcze polaczenia
//* kosaraju
vector< int > get_scc_nodes(graph_t G)
{
	graph_t R = reverse_graph( G );
	vector< int > stack;
	int count = 1;

	// niszcze graf R
	vector< int > queue;
	for( int i = 0; i < R.size(); ++i ) {
		if( R[i].label == 0 ) {
			stack.push_back( i );
		}
		dfs( queue, R, stack, count );
		assert( stack.size() == 0 );
	}
	assert( count - 1 == R.size() );
	assert( queue.size() == R.size() );
	count = 1;

	// przechodze graf G w kolejnosci z R
	vector< int > scc;
	int prev_scc_count = 0;
	for( auto it = queue.rbegin(); it < queue.rend(); ++it ) {
		int i = *it;
		if( G[i].label == 0 ) {
			stack.push_back( i );
			dfs( scc, G, stack, count );
			if( scc.size() == prev_scc_count + 1 ) {
				// znalezmy scc jednoelementowy, nie interesuje nas
				scc.pop_back();
			}
			else {
				if( prev_scc_count == 0 ) {
					// scc wieloelementowy
					prev_scc_count = scc.size();
				}
				else {
					// kolejny scc wieloelementowy
					// nie liczymy dalej, bo jest wiecej niz 1 scc
					scc.clear();
					return scc;
				}
			}
		}
	}

	return scc;
}

int positive_filter_graph( graph_t& G, vector< int >& v )
{
	int count = 0;
	sort( v.begin(), v.end() );

	// wyrzuc wierzcholki
	for_each( G.begin(), G.end(), [&](Node& n) {
		if( !binary_search( v.begin(), v.end(), n.id ) ) {
			n.neighbors.clear();
		}
	});

	for( auto&& node : G ) {
		auto et = remove_if( node.neighbors.begin(), node.neighbors.end(), [&](int i) {
			return !binary_search( v.begin(), v.end(), i );
		});
		int zostalo = et - node.neighbors.begin();
		node.neighbors.resize( zostalo );
		count += zostalo;
	}

	return count;
}

int negative_filter_graph( graph_t& G, int i )
{
	vector< int > v;
	for_each( G.begin(), G.end(), [&](Node const& n){
		if( n.id != i ) {
			v.push_back( n.id );
		}
	});
	return positive_filter_graph( G, v );
}

bool has_cycles( graph_t const& G )
{
	// Odwracam graf i bede pruc
	graph_t R = reverse_graph( G );

	for_each( R.begin(), R.end(), [&](Node const& n){
		for_each( n.neighbors.begin(), n.neighbors.end(), [&](int i) {
			R[i].label += 1;
		});
	});


	vector< int > queue;
	for( int i = 0; i < R.size(); ++i ) {
		if( R[i].label == 0 ) queue.push_back( i );
	}

	while( queue.size() ) {
		int q = queue.back(); queue.pop_back();
		Node& rr = R[q];
		for( int i = 0; i < rr.neighbors.size(); ++i ) {
			int dst = rr.neighbors[i];
			R[dst].label -= 1;
			assert( R[dst].label >= 0 );
			if( R[dst].label == 0 ) {
				queue.push_back( dst );
			}
		}
	}

	auto et = find_if( R.begin(), R.end(), [](Node const& n) {
		return n.label > 0;
	});

	bool found_loop = ( et != R.end() );
	return found_loop;
}

vector< int > run_baby_run( graph_t G, int start, int len )
{
	int curr = start;

	std::mt19937 generator( 0x11416906 );	
	for( int i = 0; i < len; ++i ) {
		Node& node = G[curr];
		node.label += 1;

		int s = node.neighbors.size();
		assert( s > 0 );

		int idx = generator() % s;
		curr = node.neighbors[idx];
	}

	auto it = max_element( G.begin(), G.end(), [](Node const& a, Node const& b){
		return a.label < b.label;
	});
	int winning_score = it->label;

	vector< int > result;
	for_each( G.begin(), G.end(), [&](Node const& n) {
		if( n.label >= (winning_score - 1) ) {
			result.push_back( n.id );
		}
	});

	assert( result.size() > 0 );
	return result;
}

int main()
{
	ios_base::sync_with_stdio( false );
	int l_skrz; cin >> l_skrz; // 2 ..  500ooo
	int l_drog; cin >> l_drog; // 1 .. 1000ooo

	graph_t G(l_skrz+1);
	for( int i = 0; i < G.size(); ++i ) {
		G[i].id = i;
	}

	for( int i = 0; i < l_drog; ++i ) {
		int a; cin >> a;
		int b; cin >> b;
		G[a].neighbors.push_back(b);
	}


	vector< int > scc = get_scc_nodes( G );

	if( scc.size() == 0 ) {
		cout << "NIE\n";
	}
	else {
		int nowa_l_drog = positive_filter_graph( G, scc );
		vector< int > best = run_baby_run(G, scc[0], 14 * nowa_l_drog );

		// wyrzucamy wierzcholek
		negative_filter_graph( G, best[0] );

		if( has_cycles( G ) ) {
			// dalej sa cykle
			cout << "0\n";
		}
		else {
			cout << best.size() << "\n";
			cout << best[0];
			for( int i = 1; i < best.size(); ++i ) {
				cout << " " << best[i];
			}
			cout << "\n";
		}
	}

	return 0;
}


ostream& operator<< (ostream& os, Node const& n)
{
	os << "[id=" << n.id << ",label=" << n.label << " (" << n.neighbors << ")]";

	return os;
}

template< typename T >
ostream& operator<< (ostream& os, vector< T >const& v )
{
	if( v.size() > 0 ) {
		os << v[0];
		for( int i = 1; i < v.size(); ++i ) {
			os << " " << v[i];
		}
	}
	return os;
}