#include <bits/stdc++.h> using namespace std; const int N=500005; vector <int> tab[N], wyn; priority_queue <int> pq; int t[N],T[N]; int tak; bool a; int main() { cout.sync_with_stdio(false); int n,m; cin >> n >> m; for(int i=0; i<m; i++) { int x,y; cin >> x >> y; tab[x].push_back(y); t[y]++; } for(int i=1; i<=n; i++) if(t[i]==0) pq.push(i); while(!pq.empty()) { //wyn.push_back(pq.top()); int z=pq.top(); pq.pop(); for(int i=0; i<tab[z].size(); i++) { t[tab[z][i]]--; if(t[tab[z][i]]==0) pq.push(tab[z][i]); } } for(int i=1;i<=n;i++) { if(t[i]==0)continue; while(!pq.empty())pq.pop(); for(int j=1;j<=n;j++)T[j]=t[j]; T[i]=0; pq.push(i); while(!pq.empty()) { //wyn.push_back(pq.top()); int z=pq.top(); //cout << z << endl; pq.pop(); for(int j=0; j<tab[z].size(); j++) { T[tab[z][j]]--; if(T[tab[z][j]]==0) pq.push(tab[z][j]); } } a=0; //cin >> a; for(int j=1;j<=n;j++)if(T[j]>0){a=1;break;} if(a==0){wyn.push_back(i); tak++;} } if(tak==0)cout << "NIE"; else { cout << tak << endl; for(int i=0;i<wyn.size();i++)cout << wyn[i] << " "; } return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int N=500005; vector <int> tab[N], wyn; priority_queue <int> pq; int t[N],T[N]; int tak; bool a; int main() { cout.sync_with_stdio(false); int n,m; cin >> n >> m; for(int i=0; i<m; i++) { int x,y; cin >> x >> y; tab[x].push_back(y); t[y]++; } for(int i=1; i<=n; i++) if(t[i]==0) pq.push(i); while(!pq.empty()) { //wyn.push_back(pq.top()); int z=pq.top(); pq.pop(); for(int i=0; i<tab[z].size(); i++) { t[tab[z][i]]--; if(t[tab[z][i]]==0) pq.push(tab[z][i]); } } for(int i=1;i<=n;i++) { if(t[i]==0)continue; while(!pq.empty())pq.pop(); for(int j=1;j<=n;j++)T[j]=t[j]; T[i]=0; pq.push(i); while(!pq.empty()) { //wyn.push_back(pq.top()); int z=pq.top(); //cout << z << endl; pq.pop(); for(int j=0; j<tab[z].size(); j++) { T[tab[z][j]]--; if(T[tab[z][j]]==0) pq.push(tab[z][j]); } } a=0; //cin >> a; for(int j=1;j<=n;j++)if(T[j]>0){a=1;break;} if(a==0){wyn.push_back(i); tak++;} } if(tak==0)cout << "NIE"; else { cout << tak << endl; for(int i=0;i<wyn.size();i++)cout << wyn[i] << " "; } return 0; } |