Problem:
Strongly Connected Components

A direced graph is strongly connected if every two nodes are reachable from each other. In a strongly connected component of a directed graph, every two nodes of the component are mutually reachable.

Input

A directed graph G(V, E) and a sequence of queries where each query contains a pair of nodes u and v.

|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
Q
u0 v0
u1 v1
:
uQ-1 vQ-1

|V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target nodes of i-th edge (directed).

ui and vi represent a pair of nodes given as the i-th query.

Output

For each query, pinrt "1" if the given nodes belong to the same strongly connected component, "0" otherwise.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 30,000

1 ≤ Q ≤ 100,000

Sample Input 1

5 6
0 1
1 0
1 2
2 4
4 3
3 2
4
0 1
0 3
2 3
3 4

Sample Output 1

1
0
1
1

Solution in C++ (judge verdict: Accepted)

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

//????????°???n
//SCC sc(n);
//sc.comp()??§?????£?????????????§£
//sc.make_graph()??§?????£?????????????§£????????°??????????§????

#define rep(i,n) for(int i=0;i<(int)(n);++i)

class SCC {
public:
	vector<vector<int> > G,rG;
	vector<int> post_order; //??°?????????????????????
	vector<bool> used; //?????§????????????????????????
	vector<int> cmp;	//?±????????????£????????????????????????????????????
	vector<vector<int> > graph;	//?????£?????????????§£????????°??????
	int V,VV;	//?????£?????????????§£?????????????????°
	SCC(int node_size){
		V = node_size;
		G.resize(node_size),rG.resize(node_size),used.resize(node_size,false);
		cmp.resize(node_size);
	}
	void add_edge(int from,int to){
		G[from].push_back(to), rG[to].push_back(from);
	}
	void dfs(int v){
		used[v] = true;
		for(int i=0;i<(int)G[v].size();i++){
			if(!used[G[v][i]]){
				dfs(G[v][i]);
			}
		}
		post_order.push_back(v);
	}
	void rdfs(int v,int k){
		used[v] = true;
		cmp[v] = k;
		for(int i=0;i<(int)rG[v].size();i++){
			if(!used[rG[v][i]]){
				rdfs(rG[v][i],k);
			}
		}
	}
	void solve(){
		fill(used.begin(),used.end(),0);
		post_order.clear();
		for(int v=0;v<V;v++){
			if(!used[v]){
				dfs(v);
			}
		}
		fill(used.begin(),used.end(),0);
		int k=0;
		for(int i=(int)post_order.size()-1;i>=0;i--){
			if(!used[post_order[i]]){
				rdfs(post_order[i],k++);
			}
		}
		VV = k;
	}
	void make_graph(){
		graph.resize(VV);
		rep(i,V){
			rep(j,G[i].size()){
				if(cmp[i] != cmp[G[i][j]]){
					graph[i].push_back(G[i][j]);
				}
			}
		}
	}
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    SCC scc(n);
    rep(i,m){
        int s,t;
        cin >> s >> t;
        scc.add_edge(s,t);
    }
    int q;
    cin >> q;
    scc.solve();
    rep(i,q){
        int u,v;
        cin >> u >> v;
        if(scc.cmp[u] == scc.cmp[v]){
            cout << "1\n";
        }else{
            cout << "0\n";
        }
    }
    return 0;
}