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
#include<iostream>
#include<vector>
#include<stack>
using namespace std;

struct List
{
	List * next;
	int v;
};

vector <bool> visited;
stack <int> cycle;
List ** graph;

bool DFS(int v, int w)
{
	visited[w]=1;
	cycle.push(w);
	List * p = graph[w];
	while(p)
	{
		int u=p->v;
		if(u == v) return true;
		if(!visited[u] && DFS(v, u)) return true;
		p=p->next;
	}
	cycle.pop();
	return false;
}

int main()
{
	int n, m;
	cin >> n >> m;
	graph = new List * [n];
	for(int i=0; i<n; i++) visited.push_back(0);
	for(int i=0; i<n; i++) graph[i] = NULL; 
	for(int i=0; i<m; i++)
	{
		int a, b;
		cin >> a >> b;
		List * p = new List;
		p->v = b-1;
		p->next = graph[a-1];
		graph[a-1]=p;
	}
	int licznik=0, sort[n];
	for(int i=0; i<n; i++) sort[i]=0;
	for(int i=0; i<n; i++) 
	{
		if(DFS(i, i))
		{
			licznik++;
			int cyclesize=cycle.size();
			for(int j=0; j<cyclesize; j++)
			{
				sort[cycle.top()]++;
				cycle.pop();
			}
			for(int j=0; j<n; j++) visited[j]=0;
		}
	}
	if(!licznik) cout << "NIE";
	else 
	{
		vector <int> answer;
		for(int i=0; i<n; i++) if(sort[i] == licznik) answer.push_back(i+1);
		cout << answer.size() << "\n";
		for(int i=0; i<answer.size(); i++) cout << answer[i] << " ";
	}
}