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
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

int crosses, roads;
int ai, bi;

vector<int> paths[1000005];
bool ignored[1000005];

bool wrong_target[1000005];
bool part_of_cycle[1000005];

bool visited[1000005];
int parent[1000005];
int leads_to_cycle[1000005];
int number[1000005];

vector<int> dfs_result;

int higher(int a, int b, int start) {
    if (a == -1) return b;
    if (b == -1) return a;
    int na = number[a];
    int nb = number[b];
    int nstart = number[start];

    if (na < nstart && nb < nstart) return na > nb ? a : b;
    if (na > nstart && nb > nstart) return na > nb ? a : b;
    if (na < nstart && nb > nstart) return a;
    if (na > nstart && nb < nstart) return b;
    if (na == nstart) return a;
    if (nb == nstart) return b;
    return max(a, b);
}

bool dfs(int current) {
    if (ignored[current]) return false;

    if (visited[current]) {
        int node = current;
        do {
            dfs_result.push_back(node);
            node = parent[node];
        } while (node != current);
        return true;
    }

    visited[current] = true;

    for (int i=0; i<paths[current].size(); ++i) {
        int neighbour = paths[current][i];
        if (!ignored[neighbour])
            parent[neighbour] = current;

        if(dfs(neighbour)) {
            ignored[current] = true;
            return true;
        }
    }

    ignored[current] = true;
    return false;
}

bool found = false;

vector<int> try_find_cycle() {
    vector<int> result;
    for (int i=0; i<crosses; ++i) {
        if (dfs(i)) {
            found = true;
//            printf("DFS: ");
//            for (int i=0; i<dfs_result.size(); ++i) printf("%d ", dfs_result[i]);
//            printf("\n");
            if (!result.empty()) {
                return vector<int>();
            }
            result = dfs_result;
        }
    }
    return result;
}

int dig(int start, int current) {
    if (part_of_cycle[current]) {
        return leads_to_cycle[current];
    }

    if (visited[current]) {
        return leads_to_cycle[current];
    }

    visited[current] = true;

    int found_c = -1;
    for (int i=0; i<paths[current].size(); ++i) {
        int neighbour = paths[current][i];
        int x = dig(start, neighbour);
        found_c = higher(found_c, x, start);
//        printf("DIGGED %d for %d -> %d\n", x+1, start+1, found_c+1);
    }

    leads_to_cycle[current] = found_c;
    return found_c;
}

vector<int> calculate_bombed_crosses(vector<int> cycle) {
    for (int i = 0; i < crosses; ++i) {
        visited[i] = false;
        leads_to_cycle[i] = -1;
    }

    for (int n = 0; n < cycle.size(); ++n) {
        number[cycle[n]] = n;
        part_of_cycle[cycle[n]] = true;
        leads_to_cycle[cycle[n]] = cycle[n];
    }

//    printf("CYCLE: ");
//    for (int i=0; i<cycle.size(); ++i) printf("%d ", cycle[i] + 1);
//    printf("\n");

    for (int k = 0; k < cycle.size(); ++k) {
        int node = cycle[k];
//        printf("ST: %d\n", node+1);
        for (int i = 0; i < paths[node].size(); ++i) {
            int iter = dig(node, paths[node][i]);
//            printf("%d <--> %d\n", node + 1, iter + 1);
            while (iter != -1 && parent[iter] != node) {
                wrong_target[parent[iter]] = true;
                iter = parent[iter];
            }
        }
    }

    vector<int> result;
    for (int i=0; i<cycle.size(); ++i) {
        if (!wrong_target[cycle[i]]) {
            result.push_back(cycle[i]);
        }
    }
    return result;
}

int main() {
    scanf("%d %d", &crosses, &roads);

    for (int i=0; i<roads; ++i) {
        scanf("%d %d", &ai, &bi);
        paths[ai-1].push_back(bi-1);
    }

    vector<int> cycle = try_find_cycle();
    std::reverse(cycle.begin(), cycle.end());

    if (!found) {
        printf("NIE\n");
    } else if (cycle.empty()) {
        printf("0\n");
    } else {
        vector<int> bombed_crosses = calculate_bombed_crosses(cycle);
        printf("%lu\n", bombed_crosses.size());
        sort(bombed_crosses.begin(), bombed_crosses.end());
        for (int i=0; i<bombed_crosses.size(); ++i) {
            printf("%d ", bombed_crosses[i]+1);
        }
        printf("\n");
    }
    return 0;
}