This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_4_A"
#include "../../template.h"
#include "../DfsTree/StronglyConnected.h"
void solve() {
int n, m; cin >> n >> m;
vector<vector<int>> g(n);
while (m--) {
int u, v; cin >> u >> v;
g[u].push_back(v);
}
DirectedDfs tree(g);
for (auto comp : tree.scc) {
if (comp.size() > 1) {
cout << 1 << endl;
return;
}
}
cout << 0 << endl;
}
#line 1 "Graph/tests/aizu_grl_4_a_strongly_connected_cycle_check.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_4_A"
#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
out << '{';
for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
out << (it == beg ? "" : ", ") << *it;
}
return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
return print_tuple_utils<0, tuple<U...>>(out, t);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
return uniform_int_distribution<long long> (0, r-1)(rng);
}
template<typename T>
vector<T> read_vector(int n) {
vector<T> res(n);
for (int& x : res) cin >> x;
return res;
}
void solve();
int main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
#line 1 "Graph/DfsTree/StronglyConnected.h"
// Index from 0
// Usage:
// DirectedDfs tree;
// Now you can use tree.scc
//
// Note: reverse(tree.scc) is topo sorted
//
// Tested:
// - (requires scc to be topo sorted) https://judge.yosupo.jp/problem/scc
// - https://cses.fi/problemset/task/1686/
// - (edges have costs) https://oj.vnoi.info/problem/bedao_g16_b
struct DirectedDfs {
vector<vector<int>> g;
int n;
vector<int> num, low, current, S;
int counter;
vector<int> comp_ids;
vector< vector<int> > scc;
DirectedDfs(const vector<vector<int>>& _g) : g(_g), n(g.size()),
num(n, -1), low(n, 0), current(n, 0), counter(0), comp_ids(n, -1) {
for (int i = 0; i < n; i++) {
if (num[i] == -1) dfs(i);
}
}
void dfs(int u) {
low[u] = num[u] = counter++;
S.push_back(u);
current[u] = 1;
for (auto v : g[u]) {
if (num[v] == -1) dfs(v);
if (current[v]) low[u] = min(low[u], low[v]);
}
if (low[u] == num[u]) {
scc.push_back(vector<int>());
while (1) {
int v = S.back(); S.pop_back(); current[v] = 0;
scc.back().push_back(v);
comp_ids[v] = ((int) scc.size()) - 1;
if (u == v) break;
}
}
}
// build DAG of strongly connected components
// Returns: adjacency list of DAG
std::vector<std::vector<int>> build_scc_dag() {
std::vector<std::vector<int>> dag(scc.size());
for (int u = 0; u < n; u++) {
int x = comp_ids[u];
for (int v : g[u]) {
int y = comp_ids[v];
if (x != y) {
dag[x].push_back(y);
}
}
}
return dag;
}
};
#line 5 "Graph/tests/aizu_grl_4_a_strongly_connected_cycle_check.test.cpp"
void solve() {
int n, m; cin >> n >> m;
vector<vector<int>> g(n);
while (m--) {
int u, v; cin >> u >> v;
g[u].push_back(v);
}
DirectedDfs tree(g);
for (auto comp : tree.scc) {
if (comp.size() > 1) {
cout << 1 << endl;
return;
}
}
cout << 0 << endl;
}