Problem:
Bridges

Find bridges of an undirected graph G(V, E).

A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.

Input

|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1

, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target nodes of i-th edge (undirected).

Output

A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.

Constraints

1 ≤ |V| ≤ 100,000

0 ≤ |E| ≤ 100,000

The graph is connected

There are no parallel edges

There are no self-loops

Sample Input 1

4 4
0 1
0 2
1 2
2 3

Sample Output 1

2 3

Sample Input 2

5 4
0 1
1 2
2 3
3 4

Sample Output 2

0 1
1 2
2 3
3 4

Solution in C++14 (judge verdict: Accepted)

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = vector<Edge>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

class Graph {
  vector<Edges> g;

public:
  Graph(const int &size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int &i) const { return g[i]; }
  Edges &operator[](const int &i) { return g[i]; }
  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  auto begin() { return g.begin(); }
  auto end() { return g.end(); }
};

class TwoEdgeConnectedComponents {
public:
  Graph g;
  Edges bridges;
  vector<int> cmp;

  TwoEdgeConnectedComponents(const Graph &g) : g(g) {
    int n = g.size();
    cmp.resize(n);
    stack<int> s, roots;
    vector<int> ord(n, -1);
    int id = 0, k = 0;
    vector<int> onS(n), cmp(n);
    function<void(int, int)> dfs = [&](int v, int u) {
      ord[v] = id++;

      s.emplace(v);
      onS[v] = true;
      roots.emplace(v);
      for (auto &e : g[v]) {
        int w = e.dst;
        if (ord[w] == -1) {
          dfs(w, v);
        } else if (u != w && onS[w]) {
          while (ord[roots.top()] > ord[w]) roots.pop();
        }
      }
      if (v == roots.top()) {
        bridges.emplace_back(u, v, 0);
        while (true) {
          int w = s.top();
          s.pop();
          onS[w] = false;
          cmp[w] = k;
          if (v == w) break;
        }
        roots.pop();
        k++;
      }
    };

    rep(i, n) {
      if (ord[i] != -1) continue;
      dfs(i, n);
      bridges.pop_back();
    }
  }
};

main {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    g.addEdge(s, t);
  }
  TwoEdgeConnectedComponents tecc(g);
  vector<pair<int, int>> es;
  for (auto &e : tecc.bridges) es.emplace_back(e.src, e.dst);
  for (auto &e : es) {
    if (e.first > e.second) swap(e.first, e.second);
  }
  sort(all(es));
  for (auto &e : es) cout << e << endl;
}