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

using namespace std;

vector<vector< int > > v;
vector<vector< int > > reversed;
vector<pair<int, int> > time1;
vector<int> result;
vector<int> numberOfComponents;
stack<int> st;
vector<int> vecCycle;
int components=0;
int iniComponents=0;
vector<int>component;
int ctr=-1;

void dfs(int x, int fake){
    //cout << "x: " << x << "\n";
    ctr++;
    time1[x].first=ctr;

    for(unsigned int i = 0; i < v[x].size(); i++){
        if(v[x][i] != fake && time1[v[x][i]].first==-1){
            dfs(v[x][i], fake);
        }
    }
    ctr++;
    time1[x].second=ctr;
    st.push(x);
}

void reversedDfs(int x, int fake){
    //cout << "reverse x: " << x <<  ", fake= " << fake << "\n";
    ctr++;
    time1[x].first=ctr;
    component[x]=components;
    //cout << x <<  "(" << components << ")\n";
    for(unsigned int i = 0; i < reversed[x].size(); i++){
        if(reversed[x][i] != fake && time1[reversed[x][i]].first == -1){
            //cout << "rtfl: " << v[x][i] << "\n";
            reversedDfs(reversed[x][i], fake);
        }
    }
    ctr++;
    time1[x].second=ctr;
}

int n, m;

void clearStack(){
    while(!st.empty()){
        st.pop();
    }
}

void sss(int fake){
    for(int i = 0; i <= n; i++){
        component[i]=0;
    }
    for(int i = 0; i <= n; i++){
        time1[i]=make_pair(-1, -1);
    }
    ctr=-1;
    clearStack();
    for(int i = 1; i <= n; i++){
        if(i == fake)continue;
        if(time1[i].first==-1)dfs(i, fake);
    }
    components=0;
    ctr=-1;
    time1.clear();
    time1.resize(n+1, make_pair(-1, -1));
    while(!st.empty()){
        int x = st.top();
        st.pop();
        //cout << "stack top: " << x << "\n";
        if(time1[x].first == -1){
            components++;
            reversedDfs(x, fake);
        }
    }
}

bool checkIfProper(){
    numberOfComponents.resize(n+1, 0);
    for(int i = 1; i <= n; i++){
        numberOfComponents[component[i]]++;
    }
    int ctrGreaterThan1=0;
    for(int i = 0; i <= n; i++){
        if(numberOfComponents[i] >= 1){
            ctrGreaterThan1++;
        }
    }
    if(ctrGreaterThan1 >= 2)return false;
    return true;
}

bool checkIfProper2(){
    numberOfComponents.resize(n+1, 0);
    for(int i = 1; i <= n; i++){
        numberOfComponents[component[i]]++;
    }
    int ctrGreaterThan1=0;
    for(int i = 0; i <= n; i++){
        if(numberOfComponents[i] >= 1){
            ctrGreaterThan1++;
        }
    }
    if(ctrGreaterThan1 >= 2)return false;
    return true;
}

vector<int>  parentOf;
vector<bool> visited;
bool foundCycle=false;

int globalParent;

void dfsCycle(int x, int parent){
    if(foundCycle)return;
    visited[x]=true;
    parentOf[x]=parent;
    for(int i = 0; i < v[x].size(); i++){
        if(!visited[v[x][i]]){
            dfsCycle(v[x][i], x);
        }
        else{
            if(v[x][i] == globalParent){
                foundCycle=true;
                parentOf[globalParent]=x;
                return;
            }
        }
    }
    if(foundCycle)return;
}

bool hasCycle2=false;

void dfsHasCycle(int x){
    visited[x]=true;
    if(hasCycle2)return;
    for(int i = 0; i < v[x].size(); i++){
        if(!visited[v[x][i]]){
            dfsHasCycle(v[x][i]);
        }
        else{
            hasCycle2=true;
        }
    }
}

bool hasCycles123(){
    for(int i = 0; i <= n; i++)visited[i]=false;
    for(int i = 1; i <= n; i++){
        if(!visited[i]){
            dfsHasCycle(i);
            if(hasCycle2)return true;
        }
    }

    return hasCycle2;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m;
    v.resize(n+1);
    reversed.resize(n+1);
    component.resize(n+1);
    time1.resize(n+1, make_pair(-1, -1));
    visited.resize(n+1);
    parentOf.resize(n+1);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        reversed[b].push_back(a);
    }
    if(!hasCycles123()){
        printf("NIE\n");
        return 0;
    }

    sss(-100);
    iniComponents = components;

    //cout << "ini: " << iniComponents << "\n";
    if(!checkIfProper()){
        printf("0\n");
        return 0;
    }
    //cout << "asd\n";
    int cycleParent=-1;
    for(int i = 0; i <= n; i++){
        visited[i]=false;
    }
    for(int i = 1; i <= n; i++){
        if(!foundCycle){
            if(numberOfComponents[component[i]] > 1){
                globalParent = i;
                dfsCycle(i, i);
                cycleParent = i;
                while(parentOf[cycleParent] != globalParent){
                    //cout << cycleParent << "\n";
                    vecCycle.push_back(cycleParent);
                    cycleParent = parentOf[cycleParent];
                }
            }
        }
    }
    vecCycle.push_back(cycleParent);

    for(int i = 0; i < vecCycle.size(); i++){
        sss(vecCycle[i]);
        if(components > iniComponents){
            result.push_back(vecCycle[i]);
        }
    }

    sort(result.begin(), result.end());
    cout << result.size() << "\n";
    for(int i = 0; i < result.size(); i++){
        cout << result[i] << " ";
    }
    cout << "\n";
}