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 Kotlin (judge verdict: Wrong Answer)


import java.util.*

class UnionFindTree{
    private var mParent:UnionFindTree? = null
    private fun Parent():UnionFindTree {
        return when(mParent) {
            null -> this
            else -> {
                mParent = mParent!!.Parent()
                mParent !!
            }
        }
    }
    fun Join(other:UnionFindTree):Unit {
        if (!IsSameUnion(other)){
            Parent().mParent = other.Parent()
        }
    }
    fun IsSameUnion(other:UnionFindTree):Boolean = Parent() == other.Parent()
}
data class Edge(val from:Node, val to:Node)
class Node(val Id:Int){
    private var mForwardEdges: LinkedList<Node> = LinkedList()
    private var mReverseEdges: LinkedList<Node> = LinkedList()
    fun ForwardEdges():Iterable<Node> = mForwardEdges
    fun ReverseEdges():Iterable<Node> = mReverseEdges
    fun AddEdge(edge:Edge):Unit {
        val (from, to) = edge
        when{
            from == this -> mForwardEdges.push(to)
            to == this -> mReverseEdges.push(from)
        }
    }
}
fun DFS(current:Node, numArray:Array<Int>, init:Int = 0):Int {
    val next = current.ForwardEdges().filter{node -> numArray[node.Id] == -1}
    numArray[current.Id] = 0
    numArray[current.Id] = if (next.isEmpty()) init + 1
    else next.fold(init){p, n -> DFS(n, numArray, p)} + 1
    return numArray[current.Id]
}
fun InvDFS(current:Node, uft:Array<UnionFindTree>, numArray: Array<Int>, isSearched:Array<Boolean>):Unit{
    current.ReverseEdges().forEach {
        when{
             !isSearched[it.Id] && numArray[current.Id] > numArray[it.Id] -> {
                isSearched[it.Id] = true
                uft[current.Id].Join(uft[it.Id])
                InvDFS(it, uft, numArray, isSearched)
            }
        }
    }
}

fun main(args:Array<String>):Unit{
    val (v, e) = readLine()!!.split(' ').map(String::toInt)
    val nodes = Array(v){Node(it)}
    val numOfStep = Array(v){-1}
    val uft = Array(v){UnionFindTree()}
    for (i in 0 until e){
        val (s, t) = readLine()!!.split(' ').map(String::toInt)
        val edge = Edge(nodes[s], nodes[t])
        nodes[s].AddEdge(edge)
        nodes[t].AddEdge(edge)
    }
    numOfStep.forEachIndexed { index, i -> when(i){
        -1 -> DFS(nodes[index], numOfStep)
    } }
    val isSearched = Array(v){false}
    numOfStep.withIndex().sortedByDescending{ i -> i.value}.forEach{
        if (!isSearched[it.index]){
            isSearched[it.index] = true
            InvDFS(nodes[it.index], uft, numOfStep, isSearched)
        }
    }
    for (i in 0 until readLine()!!.toInt()){
        val (a, b) = readLine()!!.split(' ').map(String::toInt)
        println(if (uft[a].IsSameUnion(uft[b])) 1
        else 0)
    }
}