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

using namespace std;

const int MAXN = 500009;
vector<int> AL[MAXN], ALT[MAXN];
vector<int> G[MAXN], GT[MAXN];
bool visited[MAXN];
int components[MAXN];
int componentSize[MAXN];
int components1[MAXN];
int componentSize1[MAXN];
int deleted;
int componentNr = 1;
stack<int> dfsPostOrderNodes;

void componentDfs(int start, vector<int> *AL, int component, int *components, int *componentSize) {
	components[start] = component;
	++componentSize[component];

	for (size_t i = 0; i < AL[start].size(); i++) {
		int w = AL[start][i];
		if (!components[w] && w != deleted)
			componentDfs(w, AL, component, components, componentSize);
	}
}

void SccDfs(int start, vector<int> *AL) {
	visited[start] = true;
	for (size_t i = 0; i < AL[start].size(); i++) {
		int w = AL[start][i];
		if (!visited[w] && w != deleted)
			SccDfs(w, AL);
	}
	dfsPostOrderNodes.push(start);
}

void SCC(int start, vector<int> *AL, vector<int> *ALT, int *components, int *componentSize) {
	SccDfs(start, AL);
	while (!dfsPostOrderNodes.empty()) {
		int v = dfsPostOrderNodes.top();
		dfsPostOrderNodes.pop();
		if (!components[v]) {
			componentDfs(v, ALT, componentNr, components, componentSize);
			++componentNr;
		}
	}
}

bool notConnected(vector<int> *AL, vector<int> *ALT, vector<int> &nodes, int *components, int *componentSize) {
	for (auto i : nodes)
		if (i != deleted && !visited[i])
			SccDfs(i, AL);

	componentNr = 1;
	while (!dfsPostOrderNodes.empty()) {
		int v = dfsPostOrderNodes.top();
		dfsPostOrderNodes.pop();
		if (!components[v]) {
			componentDfs(v, ALT, componentNr, components, componentSize);
			++componentNr;
		}
	}

	for (auto i : nodes)
		if (i != deleted) {
			if (componentSize[components[i]] > 1)
				return false;
		}

	return true;
}

int main(int argc, char* argv[]) {
	int n, m, a, b;

	scanf("%d%d", &n, &m);
	while (m--) {
		scanf("%d%d", &a, &b);
		AL[a].push_back(b);
		ALT[b].push_back(a);
	}

	for (int i = 1; i <= n; i++)
		if (!components[i])
			SCC(i, AL, ALT, components, componentSize);
	
	int component;
	int componentsNum = 0;
	for (int i = 1; i <= n; ++i) {
		if (componentSize[i] > 1) {
			component = i;
			++componentsNum;
		}
	}

	if (componentsNum == 0) {
		printf("NIE\n");
	}
	else if (componentsNum > 1) {
		printf("0\n");
	}
	else {
		vector<int> res, nodes;

		for (int i = 1; i <= n; ++i)
			if (components[i] == component) {
				nodes.push_back(i);
				for (int j = 0; j < AL[i].size(); ++j) {
					if (components[AL[i][j]] == component) {
						G[i].push_back(AL[i][j]);
						GT[AL[i][j]].push_back(i);
					}
				}
			}

		for (auto i : nodes)
			if (components[i] == component) {
				deleted = i;
				fill(begin(visited), visited + n + 1, false);
				fill(begin(components1), components1 + n + 1, 0);
				fill(begin(componentSize1), componentSize1 + n + 1, 0);
				if (notConnected(G, GT, nodes, components1, componentSize1))
					res.push_back(deleted);
			}

		printf("%d\n", (int)res.size());
		if (res.size() > 0) {
			for (size_t i = 0; i < res.size(); i++)
				printf("%d ", res[i]);
			printf("\n");
		}
	}

	return 0;
}