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
#include <cstdio>
#include <vector>
using namespace std;
struct jp{
	vector<int>v;
};
jp t[1000111];
vector<int>wyn;
int odw[1000111], cykle, n, m, a, b, pom, obecny;
void dfsc(int w)
{
	odw[w]=1;
	for(vector<int>::iterator it=t[w].v.begin(); it!=t[w].v.end(); it++)
	{
		if(odw[*it]==1)cykle++;
		if(odw[*it]==0)dfsc(*it);
	}
	odw[w]=2;
}
void dfs(int w)
{
	odw[w]=1;
	for(vector<int>::iterator it=t[w].v.begin(); it!=t[w].v.end(); it++)
	{
		if(odw[*it]==1&&obecny==*it)pom++;
		if(odw[*it]==0)dfs(*it);
	}
	odw[w]=2;
}
int main()
{
	scanf("%d%d", &n, &m);
	for(int i=1; i<=m; i++)
	{
		scanf("%d%d", &a, &b);
		t[a].v.push_back(b);
	}
	for(int i=1; i<=n; i++)
	{
		if(odw[i]==0)dfsc(i);
	}
	if(cykle==0)
	{
		printf("NIE");
		return 0;
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)odw[j]=0;
		pom=0;
		obecny=i;
		dfs(i);
		if(pom==cykle)wyn.push_back(i);
	}
	if(wyn.empty())printf("NIE");
	else
	{
		printf("%d\n", wyn.size());
		for(vector<int>::iterator it=wyn.begin(); it!=wyn.end(); it++)printf("%d ", *it);
	}
	getchar();
	getchar();
	return 0;
}