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

struct node_t {
	long long f_v;
	long long v;
	long long e_v;
	long long end;
	long long begin;
	long long ok;
};

using gg_t = std::vector<node_t>;
using g_t = std::vector<std::vector<long long>>;

long long cycle_count = 0;
long long begin_count = 0;
bool is_end = false;
long long first_id = -1;

long long find_cycle(g_t& g, gg_t& gg, long long n) {
	if (gg[n].v == 2)
		return 0;
	if (gg[n].v == 1) {
		++gg[n].end;
		if (is_end)
			cycle_count = -1;
		return 1;
	}

	gg[n].v = 1;
	for (auto s: g[n]) {
		if (cycle_count == -1)
			return 0;
		if (begin_count == cycle_count)
			break;
		if (find_cycle(g, gg, s) == 1)
			++gg[n].begin;
	}
	begin_count += gg[n].begin;
	gg[n].ok = begin_count == cycle_count && !is_end;
	if (gg[n].ok)
		first_id = n;
	if (gg[n].end > 0) 
		is_end = true;
	// if (is_end && begin_count < cycle_count)
	// 	cycle_count = -1;
	gg[n].v = 2;
	return 0;
}

void all_cycle(g_t& g, gg_t& gg, long long n) {
	if (gg[n].f_v == 2)
		return;
	if (gg[n].f_v == 1) {
		++cycle_count;
		return;
	}

	gg[n].f_v = 1;
	for (auto s: g[n]) {
		all_cycle(g, gg, s);
	}
	gg[n].f_v = 2;
}

long long bad_count = 0;
void find_bad(g_t& g, gg_t& gg, long long n) {
	if (gg[n].e_v > 0)
		return;
	gg[n].e_v = 1;
	if (gg[n].ok == 1) {
		++bad_count;
		gg[n].ok = 2;
		// printf("znaleziony %lld\n", n);
		return;
	}
	for (auto s: g[n]) {
		find_bad(g, gg, s);
	}
} 
void remove_bad(g_t& g, gg_t& gg, long long n) {
	// printf("remove: %lld\n", n);
	gg[n].e_v = 2;
	bad_count = 0;
	for (auto s: g[n]) {
		if (gg[s].ok == 0) {
			// printf("wszedlem\n");
			find_bad(g, gg, s);
		}
	}
	// bool found = false;
	if (bad_count > 0) {
		// printf("bad_count\n");
		while (bad_count > 0) {
			bool found = false;
			for (auto s: g[n]) {
				if (gg[s].e_v != 2 && gg[s].ok > 0) {
					found = true;
					n = s;
					gg[n].e_v = 2;
					if (gg[n].ok == 2)
						--bad_count;
					gg[n].ok = 2;
					break;
				}
			}
			// printf("lista %lld\n", n);
			if (!found)
				return;
		}
		gg[n].ok = 1;
	}
	else {
		bool found = false;
		for (auto s: g[n]) {
			if (gg[s].e_v != 2 && gg[s].ok > 0) {
				found = true;
				n = s;
				gg[n].e_v = 2;
				break;
			}
		}
		if (!found)
			return;
	}
	remove_bad(g, gg, n);
}


int main() {
	long long n, m;
	scanf("%lld%lld", &n, &m);
	++n;

	gg_t gg(n);
	for (long long i = 1; i < n; ++i) {
		gg[i] = {0, 0, 0, 0, 0, 0};
	}

	g_t g(n);
	for (long long i = 0; i < m; ++i) {
		long long v1, v2;
		scanf("%lld%lld", &v1, &v2);
		g[v1].push_back(v2);
	}

	for (long long i = 1; i < n; ++i) {
		all_cycle(g, gg, i);
	}
	if (cycle_count == 0) {
		printf("NIE\n");
		return 0;
	}
	for (long long i = 1; i < n; ++i) {
		find_cycle(g, gg, i);
	}

	if (cycle_count == -1) {
		printf("0\n\n");
		return 0;
	}

	// printf("%lld\n", cycle_count);

	remove_bad(g, gg, first_id);

	long long count = 0;
	for (long long i = 1; i < n; ++i) {
		if (gg[i].ok == 1)
			++count;
	}
	printf("%lld\n", count);
	for (long long i = 1; i < n; ++i) {
		if (gg[i].ok == 1)
			printf("%lld ", i);
	}
	// printf("\n");
	return 0;
}