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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

class SCC {
private:
  const int n;
  VV G;
  VV rG;
  V vs;
  vector<bool> used;
  V cmp;

public:
  SCC(int _n) : n(_n), G(_n), rG(_n), used(_n), cmp(_n) {}
  void addEdge(int from, int to) {
    G[from].emplace_back(to);
    rG[to].emplace_back(from);
  }
  void dfs(int v) {
    used[v] = true;
    for (auto &&nxt : G[v]) {
      if (!used[nxt])
        dfs(nxt);
    }
    vs.emplace_back(v);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (auto &&nxt : rG[v]) {
      if (!used[nxt])
        rdfs(nxt, k);
    }
    vs.emplace_back(v);
  }
  int scc() {
    rep(v, n) {
      if (!used[v])
        dfs(v);
    }
    used.assign(n, false);
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--) {
      int v = vs[i];
      if (!used[v])
        rdfs(v, k++);
    }
    return k;
  }
  bool same(int a, int b) { return cmp[a] == cmp[b]; }
};

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

  int N, M;
  cin >> N >> M;
  SCC scc(N);
  rep(i, M) {
    int s, t;
    cin >> s >> t;
    scc.addEdge(s, t);
  }

  scc.scc();

  int Q;
  cin >> Q;
  rep(i, Q) {
    int u, v;
    cin >> u >> v;
    cout << scc.same(u, v) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

class SCC {
private:
  const int n;
  VV G;
  VV rG;
  V vs;
  vector<bool> used;
  V cmp;

public:
  SCC(int _n) : n(_n), G(_n), rG(_n), used(_n), cmp(_n) {}
  void addEdge(int from, int to) {
    G[from].emplace_back(to);
    rG[to].emplace_back(from);
  }
  void dfs(int v) {
    used[v] = true;
    for (auto &&nxt : G[v]) {
      if (!used[nxt])
        dfs(nxt);
    }
    vs.emplace_back(v);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (auto &&nxt : rG[v]) {
      if (!used[nxt])
        rdfs(nxt, k);
    }
    vs.emplace_back(v);
  }
  int scc() {
    rep(v, n) {
      if (!used[v])
        dfs(v);
    }
    reverse(all(vs));
    used.assign(n, false);
    int k = 0;
    for (auto &&v : vs) {
      // for (int i = vs.size() - 1; i >= 0; i--) {
      //     int v = vs[i];
      if (!used[v])
        rdfs(v, k++);
    }
    return k;
  }
  bool same(int a, int b) { return cmp[a] == cmp[b]; }
};

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

  int N, M;
  cin >> N >> M;
  SCC scc(N);
  rep(i, M) {
    int s, t;
    cin >> s >> t;
    scc.addEdge(s, t);
  }

  scc.scc();

  int Q;
  cin >> Q;
  rep(i, Q) {
    int u, v;
    cin >> u >> v;
    cout << scc.same(u, v) << endl;
  }
}