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

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

using ll = long long;
constexpr int INF = 1e9;
constexpr ll INFLL = 1e18;
constexpr double eps = 1e-10;

constexpr ll MOD = 1e9 + 7;

struct edge {
  int from, to;
};

using edges = std::vector<edge>;
using graph = std::vector<edges>;

void add_edge(graph &g, int from, int to) {
  g[from].push_back((edge){from, to});
}

int scc(graph &G, std::vector<int> &cmp) {
  int V = G.size();
  std::vector<std::vector<int>> g(V), rg(V);
  std::vector<bool> used(V, false);
  std::vector<int> vs;
  cmp.resize(V);
  for (int i = 0; i < V; ++i) {
    for (auto e : G[i]) {
      g[i].push_back(e.to);
      rg[e.to].push_back(i);
    }
  }
  std::function<void(int)> dfs = [&g, &vs, &used, &dfs](int v) {
    used[v] = true;
    for (auto i : g[v]) {
      if (!used[i]) {
        dfs(i);
      }
    }
    vs.push_back(v);
  };
  std::function<void(int, int)> rdfs = [&rg, &cmp, &used, &rdfs](int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (int i : rg[v]) {
      if (!used[i]) {
        rdfs(i, k);
      }
    }
  };
  for (int v = 0; v < V; ++v) {
    if (!used[v]) {
      dfs(v);
    }
  }
  std::fill(used.begin(), used.end(), false);
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; --i) {
    if (!used[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int V, E;
  cin >> V >> E;
  graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    add_edge(g, s, t);
  }
  vector<int> cmp;
  int K = scc(g, cmp);
  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << (cmp[u] == cmp[v]) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PLDLD;
typedef vector<int> VI;
typedef vector<char> VB;

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define CLR(a) memset((a), 0, sizeof(a))
#define ALL(a) a.begin(), a.end()

const LD eps = 1e-5;
// const long long INF=(LL)(1e9)*(LL)(1e9);
const int INF = 1e9;

template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const LL pow(const LL p, const LL q) {
  LL t = 1;
  REP(i, q)
  t *= p;
  return t;
}

struct Edge {
  int to, from, cost;
  Edge(int to_, int from_, int weight_) : to(to_), from(from_), cost(weight_) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

const Graph reverse_edge_Graph(const Graph &g) {
  Graph rg(g.size());
  for (auto &&es : g)
    for (auto &&e : es)
      rg[e.to].push_back(Edge(e.from, e.to, e.cost));
  return rg;
}

const tuple<vector<int>, vector<vector<int>>>
SCC(const Graph &g) // StronglyConnectedComponent
{
  const int n = g.size();
  Graph rg(reverse_edge_Graph(g));
  vector<int> vs;
  vector<char> reach(n); // vector<bool>
  vector<vector<int>> scc;
  vector<int> scc_vectormap(n);

  function<void(const int)> dfs = [&](const int v) {
    reach[v] = 1;
    for (auto &&e : g[v])
      if (!reach[e.to])
        dfs(e.to);
    vs.push_back(v);
  };
  function<void(const int, const int)> rdfs = [&](const int v, const int k) {
    reach[v] = 1;
    scc[k].push_back(v);
    scc_vectormap[v] = k;
    for (auto &&e : rg[v])
      if (!reach[e.to])
        rdfs(e.to, k);
  };

  for (int i = 0; i < n; i++)
    if (!reach[i])
      dfs(i);
  fill(reach.begin(), reach.end(), 0);
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--)
    if (!reach[vs[i]]) {
      scc.push_back(vector<int>());
      rdfs(vs[i], k++);
    }
  return tuple<vector<int>, vector<vector<int>>>(scc_vectormap, scc);
}

const Graph DAG_from_SCC(int DAG_size, Graph &g, vector<int> &scc_vectormap) {
  Graph dag(DAG_size);
  for (auto &&es : g)
    for (auto &&e : es) {
      if (scc_vectormap[e.from] == scc_vectormap[e.to])
        continue;
      dag[scc_vectormap[e.from]].push_back(
          Edge(scc_vectormap[e.to], scc_vectormap[e.from], 0));
    }
  return dag;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int v, e;
  cin >> v >> e;
  Graph g(e);
  REP(i, e) {
    int s, t;
    cin >> s >> t;
    g[s].push_back(Edge(t, s, 0));
  }
  VI mp;
  vector<VI> hoge;
  tie(mp, hoge) = SCC(g);
  int q;
  cin >> q;
  REP(i, q) {
    int u, v;
    cin >> u >> v;
    cout << (mp[u] == mp[v] ? 1 : 0) << endl;
  }
}