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)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var E = Reader.IntTable(M);
        int NQ = Reader.Int();
        var Q = Reader.IntTable(NQ);
        var scc = new StronglyConnectedComponent(N, E);
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        foreach (var q in Q)
            Console.WriteLine(scc.SameComponent(q[0], q[1]) ? 1 : 0);
        Console.Out.Flush();
        Console.ReadLine();
    }
}

class Component
{
    public List<int> Vertexes;
    public int[] Edges;
    public Component(List<int> vs) { Vertexes = vs; }
}

class StronglyConnectedComponent
{
    int NumE;
    int[] VtoComp, stack;
    List<int>[] E, RevE;
    public List<Component> Components = new List<Component>();

    public int Count { get { return Components.Count; } }
    public bool SameComponent(int a, int b) { return VtoComp[a] == VtoComp[b]; }
    public int VtoComponentId(int v) { return VtoComp[v]; }
    public Component this[int i] { get { return Components[i]; } }

    public StronglyConnectedComponent(int N) { Init(N); }
    public StronglyConnectedComponent(int N, int[][] edges, int origin = 0)
    {
        Init(N);
        NumE = edges.Length;
        Array.ForEach(edges, e => AddEdge(e[0] - origin, e[1] - origin));
        Decompose();
    }

    void Init(int N)
    {
        VtoComp = new int[N];
        E = new List<int>[N];
        RevE = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            VtoComp[i] = -1;
            E[i] = new List<int>();
            RevE[i] = new List<int>();
        }
    }

    public void AddEdge(int from, int to)
    {
        NumE++;
        E[from].Add(to);
        RevE[to].Add(from);
    }

    public void Decompose()
    {
        stack = new int[NumE + 1];
        var vs = new List<int>();
        var seen = new bool[E.Length];
        for (int v = 0; v < E.Length; v++)
            if (!seen[v]) DFS(v, seen, vs);
        for (int i = vs.Count - 1; i >= 0; i--)
            if (VtoComp[vs[i]] == -1)
                Components.Add(DFSRev(vs[i], Components.Count));
        CheckEdges();
    }

    void CheckEdges()
    {
        for (int compId = 0; compId < Count; compId++)
        {
            var comp = Components[compId];
            var es = new HashSet<int>();
            foreach (int a in comp.Vertexes)
                foreach (int b in E[a])
                    if (VtoComp[b] != compId)
                        es.Add(VtoComp[b]);
            comp.Edges = es.ToArray();
            Array.Sort(comp.Edges);
        }
    }

    void DFS(int v, bool[] seen, List<int> vs)
    {
        stack[0] = v;
        for (int si = 1; si > 0; )
        {
            v = stack[--si];
            if (v < 0) { vs.Add(~v); continue; }
            if (seen[v]) continue;
            seen[v] = true;
            stack[si++] = ~v;
            for (int i = E[v].Count - 1; i >= 0; i--)
                stack[si++] = E[v][i];
        }
    }
    Component DFSRev(int v, int componentId)
    {
        var vs = new List<int>();
        stack[0] = v;
        for (int si = 1; si > 0; )
        {
            v = stack[--si];
            if (VtoComp[v] != -1) continue;
            VtoComp[v] = componentId;
            vs.Add(v);
            for (int i = RevE[v].Count - 1; i >= 0; i--)
                stack[si++] = RevE[v][i];
        }
        vs.Sort();
        return new Component(vs);
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { Dispose(); A = new string[0]; }
    public static void Set(TextReader r) { Init(); reader = r; }
    public static void Set(string file) { Init(); reader = new StreamReader(file); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    public static void Dispose() { reader.Dispose(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}