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
//Brute


#include <cstdio>
#include <vector>
#include <algorithm>

// #define DEBUG

using namespace std;

const int MAX_N=500010;
const int INF=1000*1000*1000;

int N, M;
vector<int> edges[MAX_N];

int curComponentId=0;
int componentId[MAX_N]={};
int componentSize[MAX_N]={};

int curIndex;
int index[MAX_N]={};
int lowlink[MAX_N]={};

vector<int> SSCstack;	//stack
bool onSSCstack[MAX_N]={};

int disabledNode=-1;

void strongconnect(int node) {
	// Set the depth index for v to the smallest unused index
	if (node == disabledNode)
		return;
	
	index[node]=curIndex;
	lowlink[node]=curIndex;
	++curIndex;
	
	SSCstack.push_back(node);
	onSSCstack[node]=true;

	// Consider successors of v
	for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) {
		if (*vit == disabledNode)
			continue;
		
		if (index[*vit] == 0) {
			strongconnect(*vit);
			
			lowlink[node] = min(lowlink[node], lowlink[*vit]);
			
		} else if (onSSCstack[*vit]) {
			// Successor w is in stack S and hence in the current SCC
			lowlink[node] = min(lowlink[node], index[*vit]);
		}
	}

	// If v is a root node, pop the stack and generate an SCC
	if (lowlink[node] == index[node]) {
		//start a new strongly connected component:
		++curComponentId;
		
		#ifdef DEBUG
			fprintf(stderr, "SCC = %d  ->  ", curComponentId);
		#endif
		int w;
		do {
			w = SSCstack.back();
			SSCstack.pop_back();
			onSSCstack[w] = false;
			componentId[w] = curComponentId;
			++componentSize[curComponentId];
			#ifdef DEBUG
				fprintf(stderr, "%d, ", w);
			#endif
		} while (w != node); //until (w = v)
		
		#ifdef DEBUG
			fprintf(stderr, "\n");
		#endif
	}
}

void resetSCCdata() {
	curIndex=1;
	
	for (int i=0; i<=curComponentId; ++i) {
		componentSize[i]=0;
	}
	
	curComponentId=0;
	
	for (int i=1; i<=N; ++i) {
		index[i]=0;
		componentId[i] = 0;
	}
}


bool checkNode(int dis) {
	resetSCCdata();
	disabledNode=dis;
	
	#ifdef DEBUG
		fprintf(stderr, "=============== Checking %d\n", dis);
	#endif
	
	for (int node=1; node<=N; ++node) {	//each v in V do
		if (index[node]==0) {
			curIndex = 1;
			strongconnect(node);
		}
	}
	
	vector<int> bigSCCs;
	
	for (int c=1; c<=curComponentId; ++c) {
		if (componentSize[c] > 1)
			bigSCCs.push_back(c);
	}
	
	if (bigSCCs.size()==0)
		return true;
	
	return false;
}


int main() {
	scanf("%d %d", &N, &M);
	
	int a, b;
	for (int i=0; i<M; ++i) {
		scanf("%d %d", &a, &b);
		edges[a].push_back(b);
	}
	
	
	
	//Tarjan from wikipedia (https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm) :
	for (int node=1; node<=N; ++node) {	//each v in V do
		if (index[node]==0) {
			curIndex = 1;
			strongconnect(node);
		}
	}
	
	
	//Check sizes of components:
	//If there is just one SCC with size > 1 then proceed
	//If there is more than just one SCC with size > 1 then print 0
	//If there is only components with size 1 then print NIE
	
	vector<int> bigSCCs;
	
	for (int c=1; c<=curComponentId; ++c) {
		if (componentSize[c] > 1)
			bigSCCs.push_back(c);
	}
	
	if (bigSCCs.size() == 0) {
		#ifdef DEBUG
			fprintf(stderr, "bigSCCs.size() == 0\n");
		#endif
		printf("NIE\n");
		return 0;
	} else if (bigSCCs.size() >= 2) {
		#ifdef DEBUG
			fprintf(stderr, "bigSCCs.size() >= 2\n");
		#endif
		printf("0\n\n");
		return 0;
	}
	
	#ifdef DEBUG
		fprintf(stderr, "bigSCCs[0] == %d\n", bigSCCs[0]);
	#endif
	
	
	vector<int> res;
	vector<int> nodesToCheck;
	//There is just one SSC, namely bigSSC[0]:
	for (int node=1; node<=N; ++node) {
		if (componentId[node] != bigSCCs[0])
			continue;
		
		nodesToCheck.push_back(node);
	}
	
	for (vector<int>::iterator vit=nodesToCheck.begin(); vit!=nodesToCheck.end(); ++vit) {
		if (checkNode(*vit))
			res.push_back(*vit);
	}
	
	printf("%d\n", (int)res.size());
	
	for (int i=0; i<(int)res.size(); ++i)
		printf("%d ", res[i]);
	
	printf("\n");
	
	return 0;
}