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++11 (judge verdict: Time Limit Exceeded)

#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
#define MAX_N 100003
vector<int>X[MAX_N]; bool COLOR[MAX_N];int N, M, Q1, A, B;
pair<int, int> R[MAX_N]; vector<pair<int, int>>S[MAX_N]; int T[MAX_N];
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); }
	cin >> Q1;
	for (int i = 0; i < Q1; i++) {
		cin >> R[i].first >> R[i].second; S[R[i].first].push_back(make_pair(R[i].second, i));
		S[R[i].second].push_back(make_pair(R[i].first, i));
	}
	for (int i = 0; i < N; i++) {
		if (S[i].size() >= 1) {
			queue<int>Q;
			Q.push(i); memset(COLOR, false, sizeof(COLOR)); COLOR[i] = true;
			while (!Q.empty())
			{
				int B = Q.front(); Q.pop();
				for (int j = 0; j < X[B].size(); j++) {
					if (COLOR[X[B][j]] == false) {
						COLOR[X[B][j]] = true;
						Q.push(X[B][j]);
					}
				}
			}
			for (int j = 0; j < S[i].size(); j++) {
				int a1 = S[i][j].first, a2 = S[i][j].second;
				if (COLOR[a1] == 1) { T[a2]++; }
			}
		}
	}
	for (int i = 0; i < Q1; i++) {
		if (T[i] == 2) { cout << '1' << endl; }
		else { cout << '0' << endl; }
	}
	return 0;
}