#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; vector <int> A[500005]; bool visited[500005]; bool flag = false; bool dfs( int v, int block ) { visited[v] = true; for( vector <int> :: iterator it = A[v].begin(); it != A[v].end(); it ++ ) { if( *it == block ) continue; if( visited[*it] == false ) { dfs(*it,block); } else { flag = true; break; } } return flag; } void clean( int n ) { for( int i = 1; i <= n; i ++ ) { visited[i] = false; } } int main() { int n, m; scanf("%d %d", &n, &m); for( int i = 0; i < m; i ++ ) { int a, b; scanf("%d %d", &a, &b); A[a].push_back(b); } bool g = false; for( int i = 1; i < n; i ++ ) { flag = false; clean(n); if( dfs(i,0) == true ) { g = true; break; } } if( g == false ) { printf("NIE\n"); return 0; } //if( dfs(1,2) == true ) cout << "jest cykl\n"; //else cout << "ni ma\n"; vector <int> L; for( int i = 1; i <= n; i ++ ) { bool K = true; for( int j = 1; j <= n; j ++ ) { flag = false; clean(n+1); visited[i] = true; if( j != i && visited[j] == false ) { bool f = dfs(j,i); if( f == true ) { K = false; break; } } } if( K == true ) L.push_back(i); } if( L.size() == 0 ) { printf("NIE\n"); return 0; } sort(L.begin(),L.end()); printf("%d\n", L.size()); for( int i = 0; i < L.size(); i ++ ) { printf("%d ", L[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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; vector <int> A[500005]; bool visited[500005]; bool flag = false; bool dfs( int v, int block ) { visited[v] = true; for( vector <int> :: iterator it = A[v].begin(); it != A[v].end(); it ++ ) { if( *it == block ) continue; if( visited[*it] == false ) { dfs(*it,block); } else { flag = true; break; } } return flag; } void clean( int n ) { for( int i = 1; i <= n; i ++ ) { visited[i] = false; } } int main() { int n, m; scanf("%d %d", &n, &m); for( int i = 0; i < m; i ++ ) { int a, b; scanf("%d %d", &a, &b); A[a].push_back(b); } bool g = false; for( int i = 1; i < n; i ++ ) { flag = false; clean(n); if( dfs(i,0) == true ) { g = true; break; } } if( g == false ) { printf("NIE\n"); return 0; } //if( dfs(1,2) == true ) cout << "jest cykl\n"; //else cout << "ni ma\n"; vector <int> L; for( int i = 1; i <= n; i ++ ) { bool K = true; for( int j = 1; j <= n; j ++ ) { flag = false; clean(n+1); visited[i] = true; if( j != i && visited[j] == false ) { bool f = dfs(j,i); if( f == true ) { K = false; break; } } } if( K == true ) L.push_back(i); } if( L.size() == 0 ) { printf("NIE\n"); return 0; } sort(L.begin(),L.end()); printf("%d\n", L.size()); for( int i = 0; i < L.size(); i ++ ) { printf("%d ", L[i]); } return 0; } |