Language: C++

#include <iostream>
#include <stack>
using namespace std;

template <typename Weight>
struct Edge {
    std::size_t from;
    std::size_t to;
    Weight cost;
    bool operator<(const Edge& e)
    {
        return cost < e.cost;
    }
    bool operator>(const Edge& e)
    {
        return cost > e.cost;
    }
};

template <typename Weight = bool>
struct GraphList {
    GraphList(const std::size_t V) : V(V)
    {
        edge.resize(V);
    }
    void insert(const std::size_t from, const std::size_t to, const Weight cost = 1)
    {
        edge[from].push_back(Edge<Weight>{from, to, cost});
    }
    GraphList<Weight> reversed() const
    {
        GraphList<Weight> g(V);
        for (std::size_t from = 0; from < V; from++) {
            for (const auto& e : edge[from]) {
                g.insert(e.to, from, e.cost);
            }
        }
        return g;
    }

    std::vector<std::vector<Edge<Weight>>> edge;
    const std::size_t V;
};

template <typename Weight = bool>
void SCC(const GraphList<Weight>& g, std::size_t cmp[])
{
    constexpr std::size_t INFINITY = static_cast<std::size_t>(-1);
    std::stack<std::size_t> st;
    for (std::size_t i = 0; i < g.V; i++) {
        cmp[i] = 1;
    }
    for (std::size_t i = 0; i < g.V; i++) {
        if (cmp[i]) {
            std::stack<std::size_t> inner_st;
            inner_st.push(i);
            while (not inner_st.empty()) {
                const std::size_t u = inner_st.top();
                if (cmp[u]) {
                    cmp[u] = 0;
                }
                bool fin = true;
                for (const auto& e : g.edge[u]) {
                    const std::size_t v = e.to;
                    if (cmp[v]) {
                        inner_st.push(v);
                        fin = false;
                    }
                }
                if (fin) {
                    st.push(u);
                    inner_st.pop();
                }
            }
        }
    }

    const GraphList<Weight> rg = g.reversed();
    for (std::size_t i = 0; i < g.V; i++) {
        cmp[i] = INFINITY;
    }
    std::size_t comp = 0;
    while (not st.empty()) {
        const std::size_t s = st.top();
        std::stack<std::size_t> inner_st;
        if (cmp[s] == INFINITY) {
            inner_st.push(s);
            while (not inner_st.empty()) {
                const std::size_t u = inner_st.top();
                if (cmp[u] == INFINITY) {
                    cmp[u] = comp;
                }
                bool fin = true;
                for (const auto& e : rg.edge[u]) {
                    const std::size_t v = e.to;
                    if (cmp[v] == INFINITY) {
                        inner_st.push(v);
                        fin = false;
                    }
                }
                if (fin) {
                    comp++;
                    inner_st.pop();
                }
            }
        }
        st.pop();
    }
}

template <typename Weight = bool>
void SCC(const GraphList<Weight>& g, std::vector<std::size_t>& cmp)
{
    constexpr std::size_t INFINITY = static_cast<std::size_t>(-1);
    std::stack<std::size_t> st;
    for (std::size_t i = 0; i < g.V; i++) {
        cmp[i] = 1;
    }
    for (std::size_t i = 0; i < g.V; i++) {
        if (cmp[i]) {
            std::stack<std::size_t> inner_st;
            inner_st.push(i);
            while (not inner_st.empty()) {
                const std::size_t u = inner_st.top();
                if (cmp[u]) {
                    cmp[u] = 0;
                }
                bool fin = true;
                for (const auto& e : g.edge[u]) {
                    const std::size_t v = e.to;
                    if (cmp[v]) {
                        inner_st.push(v);
                        fin = false;
                    }
                }
                if (fin) {
                    st.push(u);
                    inner_st.pop();
                }
            }
        }
    }

    const GraphList<Weight> rg = g.reversed();
    for (std::size_t i = 0; i < g.V; i++) {
        cmp[i] = INFINITY;
    }
    std::size_t comp = 0;
    while (not st.empty()) {
        const std::size_t s = st.top();
        std::stack<std::size_t> inner_st;
        if (cmp[s] == INFINITY) {
            inner_st.push(s);
            while (not inner_st.empty()) {
                const std::size_t u = inner_st.top();
                if (cmp[u] == INFINITY) {
                    cmp[u] = comp;
                }
                bool fin = true;
                for (const auto& e : rg.edge[u]) {
                    const std::size_t v = e.to;
                    if (cmp[v] == INFINITY) {
                        inner_st.push(v);
                        fin = false;
                    }
                }
                if (fin) {
                    comp++;
                    inner_st.pop();
                }
            }
        }
        st.pop();
    }
}

