Problem p02368 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
/**
 * Simple Graph Definition
 */
struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
using Graph = vector<vector<Edge>>;

// ????????°???????????¨???
void add_edge(Graph &graph, int u, int v, int cost = 0) {
  graph[u].push_back(Edge(v, cost));
  graph[v].push_back(Edge(u, cost));
}

void SCC(const Graph &g, vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;

  function<void(int)> visit = [&](int v) {
    low[v] = num[v] = ++time;
    S.push(v);
    inS[v] = true;
    for (auto &e : g[v]) {
      int w = e.to;
      if (num[w] == 0) {
        visit(w);
        low[v] = min(low[v], low[w]);
      } else if (inS[w])
        low[v] = min(low[v], num[w]);
    }
    if (low[v] == num[v]) {
      scc.push_back(vector<int>());
      while (1) {
        int w = S.top();
        S.pop();
        inS[w] = false;
        scc.back().push_back(w);
        if (v == w)
          break;
      }
    }
  };

  REP(u, n) if (num[u] == 0) visit(u);
}

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

  int V, E;
  cin >> V >> E;
  Graph G(V);
  REP(i, E) {
    int s, t;
    cin >> s >> t;
    G[s].EB(t);
  }
  vector<vector<int>> scc;
  SCC(G, scc);
  vector<int> gr(SZ(scc));
  REP(i, SZ(scc))
  for (int u : scc[i])
    gr[u] = i;

  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << (gr[u] == gr[v]) << endl;
  }

  return 0;
}

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
/**
 * Simple Graph Definition
 */
struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
using Graph = vector<vector<Edge>>;

// ????????°???????????¨???
void add_edge(Graph &graph, int u, int v, int cost = 0) {
  graph[u].push_back(Edge(v, cost));
  graph[v].push_back(Edge(u, cost));
}

void SCC(const Graph &g, vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;

  function<void(int)> visit = [&](int v) {
    low[v] = num[v] = ++time;
    S.push(v);
    inS[v] = true;
    for (auto &e : g[v]) {
      int w = e.to;
      if (num[w] == 0) {
        visit(w);
        low[v] = min(low[v], low[w]);
      } else if (inS[w])
        low[v] = min(low[v], num[w]);
    }
    if (low[v] == num[v]) {
      scc.push_back(vector<int>());
      while (1) {
        int w = S.top();
        S.pop();
        inS[w] = false;
        scc.back().push_back(w);
        if (v == w)
          break;
      }
    }
  };

  REP(u, n) if (num[u] == 0) visit(u);
}

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

  int V, E;
  cin >> V >> E;
  Graph G(V);
  REP(i, E) {
    int s, t;
    cin >> s >> t;
    G[s].EB(t);
  }
  vector<vector<int>> scc;
  SCC(G, scc);
  vector<int> gr(V);
  REP(i, SZ(scc))
  for (int u : scc[i])
    gr[u] = i;

  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << (gr[u] == gr[v]) << endl;
  }

  return 0;
}