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
//============================================================================
// Name        : Kontrmanifestacja.cpp
// Author      : Tytus Bierwiaczonek
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <list>
using namespace std;

const uint32_t size = 500000;
list<uint32_t> adjancencyList[size];
uint32_t visited[size]; // 0 not visited, 1 left, 2 visited in current cycle
uint32_t nodeModifier[size];
uint32_t cycleCounter[size];
uint32_t cycles = 0;

uint32_t dfs(uint32_t id, uint32_t run)
{
	visited[id] = 2 * run;
	uint32_t tempCycles = 0;

	for (list<uint32_t>::iterator it = adjancencyList[id].begin();
			it != adjancencyList[id].end(); ++it)
	{
		if (visited[*it] == 2 * run -1 || visited[*it] == 0)
		{
			tempCycles += dfs(*it, run);
		}
		else if (visited[*it] == 2 * run)
		{
			++tempCycles;
			++cycles;
			++nodeModifier[*it];
		}
	}

	visited[id] = 2 * run - 1;
	cycleCounter[id] += tempCycles;
	tempCycles -= nodeModifier[id];
	nodeModifier[id] = 0;

	return tempCycles;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	uint32_t n, m, i, a, b, k = 0;

	cin >> n >> m;

	for (i = 0; i < m; ++i)
	{
		cin >> a >> b;
		--a;
		--b;
		adjancencyList[a].push_back(b);
	}

	for (i = 0; i < n; ++i)
	{
		visited[i] = 0;
		nodeModifier[i] = 0;
		cycleCounter[i] = 0;
	}

	a = 0;

	for (i = 0; i < n; ++i)
	{
		if (0 == visited[i])
		{
			++a;
			dfs(i, a);
		}
	}

	if (0 == cycles)
	{
		cout << "NIE" << endl;
	}
	else
	{
		for (i = 0; i < n; ++i)
		{
			if (cycleCounter[i] == cycles)
			{
				nodeModifier[k++] = i + 1;
			}
		}

		cout << k << endl;
		for (i = 0; i < k; ++i)
		{
			cout << nodeModifier[i] << " ";
		}
	}
	return 0;
}