#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 500007;
int n, m;
vector <int> G[N];
vector <int> res;
vector <int> incip;
int stg[N];
int st[N];
bool check(int v) {
for(int i = 1; i <= n; ++i) st[i] = stg[i];
st[v] = 0;
vector <int> q;
if(stg[v] != 0) q.push_back(v);
for(int i = 0; i < incip.size(); ++i) q.push_back(incip[i]);
for(int i = 0; i < q.size(); ++i) {
int u = q[i];
for(int j = 0; j < G[u].size(); ++j) {
st[G[u][j]]--;
if(st[G[u][j]] == 0) q.push_back(G[u][j]);
}
}
int siz = q.size();
if(v == 0) siz--;
return siz == n;
}
int main() {
ios_base::sync_with_stdio(0);
stg[0] = 315;
cin >> n >> m;
for(int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
stg[b]++;
}
for(int i = 1; i <= n; ++i) if(stg[i] == 0) incip.push_back(i);
if(check(0)) {
cout << "NIE\n";
return 0;
}
for(int i = 1; i <= n; ++i) if(check(i)) res.push_back(i);
sort(res.begin(), res.end());
cout << res.size() << endl;
for(int i = 0; i < res.size(); ++i) cout << res[i] << ' ';
//system("pause");
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 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int N = 500007; int n, m; vector <int> G[N]; vector <int> res; vector <int> incip; int stg[N]; int st[N]; bool check(int v) { for(int i = 1; i <= n; ++i) st[i] = stg[i]; st[v] = 0; vector <int> q; if(stg[v] != 0) q.push_back(v); for(int i = 0; i < incip.size(); ++i) q.push_back(incip[i]); for(int i = 0; i < q.size(); ++i) { int u = q[i]; for(int j = 0; j < G[u].size(); ++j) { st[G[u][j]]--; if(st[G[u][j]] == 0) q.push_back(G[u][j]); } } int siz = q.size(); if(v == 0) siz--; return siz == n; } int main() { ios_base::sync_with_stdio(0); stg[0] = 315; cin >> n >> m; for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); stg[b]++; } for(int i = 1; i <= n; ++i) if(stg[i] == 0) incip.push_back(i); if(check(0)) { cout << "NIE\n"; return 0; } for(int i = 1; i <= n; ++i) if(check(i)) res.push_back(i); sort(res.begin(), res.end()); cout << res.size() << endl; for(int i = 0; i < res.size(); ++i) cout << res[i] << ' '; //system("pause"); return 0; } |
English