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 C# (judge verdict: Accepted)

using System;

public class Queue
{
  private int size, top, tail;
  private int[] array;
  public Queue(int size)
  {
    this.size = size + 1;
    array = new int[this.size];
    top = tail = 0;
  }
  public void Enqueue(int val)
  {
    if ((tail + 1) % this.size == this.top) throw new Exception("overflow error");
    this.array[tail] = val;
    if (this.tail + 1 >= this.size) this.tail = 0;
    else this.tail++;
  }
  public int Dequeue()
  {
    if (this.top == this.tail) throw new Exception("underflow error");
    int head = array[this.top];
    if (this.top + 1 >= this.size) this.top = 0;
    else this.top++;
    return head;
  }
  public bool IsEmpty()
  {
    return this.top == this.tail;
  }
}

public class Program
{
  private static int[,] graph;
  private static int[] distance;
  private static int graphSize;

  private static void BreadthFirsSearch()
  {
    Queue queue = new Queue(graphSize);
    try
    {
      queue.Enqueue(0);
      distance[0] = 0;
      while (!queue.IsEmpty())
      {
        int u = queue.Dequeue();
        for (int i = 0; i < graphSize; i++)
        {
          if (graph[u, i] == 0) continue;
          if (distance[i] != Int32.MaxValue) continue;
          distance[i] = distance[u] + 1;
          queue.Enqueue(i);
        }
      }
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.StackTrace.ToString());
    }
  }
  public static void Main()
  {
    int i, j, u, k, v;
    string[] ary;

    graphSize = int.Parse(Console.ReadLine());
    distance = new int[graphSize];
    for (i = 0; i < graphSize; i++) distance[i] = Int32.MaxValue;

    graph = new int[graphSize, graphSize];
    for (i = 0; i < graphSize; i++)
      for (j = 0; j < graphSize; j++)
        graph[i, j] = 0;

    for (i = 0; i < graphSize; i++)
    {
      ary = Console.ReadLine().Split(' ');
      u = int.Parse(ary[0]);
      k = int.Parse(ary[1]);
      for (j = 0; j < k; j++)
      {
        v = int.Parse(ary[2 + j]);
        graph[u - 1, v - 1] = 1;
      }
    }
    BreadthFirsSearch();

    for (i = 0; i < graphSize; i++)
    {
      Console.WriteLine("{0} {1}", i + 1, distance[i] == Int32.MaxValue ? -1 : distance[i]);
    }

  }
}