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


#define N_MAX 200002

using namespace std;

struct vertex : public vector<int>
{
    enum state_t { notVisited, inDfs, visited };
    state_t state;
    unsigned int inCycles;
    int parent;
};

class graph_t
{
public:
    graph_t() : cyclesFound(0), V(N_MAX), cycleIsles(0) {}

    unsigned int numVertices, numEdges, cyclesFound;
    vector<vertex> V;
    int cycleIsles;

    void addEdge(int a, int b)
    {
        V[a].push_back(b);
    }

    void dfs(int v)
    {
        //cout << "DFSing vertex " << v+1 << endl;
        bool cycleFound = false;
        stack<int> s;
        s.push(v);
        while (!s.empty())
        {
            int x;
            v = s.top();
            if (V[v].state == vertex::notVisited)
            {
                //out << " Visiting vertex " << v + 1 << endl;
                V[v].state = vertex::inDfs;
            }
            else // vertex::inDfs
            {
                //cout << " Exiting vertex " << v + 1 << endl;
                V[v].state = vertex::visited;
                s.pop();
                continue;
            }

            for (vertex::iterator it = V[v].begin(); it != V[v].end(); ++it)
            {
                if (V[*it].state == vertex::notVisited)
                {
                    s.push(*it);
                    V[*it].parent = v;
                }
                else if (V[*it].state == vertex::inDfs)
                {
                    //cout << "Cycle found, " << v + 1 << " -> " << *it + 1 << endl;
                    if (!cycleFound) cycleFound = true;
                    ++cyclesFound;
                    x = v;
                    while ( x != *it)
                    {
                        ++(V[x].inCycles);
                        //cout << "  " << x+1 << " is there" << endl;
                        x = V[x].parent;
                    }

                    ++(V[x].inCycles); // ostatni trzeba ręcznie
                    //cout << "  " << x+1 << " is there" << endl;
                }
            }
        }

        if (cycleFound)
           ++cycleIsles;
        //cout << "isles: " << cycleIsles << endl;}
    }

    void printInAllCycles()
    {
        int goodNum = 0;
        vector<int> good;
        for (unsigned int i = 0; i < numVertices; ++i)
        {
            if ( V[i].inCycles == cyclesFound) // in all cycles
            {
                ++goodNum;
                good.push_back(i+1);
            }
        }
        cout << goodNum << "\n";
        for (vector<int>::iterator it = good.begin(); it != good.end(); ++it)
        {
            cout << *it << " ";
        }
    }
};

graph_t graph;
int a,b;

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> graph.numVertices >> graph.numEdges;

    for (unsigned int i = 0; i < graph.numEdges; ++i)
    {
        //cout << i+1 << " out of " << graph.numEdges << "\n";
        cin >> a >> b;
        graph.addEdge(a-1,b-1);
    }

    //cout << "done!" << endl;

    for (unsigned int i = 0; i < graph.numVertices; ++i)
    {
        if (graph.V[i].state == vertex::notVisited) graph.dfs(i);
        if (graph.cycleIsles > 1)
        {
            cout << "0\n";
            return 0;
        }
    }

    //cout << "==================" << endl;

    if (graph.cyclesFound == 0)
    {
        cout << "NIE\n";
        return 0;
    }
    else
    {
        graph.printInAllCycles();
    }

    return 0;
}