Problem p02368 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, b) for (int i = 0; i < int(b); i++)
#define eb emplace_back

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src_, int dst_, Weight weight_)
      : src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

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

vector<int> stronglyConnectedComponents(const Graph &g) {
  int n = g.size();
  Graph rg(n);
  for (const Edges &es : g)
    for (const Edge &e : es)
      rg[e.dst].eb(e.dst, e.src, e.weight);
  vector<int> S(n * 2);
  vector<int> cmp(n, -1), added(n), visited(n), ord(n);
  int sz = 0;
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      int s = 0;
      S[s++] = i;
      while (s) {
        int v = S[s - 1];
        visited[v] = true;
        bool pushed = false;
        for (auto &e : g[v]) {
          int dst = e.dst;
          if (!visited[dst]) {
            S[s++] = dst;
            pushed = true;
          }
        }
        if (pushed)
          continue;
        int t = S[s - 1];
        if (!added[t]) {
          added[t] = true;
          ord[sz++] = t;
        }
        --s;
      }
    }
  int k = 0;
  for (int i = 0; i < n; i++) {
    int u = ord[n - i - 1];
    if (cmp[u] != -1)
      continue;
    int s = 0;
    S[s++] = u;
    while (s) {
      int v = S[--s];
      cmp[v] = k;
      for (auto &e : rg[v]) {
        int d = e.dst;
        if (cmp[d] == -1)
          S[s++] = d;
      }
    }
    ++k;
  }
  return cmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a].eb(a, b, 0);
  }
  vector<int> c = stronglyConnectedComponents(g);
  int q;
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] == c[b]) << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, b) for (int i = 0; i < int(b); i++)
#define eb emplace_back

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src_, int dst_, Weight weight_)
      : src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

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

vector<int> stronglyConnectedComponents(const Graph &g) {
  int n = g.size();
  Graph rg(n);
  for (const Edges &es : g)
    for (const Edge &e : es)
      rg[e.dst].eb(e.dst, e.src, e.weight);
  static int S[10010];
  vector<int> cmp(n, -1), added(n), visited(n), ord(n);
  int sz = 0;
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      int s = 0;
      S[s++] = i;
      while (s) {
        int v = S[s - 1];
        visited[v] = true;
        bool pushed = false;
        for (auto &e : g[v]) {
          int dst = e.dst;
          if (!visited[dst]) {
            S[s++] = dst;
            pushed = true;
          }
        }
        if (pushed)
          continue;
        int t = S[s - 1];
        if (!added[t]) {
          added[t] = true;
          ord[sz++] = t;
        }
        --s;
      }
    }
  int k = 0;
  for (int i = 0; i < n; i++) {
    int u = ord[n - i - 1];
    if (cmp[u] != -1)
      continue;
    int s = 0;
    S[s++] = u;
    while (s) {
      int v = S[--s];
      cmp[v] = k;
      for (auto &e : rg[v]) {
        int d = e.dst;
        if (cmp[d] == -1)
          S[s++] = d;
      }
    }
    ++k;
  }
  return cmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a].eb(a, b, 0);
  }
  vector<int> c = stronglyConnectedComponents(g);
  int q;
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] == c[b]) << '\n';
  }
}