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
#include <cstdlib>
#include <stdio.h>
#include <iostream>

#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <list>
#include <iterator>

using namespace std;

typedef vector< list<size_t> > AdjListT;
AdjListT A, B;
vector<size_t> node_stack;
vector<size_t> blocked;
size_t s;

int ile_cykli = 0;

void UNBLOCK(size_t);
bool CIRCUIT(size_t);
void OutputCycle(vector<size_t>&);



bool CIRCUIT(size_t v)
{
	if (ile_cykli>5)
		return false;
	
    bool f = false;
    node_stack.push_back(v);
    blocked[v] = true;
    for (list<size_t>::iterator iw = A[v].begin(); iw != A[v].end(); ++iw) {
        size_t w = *iw;
        if (w == s) 
		{
            OutputCycle(node_stack);
            f = true;
        } 
		else 
			if (!blocked[w]) 
			{
              if (CIRCUIT(w))
                  f = true;
			}
    }
    if (f)
        UNBLOCK(v);
    else
       for (list<size_t>::iterator iw = A[v].begin(); iw != A[v].end(); ++iw) {
           size_t w = *iw;
           if (find(B[w].begin(), B[w].end(), v) == B[w].end())
               B[w].push_back(v);
        }

    node_stack.pop_back();
    return f;
}

void UNBLOCK(size_t u)
{
    blocked[u] = false;
    for (list<size_t>::iterator iw = B[u].begin(); iw != B[u].end();) {
        size_t w = *iw;
        iw = B[u].erase(iw);
        if (blocked[w])
            UNBLOCK(w);
    }
    return;
}

vector< set<int> const* > sety = vector< set<int> const* >();

void OutputCycle(vector<size_t>& node_stack)
{
	set<int> *newset = new set<int>();

    for (vector<size_t>::iterator i = node_stack.begin(); i != node_stack.end(); ++i)
	{
   //     cout << *i << " ";
	
		newset->insert(*i);
	}
    
	ile_cykli++;
	sety.push_back(newset);
//	cout << endl;
    return;
}




std::vector<int> intersect(vector< set<int> const* > const& sets) {

    std::vector<int> result; // only return this one, for NRVO to kick in

    // 0. Check obvious cases
    if (sets.empty()) { return result; }

    if (sets.size() == 1) {
        result.assign(sets.front()->begin(), sets.front()->end());
        return result;
    }


    // 1. Merge first two sets in the result
    std::set_intersection(sets[0]->begin(), sets[0]->end(),
                          sets[1]->begin(), sets[1]->end(),
                          std::back_inserter(result));

    if (sets.size() == 2) { return result; }


    // 2. Merge consecutive sets with result into buffer, then swap them around
    //    so that the "result" is always in result at the end of the loop.

    std::vector<int> buffer; // outside the loop so that we reuse its memory

    for (size_t i = 2; i < sets.size(); ++i) {
        buffer.clear();

        std::set_intersection(result.begin(), result.end(),
                              sets[i]->begin(), sets[i]->end(),
                              std::back_inserter(buffer));

        swap(result, buffer);
    }

    return result;
}

int main()
{
	std::ios_base::sync_with_stdio(0);
	
	
	size_t N, m;

    std::cin >> N >> m;
	
	A.resize(N+1);
    blocked.resize(N+1);
    B.resize(N+1);
	
	int u,v;
	for(int i=0;i<m;i++)
	{
		cin >> u >> v;
		A.at(u).push_back(v);
	}

	for (size_t i = 0; i <= N; ++i)
        blocked[i] = false;
    s = 0;
	
	while (s <= N) {
        CIRCUIT(s);
		ile_cykli=0;

        A[s].clear();
        for (size_t i = 0; i < N; ++i)
            A[i].remove(s);
        for (size_t i = 0; i <= N; ++i) 
		{
            blocked[i] = false;
            B[i].clear();
        }

        ++s;
    }

	auto result = intersect(sety);
	if (sety.size()==0)
	{
		std::cout << "NIE" << std::endl;
	}
	else
	{
		std::cout << result.size() << std::endl;
		for (vector<int>::iterator i = result.begin(); i != result.end(); ++i)
		{
			cout << *i << " ";
		}
	}

	return 0;
}