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
273
274
275
276
277
278
279
280
281
282
283
284
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int maxN = 5e5 + 10;
const int maxM = 1e6 + 10;

vector<int>nbh[maxN];
vector<int>_nbh[maxN];
int edges[maxM][2];
int num[maxN];
int B[maxN];
int _B[maxN];
int n, m, who;

vector<int>ans;

void dfs_sort(int v) {
    B[v] = true;
    for(int a: nbh[v])
        if(!B[a])
            dfs_sort(a);
    ans.push_back(v);
}

int nr = 1;
void dfs_nr(int v) {
    B[v] = false;
    num[v] = nr;
    for(int a: _nbh[v])
        if(B[a])
            dfs_nr(a);
}

const int inf = 1e9;
int dep[maxN];
int dep_min = 0, dep_max = inf;
int from_main[maxN];
int ambiguous_add[maxN];
int ambiguous_rem[maxN];

void dfs_ambiguous(int v) {
//     printf("dfs: %d\n", v);
    B[v] = true;
    if(!from_main[v]) {
        bool is_first = true;
        for(int a: nbh[v]) {
            if(!B[a]) { // new
                if(!is_first)
                    from_main[a] = v;
                dep[a] = dep[v] + 1;
                is_first = false;
                dfs_ambiguous(a);
            } else if(!_B[a]) { // cycle
                dep_min = max(dep_min, dep[a]);
                ambiguous_add[v]++;
            } else { // touch
                if(from_main[a]) {
                    if(dep[from_main[a]] > dep[v]) {
                        ambiguous_add[v]++;
                        ambiguous_add[from_main[a]]--;
                    }
                } else {
                    ambiguous_rem[a]--;
                    ambiguous_add[v]++;
                }
            }
        }
    } else {
        for(int a: nbh[v]) {
            if(B[a] && !_B[a]) { // cycle
                ambiguous_add[from_main[v]]++;
                dep_min = max(dep_min, dep[a]);
            } else if(B[a]) { // touch
                if(!from_main[a])
                    ambiguous_rem[a]--;
                else
                    ambiguous_add[from_main[a]]--;
                ambiguous_add[from_main[v]]++;
            } else { // new
                from_main[a] = from_main[v];
                dep[a] = dep[v] + 1;
                dfs_ambiguous(a);
            }
        }
    }
    _B[v] = true;
}

int ambiguous_sum;
void dfs_ans(int v) {
    B[v] = false;
    ambiguous_sum += ambiguous_rem[v];
//     printf("%d: amb_sum:%d next:%d B_next:%d\n", v, ambiguous_sum, nbh[v][0], B[nbh[v][0]] ? 1 : 0);

    if(!ambiguous_sum)
        ans.push_back(v);
    ambiguous_sum += ambiguous_add[v];

    if(B[nbh[v][0]])
        dfs_ans(nbh[v][0]);
}

void clear() {
    ambiguous_sum = 0;
    dep_min = 0;
    for(int i = 1; i <= n; ++i) {
       B[i] = _B[i] = false;
       nbh[i].swap(_nbh[i]);
       from_main[i] = 0;
       ambiguous_add[i] = ambiguous_rem[i] = 0;
    }
}


/***************************** brutal ************************************/

void dfs_ambiguous_brute(int v) {
//     printf("dfs: %d\n", v);
    B[v] = 1;
    bool fst = true;
    for(int a: nbh[v]) {
        if(!B[a] && fst) {
            fst = false;
            dep[a] = dep[v] + 1;
            dfs_ambiguous_brute(a);
        } else if(B[a] && !_B[a]) {
            dep_max = min(dep_max, dep[v]);
            dep_min = max(dep_min, dep[a]);
        }
    }
    _B[v] = 1;
}


vector<int>all;

int act;
bool dfs_check(int v) {
    B[v] = act;
    for(int a: nbh[v]) {
        if(B[a] != act && dfs_check(a))
            return true;
        if(B[a] == act && _B[a] != act)
            return true;
    }
    _B[v] = act;
    return false;
}

bool check(int x) {
    act = x + n;
    B[x] = _B[x] = act;
    for(int i: all) {
        if(B[i] != act && dfs_check(i))
            return false;
    }
    return true;
}

void gen_ans() {
    dep[who] = 1;
    dfs_ambiguous_brute(who);
//     printf("min: %d max: %d\n", dep_min, dep_max);
    if(dep_min > dep_max) {
        printf("0\n\n");
        exit(0);
    }
    int x = who, next = who;
    while(dep[x] <= dep_max) {
//         printf("-- %d --\n", x);
        if(dep[x] >= dep_min && check(x))
            ans.push_back(x);
        if(dep[x] == dep_max)
            break;
        for(int i = 0; dep[next] <= dep[x]; ++i)
            next = nbh[x][i];
        x = next;
    }
}

void el_brute() {
    gen_ans();
    sort(ans.begin(), ans.end());
}

/*********************************** heural *************************************/

void el_heurezone() {
        dfs_ambiguous(who);
    dfs_ans(who);
    for(int& a: ans)
        if(dep[a] < dep_min)
            a = inf;
    clear();
    dfs_ambiguous(who);
    dfs_ans(who);
    for(int& a: ans)
        if(a < inf && dep[a] < dep_min)
            a = inf;
    sort(ans.begin(), ans.end());
    while(!ans.empty() && ans.back() == inf)
        ans.pop_back();
    int prev = -1;
    for(int& a: ans) {
        if(a == prev)
            a = inf;
        else
            prev = a;
    }

    sort(ans.begin(), ans.end());
    while(!ans.empty() && ans.back() == inf)
        ans.pop_back();
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; ++i) {
        scanf("%d%d", edges[i], edges[i] + 1);
        nbh[edges[i][0]].push_back(edges[i][1]);
        _nbh[edges[i][1]].push_back(edges[i][0]);
    }
    for(int i = 1; i <= n; ++i)
        if(!B[i])
            dfs_sort(i);
    while(ans.size()) {
        if(B[ans.back()]) {
            nr = ans.back();
            dfs_nr(ans.back());
        }
        ans.pop_back();
    }
    int sum = 0;
    for(int i = 1; i <= n; ++i) {
        if(num[i] != i && !B[num[i]]) {
            B[who = num[i]] = true;
            sum++;
        }
    }
//     printf("sum = %d\n", sum);
    if(sum == 0) {
        printf("NIE\n");
        return 0;
    }
    if(sum > 1) {
//         printf("0\n");
        printf("0\n\n");
        return 0;
    }

    B[who] = false;
    for(int i = 1; i <= n; ++i) {
        nbh[i].clear();
        _nbh[i].clear();
    }
    int _m = 0;
    for(int a, b, i = 0; i < m; ++i) {
        a = edges[i][0];
        b = edges[i][1];
        if(num[a] == num[b]) {
            nbh[a].push_back(b);
            _nbh[b].push_back(a);
            _m++;
        }
    }
    dep[who] = 1;
    for(int i = 1; i <= n; ++i) {
        if(num[i] == who) {
            all.push_back(i);
        }
    }

    if(all.size() * _m <= 1e7)
        el_brute();
    else
        el_heurezone();
    printf("%d\n", (int) ans.size());
    for(int a: ans)
        printf("%d ", a);
    printf("\n");
    return 0;
}