Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Breadth First Search

Write a program which reads an directed graph $G = (V, E)$, and finds the shortest distance from vertex $1$ to each vertex (the number of edges in the shortest path). Vertices are identified by IDs $1, 2, ... n$.

Input

In the first line, an integer $n$ denoting the number of vertices, is given. In the next $n$ lines, adjacent lists of vertex $u$ are given in the following format:

$u$ $k$ $v_1$ $v_2$ ... $v_k$

$u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices adjacent to $u$.

Constraints

$1 \leq n \leq 100$

Output

For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$ and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in order of IDs.

Sample Input 1

4
1 2 2 4
2 1 4
3 0
4 1 3

Sample Output 1

1 0
2 1
3 2
4 1

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in Scala (judge verdict: Wrong Answer)

import scala.io.StdIn.{readInt, readLine};
import scala.reflect.ClassTag;
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val g = new Graph(n);
        (0 until n).foreach { i => 
            readLine.split(" ").map(_.toInt).toList match {
                case src :: d :: dst => g.add(src, dst);
                case _ => ()
            }
        }
        g.breadthFirstSearch.foreach { e => println("%d %d".format(e._2, e._1))}
    }
}
class Graph(n: Int) {
    val size = n + 1;
    val matrix = Array.ofDim[Int](size, size);
    
    def add(src: Int, dsts: Seq[Int]): Unit = if (src > 0 && src < size) {
        dsts.foreach { d => if (d > 0 && d < size) matrix(src)(d) = 1};
    }
    
    def dump(): Unit = matrix.slice(1, size).foreach { a => println(a.slice(1, size).mkString(" "))}
    
    def breadthFirstSearch: Array[(Int, Int)] = {
        val distance = new Array[Int](size).map(a => -1);
        def go(que: Queue[Int]): Unit = que.deque() match {
            case None => ();
            case Some(target) => {
                (1 until size).foreach { i => 
                    if (matrix(target)(i) == 1 && distance(i) == -1) {
                        que.enque(i);
                        distance(i) = distance(target) + 1;
                    }
                }
                go(que);
            }
        }
        (1 until size).foreach { i =>
            if (distance(i) == -1) {
                val que = new Queue[Int](size);
                que.enque(i);
                distance(i) = 0;
                go(que);
            }
        }
        distance.zipWithIndex.slice(1, size);
    }
}
class Queue[A](size: Int)(implicit m: ClassTag[A]) {
    var inPtr = 0;
    var outPtr = 0;
    var cnt = 0;
    val q = m.newArray(size);
    
    def isEmpty: Boolean = cnt == 0;
    
    def isFull: Boolean = cnt == size;
    
    def enque(elem: A): Boolean = if (isFull) false else {
        q(inPtr) = elem;
        inPtr = if (inPtr == (size - 1)) 0 else inPtr + 1;
        cnt += 1;
        true;
    }
    
    def deque(): Option[A] = if (isEmpty) None else {
        val elem = q(outPtr);
        outPtr = if (outPtr == size - 1) 0 else outPtr + 1;
        cnt -= 1;
        Some(elem);
    }
}