Problem p02367 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

struct Edge {
  int src;
  int dst;
  int id;
};

// res: bridges
// v: current node
// from: parent node
int dfs2(vector<vector<Edge>> &g, vector<int> &res, int v, int &count,
         int from_id, vector<int> &low, vector<int> &pre) {
  pre[v] = count++;
  low[v] = pre[v];
  for (auto it = g[v].begin(); it != g[v].end(); it++) {
    int to = it->dst;
    if (pre[to] == -1) {
      // destination has not been visited
      // visit destination and update low[v]
      low[v] = min(low[v], dfs2(g, res, to, count, it->id, low, pre));
      if (low[to] == pre[to]) {
        // edge is not contained in a closed path -> bridge
        // res.push_back(it->id);
        res[it->id] = true;
      }
    } else {
      if (from_id == it->id) {
        // ignore a path to parent
        continue;
      }
      low[v] = min(low[v], low[to]);
    }
  }
  return low[v];
}

// Calculate bridges in a undirected graph.
// Assume graph is connected and has no parallel edges or self-loops.
// g: adjacency list
// V: number of nodes
void bridges(vector<vector<Edge>> &g, int V, vector<int> &res) {

  if (V > 0) {
    // assume at least the first vertex exists
    vector<int> low(V, -1);               // lowest reacheable index
    vector<int> pre(V, -1);               // pre-order index
    int count = 0;                        // pre-order index counter
    dfs2(g, res, 0, count, -1, low, pre); // start dfs from vertex 0
  }
}

int main() {
  int V, E;
  cin >> V >> E;
  vector<vector<Edge>> es(V);
  vector<Edge> eids;
  for (int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    if (s > t)
      swap(s, t);
    es[s].push_back(Edge{s, t, i});
    es[t].push_back(Edge{t, s, i});
    eids.push_back(Edge{s, t, i});
  }
  vector<int> res(E);
  bridges(es, V, res);
  vector<Edge> bris;
  for (int i = 0; i < E; ++i) {
    if (res[i])
      bris.push_back(eids[i]);
  }
  sort(bris.begin(), bris.end(), [=](const Edge &l, const Edge &r) {
    return l.src == r.src ? l.dst < r.dst : l.src < r.src;
  });
  for (int i = 0; i < bris.size(); ++i) {
    cout << bris[i].src << " " << bris[i].dst << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

struct Edge {
  int src;
  int dst;
  int id;
};

// res: bridges
// v: current node
// from: parent node
int dfs2(vector<vector<Edge>> &g, vector<int> &res, int v, int &count,
         int from_id, vector<int> &low, vector<int> &pre) {
  pre[v] = count++;
  low[v] = pre[v];
  for (auto it = g[v].begin(); it != g[v].end(); it++) {
    int to = it->dst;
    if (pre[to] == -1) {
      // destination has not been visited
      // visit destination and update low[v]
      low[v] = min(low[v], dfs2(g, res, to, count, it->id, low, pre));
      if (low[to] == pre[to]) {
        // edge is not contained in a closed path -> bridge
        // res.push_back(it->id);
        res[it->id] = true;
      }
    } else {
      if (from_id == it->id) {
        // ignore a path to parent
        continue;
      }
      low[v] = min(low[v], low[to]);
    }
  }
  return low[v];
}

// Calculate bridges in a undirected graph.
// Assume graph is connected and has no parallel edges or self-loops.
// g: adjacency list
// V: number of nodes
void bridges(vector<vector<Edge>> &g, int V, vector<int> &res) {

  if (V > 0) {
    // assume at least the first vertex exists
    vector<int> low(V, -1);               // lowest reacheable index
    vector<int> pre(V, -1);               // pre-order index
    int count = 0;                        // pre-order index counter
    dfs2(g, res, 0, count, -1, low, pre); // start dfs from vertex 0
  }
}

int main() {
  int V, E;
  cin >> V >> E;
  vector<vector<Edge>> es(V);
  vector<Edge> eids;
  for (int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    if (s > t)
      swap(s, t);
    es[s].push_back(Edge{s, t, i});
    es[t].push_back(Edge{t, s, i});
    eids.push_back(Edge{s, t, i});
  }
  vector<int> res(E);
  bridges(es, V, res);
  vector<Edge> bris;
  for (int i = 0; i < V; ++i) {
    if (res[i])
      bris.push_back(eids[i]);
  }
  sort(bris.begin(), bris.end(), [=](const Edge &l, const Edge &r) {
    return l.src == r.src ? l.dst < r.dst : l.src < r.src;
  });
  for (int i = 0; i < bris.size(); ++i) {
    cout << bris[i].src << " " << bris[i].dst << endl;
  }
  return 0;
}