#include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<iostream> #include<string> using namespace std; #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> int n, m, in[500005]; vector<int> V[500005], R; queue<int> Q; bool topo(int ind) { int x=0; if(ind!=0) x++; for(int i=1; i<=n; i++) { if(i!=ind && in[i]==0) { Q.push(i); x++; } } while(!Q.empty()) { int v=Q.front(); Q.pop(); for(int i=0; i<(int)V[v].size(); i++) { in[V[v][i]]--; if(V[v][i]!=ind && in[V[v][i]]==0) { Q.push(V[v][i]); x++; } } } return x==n; } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for(int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); V[a].PB(b); in[b]++; } if(topo(0)) { printf("NIE\n"); return 0; } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) in[j]=0; for(int k=1; k<=n; k++) if(k!=i) for(int j=0; j<(int)V[k].size(); j++) in[V[k][j]]++; if(topo(i)) { R.PB(i); //printf("wtf %d\n", i); } } printf("%d\n", (int)R.size()); for(int i=0; i<(int)R.size(); i++) printf("%d ", R[i]); if(!R.empty()) printf("\n"); 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<iostream> #include<string> using namespace std; #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> int n, m, in[500005]; vector<int> V[500005], R; queue<int> Q; bool topo(int ind) { int x=0; if(ind!=0) x++; for(int i=1; i<=n; i++) { if(i!=ind && in[i]==0) { Q.push(i); x++; } } while(!Q.empty()) { int v=Q.front(); Q.pop(); for(int i=0; i<(int)V[v].size(); i++) { in[V[v][i]]--; if(V[v][i]!=ind && in[V[v][i]]==0) { Q.push(V[v][i]); x++; } } } return x==n; } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for(int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); V[a].PB(b); in[b]++; } if(topo(0)) { printf("NIE\n"); return 0; } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) in[j]=0; for(int k=1; k<=n; k++) if(k!=i) for(int j=0; j<(int)V[k].size(); j++) in[V[k][j]]++; if(topo(i)) { R.PB(i); //printf("wtf %d\n", i); } } printf("%d\n", (int)R.size()); for(int i=0; i<(int)R.size(); i++) printf("%d ", R[i]); if(!R.empty()) printf("\n"); return 0; } |