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

using namespace std;

static const unsigned long CON_MAX_N = 500001;
static const unsigned long CON_MAX_M = 1000000;

static long gEdges[2*CON_MAX_M];
static long gEdgesBuf[CON_MAX_M];

struct SNode
{
	long nEdges;
	bool bInAllCycles; 
	bool bVisited;
	bool bInPath;
	long *pEdges;
	SNode() : nEdges(0), bInAllCycles(true), bVisited(false), bInPath(false), pEdges(NULL) {}
};

static unsigned long gGraphSize, gResult;
static SNode gGraph[CON_MAX_N];
static bool gbInCycle[CON_MAX_N];

struct SStackNode
{
	long num;
	long egdeIdx;
	SStackNode(long n) : num(n), egdeIdx(0){}
};

bool dfs(unsigned long start) // wglab
{
	vector<SStackNode> dfsStack;
	bool res = false;
	unsigned long idx;
	
	gGraph[start].bVisited = true;
	gGraph[start].bInPath = true;
	dfsStack.push_back(SStackNode(start));
	while(!dfsStack.empty())
	{
		SStackNode &curr = dfsStack.back();

		if (curr.egdeIdx == gGraph[curr.num].nEdges)
		{
			dfsStack.pop_back();
			gGraph[curr.num].bInPath = false;
			continue;
		}

		while (curr.egdeIdx < gGraph[curr.num].nEdges)
		{
			unsigned long next = gGraph[curr.num].pEdges[curr.egdeIdx];
			++ curr.egdeIdx;
			if(!gGraph[next].bInPath)
			{
				gGraph[next].bVisited = true;
				gGraph[next].bInPath = true;
				dfsStack.push_back(SStackNode(next));
				break;
			}
			else
			{
				memset(gbInCycle, 0, sizeof(gbInCycle));
				for(idx = 0; idx < dfsStack.size() && dfsStack[idx].num != next; ++ idx);
				for(; idx < dfsStack.size(); ++ idx)
				{
					gbInCycle[dfsStack[idx].num] = true;
				}
				for(idx = 1; idx <= gGraphSize; ++ idx)
				{
					if(gGraph[idx].bInAllCycles && !gbInCycle[idx])
					{
						-- gResult;
					}
					gGraph[idx].bInAllCycles = (gGraph[idx].bInAllCycles && gbInCycle[idx]);
				}
				if(gResult == 0)
				{
					return true;
				}
				res = true;
			}
		}
	}
	return res;
}

int main()
{
	bool bIsCycle = false;
	unsigned long M, i, idx = 0;


	
	cin >> gGraphSize >> M;
	gResult = gGraphSize;
	for(i = 0; i < M*2; i += 2)
	{
		cin >> gEdges[i] >> gEdges[i+1];
		++ gGraph[gEdges[i]].nEdges;
	}
	for(i = 1; i <= gGraphSize; ++i)
	{
		gGraph[i].pEdges = &gEdgesBuf[idx];
		idx += gGraph[i].nEdges;
		gGraph[i].nEdges = 0;
	}
	for(i = 0; i < M*2; i += 2)
	{
		gGraph[gEdges[i]].pEdges[gGraph[gEdges[i]].nEdges] = gEdges[i+1];
		++ gGraph[gEdges[i]].nEdges;
	}

	for(i = 1; i <= gGraphSize; ++i)
	{
		if(!gGraph[i].bVisited && dfs(i))
		{
			bIsCycle = true;
			if(gResult == 0)
			{
				break;
			}
		}
	}

	if(bIsCycle)
	{
		cout << gResult << endl;
		if(gResult > 0)
		{
			for(i = 1; i <= gGraphSize; ++i)
			{
				if(gGraph[i].bInAllCycles)
				{
					cout << i << " "; 
				}
			}
		}
	}
	else
	{
		cout << "NIE";
	}
}