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

#define MAX_N 500003

#define debug(x...) //printf(x)

int n, m;

bool isRemoved[MAX_N];
bool isNotInSolution[MAX_N];
int predecessorCount[MAX_N];
bool isOnStack[MAX_N];
bool wasInDFS[MAX_N];
//std::stack<int> stack;
std::vector<int> successors[MAX_N];

bool processingCycle = false;

void remove(int node)
{
	isRemoved[node] = true;
	for (std::vector<int>::iterator it = successors[node].begin(); it != successors[node].end(); ++it)
	{
		predecessorCount[*it]--;
	}
}

void bringBack(int node)
{
	isRemoved[node] = false;
	for (std::vector<int>::iterator it = successors[node].begin(); it != successors[node].end(); ++it)
	{
		predecessorCount[*it]++;
	}
}

int dfs(int node)
{
	debug("DFS in %d\n", node);
	int ret = 0;
	wasInDFS[node] = true;
	isOnStack[node] = true;
	for (std::vector<int>::iterator it = successors[node].begin(); it != successors[node].end(); ++it)
	{
		if (!isRemoved[*it])
		{
			if (isOnStack[*it])
			{
				//cycle found!
				debug("Found cycle!\n");
				processingCycle = true;
				return *it;
			}
			else
			{
				int recRet = dfs(*it);
				if (recRet != 0)
				{
					if (processingCycle)
					{
						if (node == recRet) processingCycle = false;
					}
					else
					{
						isOnStack[node] = false;
					}
					ret = recRet;
					break;
				}
				else 
				{
					//isOnStack[node] = false;
				}
			}
		}
	}
	if (!processingCycle)
	{
		isOnStack[node] = false;
	}
	return ret;
}

int searchForCycles()
{
	int startWith = 0;
	
	for (int i = 1; i <= n; ++i)
	{
		wasInDFS[i] = false;
	}
	
	for (int i = 1; i <= n; ++i)
	{
		if (!isRemoved[i] && !wasInDFS[i] && predecessorCount[i] == 0)
		{
			startWith = i;
			int cycleStart = dfs(startWith);
			if (cycleStart != 0)
			{
				for (int i = 1; i <= n; ++i)
				{
					if (!isOnStack[i])
					{
						isNotInSolution[i] = true;
					}
					else
					{
						debug("On stack %d\n", i);
						isOnStack[i] = false; //not any more
					}
				}
				return cycleStart;
			}
		}
	}
	
	if (startWith == 0)
	{
		debug("All are in cycle\n");
		return 1;
	}
	else
	{
		debug("No cycle found\n");
		return 0;
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	
	for (int i = 0; i < m; ++i)
	{
		int from, to;
		scanf("%d%d", &from, &to);
		successors[from].push_back(to);
		predecessorCount[to]++;
	}
	

	int initialCycle = searchForCycles();

	
	if (initialCycle == 0)
	{
		for (int i = 1; i <= n; ++i)
		{
			debug("Removing %d from solution\n");
			isNotInSolution[i] = true;
		}
	}
	else
	{
		for (int i = 1; i <= n; ++i)
		{
			if (!isNotInSolution[i])
			{
				debug("Temporary removing %d\n", i);
				remove(i);
				int foundCycle = searchForCycles();
				if (foundCycle != 0)
				{
					debug("Removing %d from solution\n");
					isNotInSolution[i] = true;
				}
				
				debug("Bringing %d back\n", i);
				bringBack(i);
			}
		}
	}
	
	std::vector<int> solution;
	for (int i = 1; i <= n; ++i)
	{
		if (!isNotInSolution[i])
		{
			solution.push_back(i);
		}
	}
	
	if (solution.size() == 0)
	{
		printf("NIE\n\n");
	}
	else
	{
		printf("%d\n", solution.size());
		for (std::vector<int>::iterator it = solution.begin(); it != solution.end(); ++it)
		{
			printf("%d ", *it);
		}
		printf("\n");
	}
	

	return 0;
}