Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include <iostream>
#include <queue>


using namespace std;


static const  int N =  100;
static const  int no =  -2;

int graph[N][N] = {{}};
int d[N] = {};
int n;

void bfs(int v){
  queue<int> Q; 
  Q.push(v);

  int u;
    for(int i = 1;i <= n; i++){
      d[i] = no;
    
}
  d[v] = 0;

  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] +1;
      Q.push(i);
    }
  }
}


int main(){
  int u,v,k;
  //graph
  cin >> n ;

  for(int i = 0; i < n; i++){
    cin >> u >> k ;
    for(int j = 0; j < k ; j++){
      cin >> v;
      graph[u][v] = 1;
    }
  }


  bfs(1);

  for(int i = 1;i <= n; i++){
    cout << i << ' ' << d[i] << endl;
  }

  return 0;
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<"\n";
    }
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;

class BFS {
private:
    int color[MAX];
    int n;
    int M[MAX][MAX];
    queue<int> Q;
    int distance[MAX];

public:
    BFS()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                M[i][j] = 0;
        int vertex;
        int degree;
        for(int i = 0; i < n; i++)
        {
            color[i] = WHITE;
            cin >> vertex;
            cin >> degree;
            int to_v;
            for(int j = 0; j < degree; j++)
            {
                cin >> to_v;
                M[vertex - 1][to_v - 1] = 1;
            }
        }
    }

    void bfs()
    {
        for(int i = 0; i < n; i++)
            distance[i] = -1;

        distance[0] = 0;
        Q.push(0);
        color[0] = GRAY;
        while(Q.size() != 0)
        {
            int u = Q.front();
            Q.pop();
            color[u] = BLACK;
            for(int v = 0; v < n; v++)
            {
                if(M[u][v] && color[v] == WHITE)
                {
                    distance[v]  = distance[u] + 1;
                    Q.push(v);
                    color[v] = GRAY;
                }
            }
        }
    }

    void print()
    {
        for(int i = 0; i < n; i++)
            cout << i + 1 << " " << distance[i] << endl;
    }
};

int main()
{
    BFS bfs;
    bfs.bfs();
    bfs.print();
}

C++: #include <iostream>
#include <stdio.h>
#include <queue>
#define N 101
#define INFTY (1<<21)


int n;
int M[N][N];
int d[N];

void bfs(int u) {
    std::queue<int> Q;
    Q.push(u);
    for (int i = 0; i < n; i++) d[i] = INFTY;
    d[u] = 0;
    while ( ! Q.empty() ) {
        int j = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[j][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[j] + 1;
            Q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}

int main() {
    int u, k, v;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) M[i][j] = 0;
    }
    
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        u--;
        if (k == 0) continue;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
 
#define N 100
#define INFTY 1000000
 
int M[N][N],d[N];
int n,u,k,v;
 
//bfs
void bfs(int s){
 
  queue <int> Q;
  //十分大きな数字に初期化
  for(int i = 0; i < N; i++ ) d[i] = INFTY;
  
  //最初の訪問先
  d[s] = 0;
  Q.push(s);

  //Qが空でないとき
  while ( !Q.empty() ){

    //queueから出す
    u = Q.front(); Q.pop();

    for( int v = 0; v < n; v++ ){

      //つながってないとき
      if( M[u][v] == 0 ) continue;

      //既に訪問ずみのとき
      if( d[v] != INFTY ) continue;

      //時間を入力
      d[v] = d[u] + 1;

      //queueに追加
      Q.push(v);
    }
  }
 
  //出力
  for( int i = 0; i < n; i++ ){
    cout << i+1 << " ";
    if ( d[i] == INFTY ) cout << -1;
    else cout << d[i];
    cout << endl;
  }
 
}
 
 
int main(){

  //初期化
  for( int i = 0; i < n; i++ ){
    for( int j = 0; j < n; j++ ) M[u][v] = 0;
  }
  
  //入力
  cin >> n;
  for( int i = 0; i < n; i++ ){
    cin >> u;
    u--;
    cin >> k;
    for( int j = 0; j < k; j++ ){
      cin >> v;
      v--;
      //つながっているとき
      M[u][v] = 1;
    }
  }
  
  //bfs
  bfs(0);
   
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

vector<int> d(101, -1);
vector<vector<int> > c(101, vector<int>(101, 0));

int n, u, k, v;
typedef pair<int, int> P; 
int bfs(void) {
    queue<P> que;
    que.push(P(0, 0));
    while(que.size()>0) {
        P p = que.front(); que.pop();
        if(d[p.first]==-1) d[p.first] = p.second;
        for(int i=0; i<n; ++i) {
            if(c[p.first][i]==1 && d[i]==-1) que.push(P(i, d[p.first]+1));
        }
    }
}

int main() {
    cin >> n;
    for(int i=0; i<n; ++i) {
        cin >> u >> k;
        for(int j=0; j<k; ++j) {
            cin >> v;
            c[i][v-1] = 1;
        }
    }
    bfs();
    for(int i=0; i<n; ++i) {
        cout << i+1 << " " << d[i] << endl;
    }
    return 0;
}