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
#include <cstdio>
#include <deque>
#include <list>
#include <stack>
using namespace std;

#undef DEBUG

#define EVEN(X) (X % 2 == 0)
#define ODD(X) (X % 2 != 0)

#define FOR(E, I) for(auto E = I.begin(); E != I.end(); E++)

const int MAX_N = 500000;
const int MAX_M = 1000000;

typedef struct Node
{
#ifdef DEBUG
	int indexForDisplay;
#endif
	bool visited;
	bool visitedInCurrentPath;
	int cycleCount;
	int cycleStart, cycleEnd;
	list<Node*> connections;
} Node;

Node nodes[MAX_N];


int dfsAndMarkCycles(Node* startingNode)
{
	int totalCycleCount = 0, currentCycleCount = 0;
	stack<Node*> q;
	deque<Node*> backtrack;

	q.push(startingNode);

#ifdef DEBUG
	printf("DFS starts from node %d\n", startingNode->indexForDisplay);
#endif

	while (!q.empty())
	{
		Node *n = q.top();
#ifdef DEBUG
		printf("Node %d, backtrack ", n->indexForDisplay);
		for (auto bIter = backtrack.rbegin(); bIter != backtrack.rend(); bIter++)
		{
			printf("%d ", (*bIter)->indexForDisplay);
		}
		printf("\n");
#endif

		if (n->visitedInCurrentPath)
		{
#ifdef DEBUG
			printf("already visited node %d\n", n->indexForDisplay);
#endif
			n->visitedInCurrentPath = false;

			Node *b = backtrack.back();
			currentCycleCount += b->cycleStart;
			b->cycleCount = currentCycleCount;
			currentCycleCount -= b->cycleEnd;

			backtrack.pop_back();
			q.pop();
		}
		else
		{
			n->visited = true;
			n->visitedInCurrentPath = true;
			backtrack.push_back(n);

			FOR(cIter, n->connections)
			{
				if ((*cIter)->visitedInCurrentPath) 
				{
#ifdef DEBUG
					printf("cycle detected at node %d, previous node %d\n", (*cIter)->indexForDisplay, backtrack.back()->indexForDisplay);
#endif
					n->cycleStart++;
					(*cIter)->cycleEnd++;
					totalCycleCount++;
				}
				else
				{
					q.push(*cIter);
#ifdef DEBUG
					printf("queued %d -> %d\n", n->indexForDisplay, (*cIter)->indexForDisplay);
#endif
				}
			}
		}
	}

#ifdef DEBUG
	printf("cycle count for subgraph starting from node %d is %d\n", startingNode->indexForDisplay, totalCycleCount);
#endif

	return totalCycleCount;
}


list<int> solution(int n)
{
	int cycleCount = 0;

	for (int i = 0; i < n; i++)
	{
		if (!nodes[i].visited)
		{
			cycleCount += dfsAndMarkCycles(&nodes[i]);
		}
	}

#ifdef DEBUG
	printf("total cycle count %d\n", cycleCount);
#endif

	list<int> result;
	if (cycleCount > 0)
	{
		for (int i = 0; i < n; i++)
		{
			if (nodes[i].cycleCount == cycleCount)
			{
				result.push_back(i + 1);
			}
		}
	}

#ifdef DEBUG
	printf("critical intersections %d\n", result.size());
#endif

	return result;
}


int main()
{
	int n, m;
	scanf("%d%d", &n, &m);

#ifdef DEBUG
	for (int i = 0; i < n; i++) nodes[i].indexForDisplay = i + 1;
#endif
	for (int i = 0; i < m; i++)
	{
		int a, b;
		scanf("%d%d", &a, &b);

		nodes[a - 1].connections.push_back(nodes + b - 1);
	}

	list<int> result = solution(n);
	result.sort();

	if (!result.empty())
	{
		printf("%d\n", result.size());
		FOR(rIter, result) printf("%d ", *rIter);
		printf("\n");
	}
	else
	{
		puts("NIE");
	}

	return 0;
}