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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)

#define ZERO(a) memset(a, 0, sizeof(a))
template <int MV> class SCC {
public:
  vector<vector<int>> SC;
  int NV, GR[MV], rev[MV];

private:
  vector<int> E[MV], RE[MV], NUM;
  int vis[MV];

public:
  void init(int NV) {
    this->NV = NV;
    for (int i = 0; i < MV; i++) {
      E[i].clear();
      RE[i].clear();
    }
  }
  void add_edge(int x, int y) {
    E[x].push_back(y);
    RE[y].push_back(x);
  }
  void dfs(int cu) {
    vis[cu] = 1;
    for (int i = 0; i < E[cu].size(); i++)
      if (!vis[E[cu][i]])
        dfs(E[cu][i]);
    NUM.push_back(cu);
  }
  void revdfs(int cu, int ind) {
    int i;
    vis[cu] = 1;
    GR[cu] = ind;
    SC[ind].push_back(cu);
    rev[cu] = ind;
    rep(i, 0, RE[cu].size()) if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind);
  }
  void scc() {
    int c = 0;
    SC.clear();
    SC.resize(MV);
    NUM.clear();
    ZERO(vis);
    for (int i = 0; i < NV; i++)
      if (!vis[i])
        dfs(i);
    ZERO(vis);
    for (int i = NUM.size() - 1; i >= 0; i--)
      if (!vis[NUM[i]]) {
        SC[c].clear();
        revdfs(NUM[i], c);
        sort(SC[c].begin(), SC[c].end());
        c++;
      }
    SC.resize(c);
  }
};

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

  int VN, EN;
  cin >> VN >> EN;
  SCC<10101> scc;
  scc.init(VN);
  rep(i, 0, EN) {
    int s, t;
    cin >> s >> t;
    scc.add_edge(s, t);
  }
  scc.scc();

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int u, v;
    cin >> u >> v;
    if (scc.GR[u] == scc.GR[v])
      printf("1\\n");
    else
      printf("0\\n");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)

#define ZERO(a) memset(a, 0, sizeof(a))
class SCC {
public:
  static const int MV = 102;
  vector<vector<int>> SC;
  int NV, GR[MV], rev[MV];

private:
  vector<int> E[MV], RE[MV], NUM;
  int vis[MV];

public:
  void init(int NV) {
    this->NV = NV;
    for (int i = 0; i < MV; i++) {
      E[i].clear();
      RE[i].clear();
    }
  }
  void add_edge(int x, int y) {
    E[x].push_back(y);
    RE[y].push_back(x);
  }
  void dfs(int cu) {
    vis[cu] = 1;
    for (int i = 0; i < E[cu].size(); i++)
      if (!vis[E[cu][i]])
        dfs(E[cu][i]);
    NUM.push_back(cu);
  }
  void revdfs(int cu, int ind) {
    int i;
    vis[cu] = 1;
    GR[cu] = ind;
    SC[ind].push_back(cu);
    rev[cu] = ind;
    rep(i, 0, RE[cu].size()) if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind);
  }
  void scc() {
    int c = 0;
    SC.clear();
    SC.resize(MV);
    NUM.clear();
    ZERO(vis);
    for (int i = 0; i < NV; i++)
      if (!vis[i])
        dfs(i);
    ZERO(vis);
    for (int i = NUM.size() - 1; i >= 0; i--)
      if (!vis[NUM[i]]) {
        SC[c].clear();
        revdfs(NUM[i], c);
        sort(SC[c].begin(), SC[c].end());
        c++;
      }
    SC.resize(c);
  }
};

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

  int VN, EN;
  cin >> VN >> EN;
  SCC scc;
  scc.init(VN);
  rep(i, 0, EN) {
    int s, t;
    cin >> s >> t;
    scc.add_edge(s, t);
  }
  scc.scc();

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int u, v;
    cin >> u >> v;
    if (scc.GR[u] == scc.GR[v])
      printf("1\\n");
    else
      printf("0\\n");
  }
}