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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector < pair <int,int> > kr[500005];
vector < int > kr2[500005], kr3[500005];
int n,m;
int odw[500005];
bool used[1000005];
pair <int,int> edge[1000005];
int nr[500005];
vector <int> cycle;
bool other_cycle = false;
int value[500005][4];
bool drzewo[6000005];
void add_tree(int pos, int s, int e, int cs, int ce, bool v){
    if (s > e){
        add_tree(pos, s, cycle.size()*3, cs, ce, v);
        add_tree(pos, 1, e, cs, ce, v);
    }
    else if ((s == cs) && (e == ce)){
        drzewo[pos] = v;
    }
    else if ((s <= (cs+ce)/2) && (e <= (cs+ce)/2)){
        add_tree(pos*2, s, e, cs, (cs+ce)/2, v);
    }
    else if ((s > (cs+ce)/2) && (e > (cs+ce)/2)){
        add_tree(pos*2+1, s, e, (cs+ce)/2+1, ce, v);
    }
    else {
        add_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2, v);
        add_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce, v);
    }
}
bool get_tree(int pos, int s, int cs, int ce){
    if (drzewo[pos])
        return true;
    if ((s == cs) && (s == ce)){
        return drzewo[pos];
    }
    else if (s <= (cs+ce)/2){
        return get_tree(pos*2, s, cs, (cs+ce)/2);
    }
    else {
        return get_tree(pos*2+1, s, (cs+ce)/2+1, ce);
    }
}
int primary_cycle(int start){
    odw[start] = 1;
    for (int i = 0; i < kr[start].size(); i++){
        if (odw[ kr[start][i].first ] == 0){
            int t = primary_cycle(kr[start][i].first);
            if (t == -2){
                return t;
            }
            if (t != -1){
                cycle.push_back(start);
                used[ kr[start][i].second ] = true;
                if (t == start){
                    return -2;
                }
                else {
                    return t;
                }
            }
        }
        else if (odw[ kr[start][i].first ] == 1){
            cycle.push_back(start);
            used[ kr[start][i].second ] = true;
            return kr[start][i].first;
        }
    }
    odw[start] = 2;
    return -1;
}
void second_cycle(int start){
    odw[start] = 1;
    for (int i = 0; i < kr2[start].size(); i++){
        if (odw[ kr2[start][i] ] == 0){
            second_cycle(kr2[start][i]);
        }
        else if (odw[ kr2[start][i] ] == 1){
            other_cycle = true;
        }
    }
    odw[start] = 2;
}
int dfs_min_2(int start){
    odw[start] = 1;
    int ret = 2000000002;
    if (nr[start] != -1){
        value[start][0] = nr[start];
        return nr[start];
    }
    for (int i = 0; i < kr2[start].size(); i++){
        if (odw[ kr2[start][i] ] < 1){
            ret = min(ret, dfs_min_2(kr2[start][i]));
        }
        else {
            ret = min(ret, value[kr2[start][i]][0]);
        }
    }
    value[start][0] = ret;
    return ret;
}
int dfs_max_2(int start){
    odw[start] = 2;
    int ret = -1;
    if (nr[start] != -1){
        value[start][1] = nr[start];
        return nr[start];
    }
    for (int i = 0; i < kr2[start].size(); i++){
        if (odw[ kr2[start][i] ] < 2){
            ret = max(ret, dfs_max_2(kr2[start][i]));
        }
        else {
            ret = max(ret, value[kr2[start][i]][1]);
        }
    }
    value[start][1] = ret;
    return ret;
}
int dfs_min_3(int start){
    odw[start] = 3;
    int ret = 2000000002;
    if (nr[start] != -1){
        value[start][2] = nr[start];
        return nr[start];
    }
    for (int i = 0; i < kr3[start].size(); i++){
        if (odw[ kr3[start][i] ] < 3){
            ret = min(ret, dfs_min_3(kr3[start][i]));
        }
        else {
            ret = min(ret, value[kr3[start][i]][2]);
        }
    }
    value[start][2] = ret;
    return ret;
}
int dfs_max_3(int start){
    odw[start] = 4;
    int ret = -1;
    if (nr[start] != -1){
        value[start][3] = nr[start];
        return nr[start];
    }
    for (int i = 0; i < kr3[start].size(); i++){
        if (odw[ kr3[start][i] ] < 4){
            ret = max(ret, dfs_max_3(kr3[start][i]));
        }
        else {
            ret = max(ret, value[kr3[start][i]][3]);
        }
    }
    value[start][3] = ret;
    return ret;
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i = 0; i < m; i++){
        int a,b;
        scanf("%d%d",&a,&b);
        kr[a].push_back(make_pair(b,i));
        edge[i] = make_pair(a,b);
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            primary_cycle(i);
            if (cycle.size() > 0)
                break;
        }
    }
    if (cycle.size() == 0){
        printf("NIE\n");
        return 0;
    }
    for (int i = 1; i <= n; i++){
        odw[i] = 0;
    }
    for (int i = 0; i < cycle.size(); i++){
        odw[ cycle[i] ] = 2;
    }
    for (int i = 0; i < m; i++){
        if (!used[i])
            kr2[edge[i].first].push_back(edge[i].second);
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            second_cycle(i);
            if (other_cycle)
                break;
        }
    }
    if (other_cycle){
        printf("%d\n", 0);
        return 0;
    }
    for (int i = 1; i <= n; i++){
        kr2[i].clear();
        odw[i] = 0;
        nr[i] = -1;
    }
    for (int i = 0; i < cycle.size(); i++){
        nr[ cycle[i] ] = cycle.size() - i;
    }
    /*for (int i = cycle.size()-1; i >= 0; i--){
        printf("%d ", cycle[i]);
    }
    printf("\n");*/
    for (int i = 0; i < m; i++){
        if (!used[i]){
            kr2[edge[i].first].push_back(edge[i].second);
            kr3[edge[i].second].push_back(edge[i].first);
        }
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            dfs_min_2(i);
        }
    }
    for (int i = 1; i <= n; i++){
        odw[i] = 0;
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            dfs_max_2(i);
        }
    }
    for (int i = 1; i <= n; i++){
        odw[i] = 0;
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            dfs_min_3(i);
        }
    }
    for (int i = 1; i <= n; i++){
        odw[i] = 0;
    }
    for (int i = 1; i <= n; i++){
        if (odw[i] == 0){
            dfs_max_3(i);
        }
    }
    for (int i = 0; i < m; i++){
        if (!used[i]){
            int a = edge[i].first, b = edge[i].second;
            //printf("www %d %d %d %d %d %d\n", a, b, value[b][0], value[b][1], value[a][2], value[a][3]);
            if ((value[b][1] != -1) && (value[a][2] != 2000000002)){
                add_tree(1, (value[a][2]-1) * 3 + 3, (value[b][1]-1) * 3 + 1, 1, cycle.size()*3, true);
            }
            if ((value[b][0] != 2000000002) && (value[a][3] != -1)){
                add_tree(1, (value[a][3]-1) * 3 + 3, (value[b][0]-1) * 3 + 1, 1, cycle.size()*3, true);
            }
        }
    }
    vector <int> odp;
    for (int i = cycle.size()-1; i >= 0; i--){
        bool f = get_tree(1, (nr[ cycle[i] ] - 1)*3 + 2, 1, cycle.size()*3);
        if (!f){
            odp.push_back(cycle[i]);
        }
    }
    sort(odp.begin(), odp.end());
    printf("%d\n", odp.size());
    for (int i = 0; i < odp.size(); i++){
        printf("%d ", odp[i]);
    }
}