#include <bits/stdc++.h>
#include <string>
using namespace std;
template<typename T>
inline ostream& operator<< (ostream& out, const vector<T>& data) {
out << data[0];
for( auto it = data.begin()+1; it != data.end(); it++ )
out << ' ' << *it;
return out;
}
template<typename T>
inline istream& operator>> (istream& in, vector<T>& data) {
for( auto &v : data )
in >> v;
return in;
}
template<typename A, typename B>
inline ostream& operator<< (ostream& out, const pair<A, B>& data) {
return out << data.first << ' ' << data.second;
}
template<typename T>
class vector_from_one : public vector<T> {
public:
using vector<T>::vector;
T& operator[] (size_t n) { return vector<T>::operator[](n-1); }
const T& operator[] (size_t n) const { return vector<T>::operator[](n-1); }
T& at (size_t n) { return vector<T>::at(n-1); }
const T& at (size_t n) const { return vector<T>::at(n-1); }
};
struct vertex_t : public vector<vertex_t*> {
bool visited = false;
int visits = 0;
int active = 0;
int value;
};
void test() {
int n, m;
cin >> n >> m;
vector_from_one<vertex_t> V(n);
while( m --> 0 ) {
int a, b;
cin >> a >> b;
V[a].push_back(&V[b]);
}
stack<vertex_t*> Q;
int cycles = 0, active = 0;
for( auto &start : V ) {
if( not start.visited ) {
Q.push(&start);
start.visits++;
}
while( not Q.empty() ) {
vertex_t *v = Q.top();
if( v->visited ) {
Q.pop();
v->value = active;
v->visits--;
if( v->visits == 0 ) {
active -= v->active;
} else {
cycles++;
active++;
v->active++;
}
} else {
v->visited = true;
for( auto u : *v ) {
Q.push(u);
u->visits++;
}
}
}
}
if( cycles == 0 )
cout << "NIE\n";
else{
int answer = 0;
for( auto &v : V )
if( v.value == cycles )
answer++;
cout << answer << '\n';
if( answer ) {
for( int i = 1; i <= n; i++ )
if( V[i].value == cycles )
cout << i << ' ';
cout << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
//cin >> T;
while( T --> 0 )
test();
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 | #include <bits/stdc++.h> #include <string> using namespace std; template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& data) { out << data[0]; for( auto it = data.begin()+1; it != data.end(); it++ ) out << ' ' << *it; return out; } template<typename T> inline istream& operator>> (istream& in, vector<T>& data) { for( auto &v : data ) in >> v; return in; } template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& data) { return out << data.first << ' ' << data.second; } template<typename T> class vector_from_one : public vector<T> { public: using vector<T>::vector; T& operator[] (size_t n) { return vector<T>::operator[](n-1); } const T& operator[] (size_t n) const { return vector<T>::operator[](n-1); } T& at (size_t n) { return vector<T>::at(n-1); } const T& at (size_t n) const { return vector<T>::at(n-1); } }; struct vertex_t : public vector<vertex_t*> { bool visited = false; int visits = 0; int active = 0; int value; }; void test() { int n, m; cin >> n >> m; vector_from_one<vertex_t> V(n); while( m --> 0 ) { int a, b; cin >> a >> b; V[a].push_back(&V[b]); } stack<vertex_t*> Q; int cycles = 0, active = 0; for( auto &start : V ) { if( not start.visited ) { Q.push(&start); start.visits++; } while( not Q.empty() ) { vertex_t *v = Q.top(); if( v->visited ) { Q.pop(); v->value = active; v->visits--; if( v->visits == 0 ) { active -= v->active; } else { cycles++; active++; v->active++; } } else { v->visited = true; for( auto u : *v ) { Q.push(u); u->visits++; } } } } if( cycles == 0 ) cout << "NIE\n"; else{ int answer = 0; for( auto &v : V ) if( v.value == cycles ) answer++; cout << answer << '\n'; if( answer ) { for( int i = 1; i <= n; i++ ) if( V[i].value == cycles ) cout << i << ' '; cout << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; //cin >> T; while( T --> 0 ) test(); return 0; } |
English