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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;


#define DEBUG false
#define log if (DEBUG) cerr
typedef long long ll;
typedef unsigned long long ull;




struct Crossing
{
    int id;
    set<int> nexts;
};

map<pair<int, int>, bool> roads;
vector<Crossing> crossings;
set<int> sureCrossings;
bool cycleExists = false;
int numberOfCrossings, numberOfRoads;


void logSet(set<int> s)
{
    if (DEBUG)
    {
        log << "{ ";
        for (auto a : s) log << a << ',';
        log << " }" << endl;
    }
}

void readData()
{
    int i, a, b;
    cin >> numberOfCrossings >> numberOfRoads;
    crossings.resize(numberOfCrossings + 1);
    for (i = 0; i <= numberOfCrossings; i++)
    {
        crossings[i].id = i;
    }

    for (i = 0; i < numberOfRoads; i++)
    {
        cin >> a >> b;
        crossings[a].nexts.insert(b);
        roads[make_pair(a, b)] = false;
    }
}

void processCycle(vector<int>& path, map<int, set<int>>& crossingsInPath, Crossing& crossing)
{
    auto indexIt = crossingsInPath[crossing.id].end();
    indexIt --;
    int startIdx = *indexIt;
    if (cycleExists)
    {
        set<int> crossingsInCycle;
        for (int i = startIdx; i < (int)path.size(); i++)
        {
            crossingsInCycle.insert(path[i]);
        }
        logSet(crossingsInCycle);

        set<int> newSureCrossings;
        set_intersection(sureCrossings.begin(), sureCrossings.end(), crossingsInCycle.begin(),
                crossingsInCycle.end(), inserter(newSureCrossings, newSureCrossings.begin()));

        sureCrossings = newSureCrossings;
        if (sureCrossings.empty())
        {
            throw 0;
        }
    }
    else
    {
        for (int i = startIdx; i < (int)path.size(); i++)
        {
            sureCrossings.insert(path[i]);
        }
        log << "First cycle:" << endl;
        logSet(sureCrossings);
        cycleExists = true;
    }
}

void solveRecursive(vector<int>& path, map<int, set<int>>& crossingsInPath, Crossing& crossing)
{
    if (crossingsInPath.find(crossing.id) != crossingsInPath.end())
    {
        processCycle(path, crossingsInPath, crossing);
    }

    crossingsInPath[crossing.id].insert(path.size());
    path.push_back(crossing.id);
    while (!crossing.nexts.empty())
    {
        Crossing& nextCrossing = crossings[*crossing.nexts.begin()];
        roads[make_pair(crossing.id, nextCrossing.id)] = true;
        crossing.nexts.erase(crossing.nexts.begin());
        solveRecursive(path, crossingsInPath, nextCrossing);
    }
    path.pop_back();
    crossingsInPath[crossing.id].erase(path.size());
    if (crossingsInPath[crossing.id].empty())
    {
        crossingsInPath.erase(crossing.id);
    }
}

void solve()
{
    try
    {
        for (auto& road : roads)
        {
            if (road.second) continue;

            vector<int> path;
            map<int, set<int>> crossingsInPath;

            solveRecursive(path, crossingsInPath, crossings[road.first.first]);
        }
    }
    catch (int exception)
    {
        log << "Zero crossings caught" << endl;
    }

    if (cycleExists)
    {
        cout << sureCrossings.size() << endl;
        for (int c : sureCrossings) cout << c << ' ';
    }
    else
    {
        cout << "NIE";
    }
}

int main()
{
    readData();
    solve();
    return 0;
}