#include #include #include using std::vector; using std::stack; int acyclic(vector > &adj) { vector marked(adj.size()); vector visited(adj.size()); stack st; for (int i = 0; i < adj.size(); i++) { if (marked[i]) continue; st.push(i); while(!st.empty()) { int v = st.top(); visited[v] = true; int s = adj[v].size(); for (int j = 0; j < s; j++) { int w = adj[v][j]; if (!marked[w]) { if (visited[w]) return 1; st.push(w); } } if (st.top() == v) { visited[v] = false; marked[v] = true; st.pop(); } } } return 0; } int main() { size_t n, m; std::cin >> n >> m; vector > adj(n, vector()); for (size_t i = 0; i < m; i++) { int x, y; std::cin >> x >> y; adj[x - 1].push_back(y - 1); } std::cout << acyclic(adj) << '\n'; }