output_description: For each query, pinrt "1" if the given nodes belong to the same strongly
  connected component, "0" otherwise.
input_description: 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.
attempt: import sys
  
  sys.setrecursionlimit(int(1e6))
  
  n, m = map(int, input().split())
  
  graph = {}
  graph_rev = {}
  
  for i in range(m):
      s, t = map(int, input().split())
      if s not in graph:
          graph[s] = set()
      graph[s].add(t)
      if t not in graph_rev:
          graph_rev[t] = set()
      graph_rev[t].add(s)
  
  order = []
  visited = set()
  
  
  def dfs1(i):
      visited.add(i)
      if i in graph:
          for t in graph[i]:
              if t not in visited:
                  dfs1(t)
      order.append(i)
  
  
  for i in range(n):
      if i not in visited:
          dfs1(i)
  
  group = [-1] * n
  visited.clear()
  
  
  def dfs2(i, g):
      visited.add(i)
      group[i] = g
      if i in graph_rev:
          for t in graph_rev[i]:
              if t not in visited:
                  dfs2(t, g)
  
  
  group_id = 0
  
  while len(order) > 0:
      v = order.pop()
      if v in visited:
          continue
      dfs2(v, group_id)
      group_id += 1
  
  q = int(input())
  ans = []
  
  for i in range(q):
      u, v = map(int, input().split())
      ans.append(1) if group[u] == group[v] else ans.append(0)
  
  for a in ans:
      print(a)
  
problem_description: 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.
samples: [{"input": "5 6\n    0 1\n    1 0\n    1 2\n    2 4\n    4 3\n    3 2\n    4\n    0 1\n    0 3\n    2 3\n    3 4", "output": "1\n    0\n    1\n    1"}]