Problem p02367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;
typedef vector<int> vi;
typedef vector<vi> vvi;

int dfs(vvi &g, vector<pi> &res, int v, int &count, int from, vi &low,
        vi &pre) {
  pre[v] = count++;
  low[v] = pre[v];
  for (vi::iterator it = g[v].begin(); it != g[v].end(); it++) {
    int to = *it;
    if (pre[to] == -1) {

      low[v] = min(low[v], dfs(g, res, to, count, v, low, pre));
      if (low[to] == pre[to]) {
        res.push_back(make_pair(min(to, v), max(to, v)));
      }
    } else {
      if (from == to) {
        continue;
      }
      low[v] = min(low[v], low[to]);
    }
  }
  return low[v];
}

// g: adjacency list
// v:number of nodes

vector<pi> bridges(vvi &g, int v) {
  vector<pi> res;
  if (v > 0) {

    vi low(v, -1);
    vi pre(v, -1);
    int count = 0;
    dfs(g, res, 0, count, -1, low, pre);
  }
  return res;
}

vvi G;
vector<pi> ans;

int main() {
  int v, e;
  cin >> v >> e;
  G.resize(v);

  rep(i, e) {
    int s, t;
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  ans = bridges(G, v);
  sort(ans.begin(), ans.end());

  rep(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;
typedef vector<int> vi;
typedef vector<vi> vvi;

// res: bridges
// v: current node
// from: parent node

int dfs(vvi &g, vector<pi> &res, int v, int &count, int from, vi &low,
        vi &pre) {
  pre[v] = count++;
  low[v] = pre[v];
  for (vi::iterator it = g[v].begin(); it != g[v].end(); it++) {
    int to = *it;
    if (pre[to] == -1) {
      // destination has not been visited
      // visit destination and update low[v]
      low[v] = min(low[v], dfs(g, res, to, count, v, low, pre));
      if (low[to] == pre[to]) {
        // edge is not contained in a closed path -> bridge
        res.push_back(make_pair(v, to));
      }
    } else {
      if (from == to) {
        // ignore a path to parent
        continue;
      }
      low[v] = min(low[v], low[to]);
    }
  }
  return low[v];
}

// g: adjacency list
// v:number of nodes

vector<pi> bridges(vvi &g, int v) {
  vector<pi> res;
  if (v > 0) {
    // assume at least the first vertex exists
    vi low(v, -1);                       // lowest reacheable index
    vi pre(v, -1);                       // pre-order index
    int count = 0;                       // pre-order index counter
    dfs(g, res, 0, count, -1, low, pre); // start dfs from vetex 0
  }
  return res;
}

vvi G;
vector<pi> ans;

int main() {
  int v, e;
  cin >> v >> e;
  G.resize(v);

  rep(i, e) {
    int s, t;
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  ans = bridges(G, v);
  sort(ans.begin(), ans.end());

  for (int i = 0; i < ans.size() - 1; i++) {
    for (int j = i + 1; j < ans.size(); j++) {
      if (ans[i].first == ans[j].first) {
        if (ans[i].second > ans[j].second)
          swap(ans[i], ans[j]);
      }
    }
  }

  rep(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}