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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;

#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair

#define INF 1000000001

/**
 * Reprezentacja grafu oraz algorytmy grafowe na podstawie książki:
 * Piotr Stańczyk
 * "Algorytmika praktyczna Nie tylko dla mistrzów"
 * PWN 2009
 */
template<class V, class E> struct Graph {

    // Represents Edges
    struct Ed : E {
        int v;
        Ed(E p, int w) : E(p), v(w) { }
    };

    // Represents Vertices
    struct Ve : V, vector<Ed> { };

    // vertices in graph
    vector<Ve> g;

    // n - number of vertices in graph
    Graph(int n = 0) : g(n) { }

    void EdgeD(int b, int e, E d = E()) {
        g[b].PB(Ed(d, e));
    }

    int tdfs;
    int nr; // used by Strongly Connected Components algorithms

    // excluded vertices
    vector<vector<PII>> exv;
    vector<PII> cd;

    // size of SCC
    VI vcnt;

    VI st;

    // Used by SccS() method
    void SccSDfs(int v) {
        if (g[v].t != -1)
            return;

        if (nr >= 0) { // Phase 2
            ++(vcnt[nr]);
            g[v].d = ++tdfs;
            st.PB(g[v].d);
        }

        g[v].t = nr;
        FOREACH(it, g[v]) {
            if (g[it->v].t == -1) {
                SccSDfs(it->v);
            } else if (nr >= 0 && g[v].t == g[it->v].t) {
                // 2nd phase, same SCC

                // back edge
                if (g[it->v].f == -1) {
                    // back edge found cycle
                    cd[nr].ST = max(cd[nr].ST, g[it->v].d);
                    auto up = --(upper_bound(ALL(st), cd[nr].ND));
                    cd[nr].ND = min(*up, g[v].d);

                    auto lca = --(upper_bound(ALL(st), g[it->v].d));
                    if (*lca != g[it->v].d)
                        exv[nr].PB(MP(*lca + 1, g[it->v].d - 1));
                } else { //forward edge
                    auto lca = --(upper_bound(ALL(st), g[it->v].d));
                    exv[nr].PB(MP(*lca + 1, g[it->v].d - 1));
                    exv[nr].PB(MP(*(++lca), g[v].d));
                }
            }
        }

        // -3 for counting components from 0
        if (nr < 0) g[v].t = -(--nr) - 3;

        if (nr >= 0) { // Phase 2
            g[v].f = g[v].d;
            st.pop_back();
        }
    }

    // Strongly connected componets - this version
    // doesn't compute strongly connected components graph
    // Requires:
    //      int V::t
    //      int V::d
    //      int V::f
    void SccS() {
        Graph<V, E> gt(SIZE(g));
        REP(x, SIZE(g)) {
            g[x].t = gt.g[x].t = -1;
            g[x].d = g[x].f = gt.g[x].d = gt.g[x].f = -1;
            FOREACH(it, g[x]) gt.EdgeD(it->v, x);
        }

        gt.nr = -2;
        gt.tdfs = -1;
        VI v(SIZE(g));
        REP(x, SIZE(g)) {
            gt.SccSDfs(x);
            v[gt.g[x].t] = x;
        }

        nr = 0;
        FORD(x, SIZE(g)-1, 0) if (g[v[x]].t == -1) {
            tdfs = -1;
            vcnt.PB(0);
            cd.PB(MP(0, INF));
            exv.PB({});
            SccSDfs(v[x]);
            nr++;
        }
    }

    // -1 no such route
    int FindVerts(VI& vout) {
        vout.clear();
        SccS();

        int k = -1;
        REP(x, SIZE(vcnt)) {
            if (k == -1 && vcnt[x] > 1)
                k = x;
            else if (k != -1 && vcnt[x] > 1)
                return 0;
        }

        if (k == -1)
            return -1;

        VI ds(vcnt[k]);
        REP(v, SIZE(g)) if (g[v].t == k) ds[g[v].d] = v;

        sort(ALL(exv[k]));
        int i = cd[k].ST;
        int mx = min(cd[k].ND, vcnt[k] - 1);

        for (auto p: exv[k]) {
            for (; i < p.ST && i <= mx; ++i)
                vout.PB(ds[i]);

            if (i > mx) break;

            i = max(i, p.ND + 1);
        }

        for (; i <= mx; ++i) vout.PB(ds[i]);

        sort(ALL(vout));
        return SIZE(vout);
    }
};

struct Edge { };
struct Vert {
    int t, d, f;
};

int main()
{
    //ios_base::sync_with_stdio(0);
    int n, m, a, b;

    cin >> n >> m;
    Graph<Vert, Edge> g(n);
    REP(i, m) {
        cin >> a >> b;
        g.EdgeD(--a, --b);
    }

    VI v;
    int k = g.FindVerts(v);
    if (k == -1) {
        cout << "NIE\n";
    } else {
        cout << k << "\n";
        REP(x, SIZE(v)) {
            if (x > 0) cout << " ";
            cout << (v[x] + 1);
        }
        cout << "\n";
    }

    return 0;
}