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 Python3 (judge verdict: Accepted)

import sys
sys.setrecursionlimit(1000000)

def dfs(v, visited, edges, order):
  visited[v] = True
  for to in edges[v]:
    if not visited[to]:
      dfs(to, visited, edges, order)
  order.append(v)

def search_strongly_connection(v, visited, reverse_edges, parent, num):
  visited[v] = True
  for to in reverse_edges[v]:
    if not visited[to]:
      parent[to] = num
      search_strongly_connection(to, visited, reverse_edges, parent, num)


v,e=map(int,input().split())
edges=[[] for _ in range(v)]
reverse_edges = [[] for _ in range(v)]
#有向グラフ
for _ in range(e):
    s,t=map(int,input().split())
    edges[s].append(t)
    reverse_edges[t].append(s)

order=[]
visited=[False]*v
for i in range(v):
    if not visited[i]:
        dfs(i,visited,edges,order)
order.reverse()

visited=[False]*v
parent=[i for i in range(v)]
for i in order:
    if not visited[i]:
        search_strongly_connection(i,visited,reverse_edges,parent,i)

q=int(input())
for _ in range(q):
    u,v=map(int,input().split())
    print(1 if parent[u]==parent[v] else 0)