int main()
{
    std::size_t V, E, Q;
    cin >> V >> E;
    GraphList<> g(V);
    for (std::size_t i = 0; i < E; i++) {
        std::size_t s, t;
        cin >> s >> t;
        g.insert(s, t);
    }
    vector<std::size_t> cmp(V);
    SCC(g, cmp);
    cin >> Q;
    for (std::size_t i = 0; i < Q; i++) {
        std::size_t u, v;
        cin >> u >> v;
        if (cmp[u] == cmp[v]) {
            cout << 1 << endl;
        } else {
            cout << 0 << endl;
        }
    }

    return 0;
}

Compiler output:
a.cc:41:10: error: 'vector' in namespace 'std' does not name a template type
   41 |     std::vector<std::vector<Edge<Weight>>> edge;
      |          ^~~~~~
a.cc:3:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    2 | #include <stack>
  +++ |+#include <vector>
    3 | using namespace std;
a.cc: In constructor 'GraphList<Weight>::GraphList(std::size_t)':
a.cc:24:9: error: 'edge' was not declared in this scope; did you mean 'Edge'?
   24 |         edge.resize(V);
      |         ^~~~
      |         Edge
a.cc: In member function 'void GraphList<Weight>::insert(std::size_t, std::size_t, Weight)':
a.cc:28:9: error: 'edge' was not declared in this scope; did you mean 'Edge'?
   28 |         edge[from].push_back(Edge<Weight>{from, to, cost});
      |         ^~~~
      |         Edge
a.cc: In member function 'GraphList<Weight> GraphList<Weight>::reversed() const':
a.cc:34:34: error: 'edge' was not declared in this scope; did you mean 'Edge'?
   34 |             for (const auto& e : edge[from]) {
      |                                  ^~~~
      |                                  Edge
a.cc: At global scope:
a.cc:112:38: error: 'std::vector' has not been declared
  112 | void SCC(const GraphList<Weight>& g, std::vector<std::size_t>& cmp)
      |                                      ^~~
a.cc:112:49: error: expected ',' or '...' before '<' token
  112 | void SCC(const GraphList<Weight>& g, std::vector<std::size_t>& cmp)
      |                                                 ^
a.cc: In function 'void SCC(const GraphList<Weight>&, int)':
a.cc:117:9: error: 'cmp' was not declared in this scope
  117 |         cmp[i] = 1;
      |         ^~~
a.cc:120:13: error: 'cmp' was not declared in this scope
  120 |         if (cmp[i]) {
      |             ^~~
a.cc:146:9: error: 'cmp' was not declared in this scope
  146 |         cmp[i] = INFINITY;
      |         ^~~
a.cc:152:13: error: 'cmp' was not declared in this scope; did you mean 'comp'?
  152 |         if (cmp[s] == INFINITY) {
      |             ^~~
      |             comp
a.cc: In function 'int main()':
a.cc:187:5: error: 'vector' was not declared in this scope
  187 |     vector<std::size_t> cmp(V);
      |     ^~~~~~
a.cc:187:5: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:187:23: error: expected primary-expression before '>' token
  187 |     vector<std::size_t> cmp(V);
      |                       ^
a.cc:187:25: error: 'cmp' was not declared in this scope
  187 |     vector<std::size_t> cmp(V);
      |                         ^~~