# include using namespace std; using ll = long long; using ull = unsigned long long; const double pi = acos(-1); templateconstexpr T inf() { return ::std::numeric_limits::max(); } templateconstexpr T hinf() { return inf() / 2; } template T_char TL(T_char cX) { return tolower(cX); } template T_char TU(T_char cX) { return toupper(cX); } template bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; } template bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; } template bool is_sqare(T a) { if(floor(sqrt(a)) * floor(sqrt(a)) == a){ return true; }return false; } int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; } int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; } int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; } ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); }; ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; }; template using dijk = priority_queue, greater>; # define all(qpqpq) (qpqpq).begin(),(qpqpq).end() # define UNIQUE(wpwpw) sort(all((wpwpw)));(wpwpw).erase(unique(all((wpwpw))),(wpwpw).end()) # define LOWER(epepe) transform(all((epepe)),(epepe).begin(),TL) # define UPPER(rprpr) transform(all((rprpr)),(rprpr).begin(),TU) # define rep(i,upupu) for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++) # define reps(i,opopo) for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++) # define len(x) ((int)(x).size()) # define bit(n) (1LL << (n)) # define pb push_back #ifdef LOCAL # include "_debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif struct INIT{ INIT(){ std::ios::sync_with_stdio(false); std::cin.tie(0); cout << fixed << setprecision(20); } }INIT; class FindingAllCycles { private: struct node { int prev, next, to; node(const int _prev, const int _next, const int _to) : prev(_prev), next(_next), to(_to){} }; const int V; vector > G; vector > block_stack; stack st; bool *fix, *blocked, *used; bool enumerate; void erase_edge(const int u, const int id){ G[u][G[u][id].next].prev = G[u][id].prev; G[u][G[u][id].prev].next = G[u][id].next; } void scc_dfs(const int u, int& tm, int& cnt, vector& ord, vector& low, vector& cmp, stack& st){ ord[u] = low[u] = tm++, st.push(u); for(int id = G[u][0].next; id != 0; id = G[u][id].next){ const int v = G[u][id].to; if(ord[v] < 0){ scc_dfs(v, tm, cnt, ord, low, cmp, st); low[u] = min(low[u], low[v]); }else if(cmp[v] < 0){ low[u] = min(low[u], ord[v]); } if(cmp[v] >= 0) erase_edge(u, id); } if(ord[u] == low[u]){ while(true){ const int v = st.top(); st.pop(), cmp[v] = cnt; if(v == u) break; } ++cnt; } } void scc(){ vector ord(V, -1), low(V), cmp(V, -1); stack st; int tm = 0, cnt = 0; for(int i = 0; i < V; ++i){ if(ord[i] < 0) scc_dfs(i, tm, cnt, ord, low, cmp, st); } } void unblock(const int u){ blocked[u] = false; while(!block_stack[u].empty()){ const int v = block_stack[u].top(); block_stack[u].pop(); if(blocked[v]) unblock(v); } } bool dfs(const int u, const int s, vector& path, vector& ver_list){ bool flag = false; path.push_back(u); blocked[u] = true; if(!used[u]) used[u] = true, ver_list.push_back(u); for(int id = G[u][0].next; id != 0; id = G[u][id].next){ const int v = G[u][id].to; if(fix[v]){ erase_edge(u, id); continue; } if(v == s){ if(enumerate) ans.push_back(path); ++count, flag = true; }else if(!blocked[v]){ if(dfs(v, s, path, ver_list)) flag = true; } } if(flag) unblock(u); else{ for(int id = G[u][0].next; id != 0; id = G[u][id].next){ block_stack[G[u][id].to].push(u); } } path.pop_back(); return flag; } public: vector> ans; int count; FindingAllCycles(const int node_size) : V(node_size), G(V, vector{{0, 0, -1}}), block_stack(V), fix(new bool[V]()), blocked(new bool[V]()), used(new bool[V]()), count(0){} ~FindingAllCycles(){ delete[] fix, delete[] blocked, delete[] used; } void add_edge(const int u, const int v){ if(u == v){ ans.push_back({u}); return; } G[u][0].prev = G[u].back().next = (int)G[u].size(); G[u].push_back((node){(int)G[u].size() - 1, 0, v}); } int solve(bool _enumerate=true){ scc(); enumerate = _enumerate; for(int i = 0; i < V; ++i){ vector path, ver_list; dfs(i, i, path, ver_list); fix[i] = true; for(int j : ver_list){ used[j] = blocked[j] = false, block_stack[j] = stack(); } } return count; } }; void solve(){ int n, m; cin >> n >> m; vector> g(n); vector u(m), v(m); FindingAllCycles fc(n); rep(i, m){ cin >> u[i] >> v[i]; u[i]--, v[i]--; g[u[i]].push_back(v[i]); fc.add_edge(u[i], v[i]); } fc.solve(true); set ban_v; set> ban; rep(i, len(fc.ans)){ debug(fc.ans[i]); bool flg = false; for(auto x : fc.ans[i]){ if(ban_v.count(x))flg = true; } if(flg)continue; rep(j, len(fc.ans[i])){ ban.insert({fc.ans[i][j], fc.ans[i][(j + 1) % len(fc.ans)]}); ban_v.insert(fc.ans[i][j]); } } rep(i, m){ if(ban.count({u[i], v[i]}))continue; cout << u[i] + 1 << " " << v[i] + 1 << endl; } } int main(){ int t = 1; //cin >> t; while(t--)solve(); }