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;
using System.Windows;
using System.Collections.Generic;
using System.Linq;

class Program
{
  static int n;
    static void Main(string[] args)
    {
      //入力を受け取る
      n = int.Parse(Console.ReadLine());
      Node[] T = new Node[n+1];
      
      //各ノードを初期化
      for(int i = 1; i < n+1 ; i++){
        var ukv = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        var u = ukv[0];
        var k = ukv[1];
        T[i] = new Node(u,k);
        for(int j = 2;j < k+2; j++){
          T[i].RegistAdjacency(ukv[j]);
        }
      }
      
      //キューにノード1を入れる
      NodeQueue MyQ = new NodeQueue();
      T[1].d = 0;
      MyQ.enque(T[1]);
      //キューが空になるまで探索をする
      while(MyQ.IsNotEmpty()){
        //キューからノードを取り出す
        var extract = MyQ.deque();
        //Console.WriteLine("aaa");
        //取り出したノードの子ノードの深さを登録
        foreach(int child in extract.adjacency){
          //深さが未登録であったらキューに入れる
          if(T[child].d == -1){
            T[child].d = extract.d+1;
            MyQ.enque(T[child]);
          }
          //親の次数と子の次数の差が1以上あったら最短経路の方を登録
          if(extract.d+1 < T[child].d){
            T[child].d = extract.d+1;
          }
        }
      }
      
     // Console.WriteLine(string.Join(", ",T[1].adjacency));
      
      for(int i = 1;i < n+1;i++){
        Console.WriteLine(T[i].id+" "+T[i].d);
      }
    }

}

class Node{
  public int id;
  public int d = -1;
  public int[] adjacency;
  private int point = 0;
  
  public Node(int id,int depth){
    this.id = id;
    this.adjacency = new int[depth];
  }
  
  public void RegistAdjacency(int child){
    this.adjacency[point] = child;
    point++;
  }
}

class NodeQueue{
  Node[] Nodes = new Node[100];
  int head = 0;
  int tail = 0;
  
  public void enque(Node N){
    Nodes[tail] = N;
    tail++;
  }
  
  public Node deque(){
    Node res = Nodes[head];
    head++;
    return res;
  }
  
  public bool IsNotEmpty(){
    if(tail != head){
      return true;
    }else{
      return false;
    }
  }
}