#include <iostream> #include <vector> #include <set> #include <algorithm> #include <stdlib.h> using namespace std; struct vertex { int visitedId; int onCycleId; bool isInResult; bool isOnStack; set< int > links; vertex() : visitedId( -10000 ), onCycleId( -10000 ), isInResult( true ), isOnStack( false ) {} }; vector< vertex > verts; int excludedId; vector< int > stack; bool find_cycle( const int id ) { vertex& v = verts[ id ]; if ( v.visitedId == excludedId ) { if ( excludedId < 0 && v.isOnStack ) { int curr = stack.size() - 1; while ( 1 ) { verts[ stack[ curr ] ].onCycleId = excludedId; if ( stack[ curr ] == id ) { break; } --curr; } const int n = verts.size(); for ( int i = 0; i < n; ++i ) { if ( verts[ i ].onCycleId != excludedId ) { verts[ i ].isInResult = false; } } } return v.isOnStack; } v.visitedId = excludedId; v.isOnStack = true; if ( excludedId < 0 ) { stack.push_back( id ); } for ( set< int >::iterator it = v.links.begin(); it != v.links.end(); ++it ) { const int linkId = *it; const vertex& link = verts[ linkId ]; if ( linkId == excludedId ) { continue; } if ( find_cycle( linkId ) ) { v.isOnStack = false; if ( excludedId < 0 ) { stack.pop_back(); } return true; } } v.isOnStack = false; if ( excludedId < 0 ) { stack.pop_back(); } return false; } int main() { int n, m; cin >> n >> m; verts.resize( n ); for ( int i = 0; i < m; ++i ) { int a, b; cin >> a >> b; --a; --b; verts[ a ].links.insert( b ); } // Find any cycle const int steps = 10; for ( int i = 0; i < steps; ++i ) { excludedId = -( i + 1 ); if ( !find_cycle( i * n / steps ) ) { cout << "NIE"; return 0; } } // Brute-force for ( int i = 0; i < n; ++i ) { if ( !verts[ i ].isInResult ) { continue; } excludedId = i; for ( int j = 0; j < n; ++j ) { if ( i != j && find_cycle( j ) ) { verts[ i ].isInResult = false; break; } } } int count = 0; for ( int i = 0; i < n; ++i ) { if ( verts[ i ].isInResult ) { ++count; } } cout << count << endl; for ( int i = 0; i < n; ++i ) { if ( verts[ i ].isInResult ) { cout << ( i + 1 ) << " "; } } 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 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 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <stdlib.h> using namespace std; struct vertex { int visitedId; int onCycleId; bool isInResult; bool isOnStack; set< int > links; vertex() : visitedId( -10000 ), onCycleId( -10000 ), isInResult( true ), isOnStack( false ) {} }; vector< vertex > verts; int excludedId; vector< int > stack; bool find_cycle( const int id ) { vertex& v = verts[ id ]; if ( v.visitedId == excludedId ) { if ( excludedId < 0 && v.isOnStack ) { int curr = stack.size() - 1; while ( 1 ) { verts[ stack[ curr ] ].onCycleId = excludedId; if ( stack[ curr ] == id ) { break; } --curr; } const int n = verts.size(); for ( int i = 0; i < n; ++i ) { if ( verts[ i ].onCycleId != excludedId ) { verts[ i ].isInResult = false; } } } return v.isOnStack; } v.visitedId = excludedId; v.isOnStack = true; if ( excludedId < 0 ) { stack.push_back( id ); } for ( set< int >::iterator it = v.links.begin(); it != v.links.end(); ++it ) { const int linkId = *it; const vertex& link = verts[ linkId ]; if ( linkId == excludedId ) { continue; } if ( find_cycle( linkId ) ) { v.isOnStack = false; if ( excludedId < 0 ) { stack.pop_back(); } return true; } } v.isOnStack = false; if ( excludedId < 0 ) { stack.pop_back(); } return false; } int main() { int n, m; cin >> n >> m; verts.resize( n ); for ( int i = 0; i < m; ++i ) { int a, b; cin >> a >> b; --a; --b; verts[ a ].links.insert( b ); } // Find any cycle const int steps = 10; for ( int i = 0; i < steps; ++i ) { excludedId = -( i + 1 ); if ( !find_cycle( i * n / steps ) ) { cout << "NIE"; return 0; } } // Brute-force for ( int i = 0; i < n; ++i ) { if ( !verts[ i ].isInResult ) { continue; } excludedId = i; for ( int j = 0; j < n; ++j ) { if ( i != j && find_cycle( j ) ) { verts[ i ].isInResult = false; break; } } } int count = 0; for ( int i = 0; i < n; ++i ) { if ( verts[ i ].isInResult ) { ++count; } } cout << count << endl; for ( int i = 0; i < n; ++i ) { if ( verts[ i ].isInResult ) { cout << ( i + 1 ) << " "; } } return 0; } |