Problem p02368 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const int N = 100;

int idx[N];
int lowlink[N];
bool onstack[N];

struct edge {
  int to;
  edge(int _to) : to(_to){};
};
using node = vector<edge>;
using graph = vector<node>;

void visit(graph &g, int node, VVI &scc, stack<int> &st, vector<bool> &on,
           VI &lowlink, VI &id, int &cur) {
  lowlink[node] = id[node] = cur++;
  st.push(node);
  on[node] = true;
  for (auto e : g[node]) {
    if (id[e.to] == -1) {
      visit(g, e.to, scc, st, on, lowlink, id, cur);
      lowlink[node] = min(lowlink[node], lowlink[e.to]);
    } else if (on[e.to]) {
      lowlink[node] = min(lowlink[node], id[e.to]);
    }
    if (lowlink[node] == id[node]) {
      scc.PB(VI());
      while (1) {
        int t_node = st.top();
        st.pop();
        on[t_node] = false;
        scc.back().push_back(t_node);
        if (t_node == node)
          break;
      }
    }
  }
}

VVI scc(graph &g) {
  int n = g.size();
  VI id(n, -1), low(n);
  stack<int> st;
  vector<bool> on(n);
  int cur = 0;
  VVI scc;
  REP(i, n) if (id[i] == -1) visit(g, i, scc, st, on, low, id, cur);
  return scc;
}

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

int main() {
  int v, e;
  scanf("%d%d", &v, &e);
  graph g(v);
  REP(i, e) {
    int x, y;
    scanf("%d%d", &x, &y);
    addedge(g, x, y);
  }
  auto SCC = scc(g);
  VI idx(v);
  REP(i, SCC.size()) {
    for (auto x : SCC[i]) {
      idx[x] = i;
    }
  }
  int q;
  scanf("%d", &q);
  REP(i, q) {
    int x, y;
    scanf("%d%d", &x, &y);
    cout << (idx[x] == idx[y]) << endl;
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const int N = 100;

int idx[N];
int lowlink[N];
bool onstack[N];

struct edge {
  int to;
  edge(int _to) : to(_to){};
};
using node = vector<edge>;
using graph = vector<node>;

void visit(graph &g, int node, VVI &scc, stack<int> &st, vector<bool> &on,
           VI &lowlink, VI &id, int &cur) {
  lowlink[node] = id[node] = cur++;
  st.push(node);
  on[node] = true;
  for (auto e : g[node]) {
    if (id[e.to] == -1) {
      visit(g, e.to, scc, st, on, lowlink, id, cur);
      lowlink[node] = min(lowlink[node], lowlink[e.to]);
    } else if (on[e.to]) {
      lowlink[node] = min(lowlink[node], id[e.to]);
    }
  }
  if (lowlink[node] == id[node] && !st.empty()) {
    scc.PB(VI());
    while (1) {
      int t_node = st.top();
      st.pop();
      on[t_node] = false;
      scc.back().push_back(t_node);
      if (t_node == node)
        break;
    }
  }
}

VVI scc(graph &g) {
  int n = g.size();
  VI id(n, -1), low(n);
  stack<int> st;
  vector<bool> on(n);
  int cur = 0;
  VVI scc;
  REP(i, n) if (id[i] == -1) visit(g, i, scc, st, on, low, id, cur);
  return scc;
}

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

int main() {
  int v, e;
  scanf("%d%d", &v, &e);
  graph g(v);
  REP(i, e) {
    int x, y;
    scanf("%d%d", &x, &y);
    addedge(g, x, y);
  }
  auto SCC = scc(g);
  VI idx(v);
  REP(i, SCC.size()) {
    for (auto x : SCC[i]) {
      idx[x] = i;
    }
  }
  int q;
  scanf("%d", &q);
  REP(i, q) {
    int x, y;
    scanf("%d%d", &x, &y);
    cout << (idx[x] == idx[y]) << endl;
  }
  return 0;
}