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<stdio.h>
int ta[10010],to[30010],nt[30010],rta[10010],rto[30010],rnt[30010];
int id[10010],rid[10010],cmp[100010],r=0,f[10010]={0};
void dfs(int s){
  int i;
  f[s]=1;
  for(i=ta[s];i+1;i=nt[i]){
    if(f[to[i]]==0)dfs(to[i]);
  }
  id[rid[s]=r++]=s;//printf("\n");
}
void rdfs(int s,int k){
  int i;
  f[s]=2;
  cmp[s]=k;
  for(i=rta[s];i+1;i=rnt[i]){
    if(f[rto[i]]==1)rdfs(rto[i],k);
  }
}
void scc(int v,int e,int *a,int *b){
  int i,k;
  for(i=0;i<v;i++)ta[i]=rta[i]=-1;
  for(i=0;i<e;i++){
     nt[i]= ta[rto[i]=a[i]];
    rnt[i]=rta[ to[i]=b[i]];
    ta[a[i]]=rta[b[i]]=i;
  }
  for(i=k=0;i<v;i++){
    if(f[i]==0)dfs(i);
  }
  for(i=r;i;i--){
    if(f[id[i-1]]==1)rdfs(id[i-1],id[i-1]);
  }
}
int main(){
  int v,e,a[100010],b[100010],s,t,i;
  scanf("%d %d",&v,&e);
  for(i=0;i<e;i++)scanf("%d %d",&a[i],&b[i]);
  scc(v,e,a,b);
  scanf("%d",&v);
  while(v--){
    scanf("%d %d",&s,&t);
    printf("%d\n",cmp[s]-cmp[t]?0:1);
  }
  return 0;
}