Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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<100;i++){
        for(int j=0;j<100;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";

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

    return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];  // 距離で訪問状態（color）を管理する

void bfs(int s) {
  queue<int> q;  // 標準ライブラリの queue を使用
  q.push(s);
  for ( int i = 0; i < n; i++ ) d[i]  = INFTY;
  d[s] = 0;
  int u;
  while ( !q.empty() ) {
    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;
      q.push(v);
    }
    for ( int i = 0; i < n; i++ )
      cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;

  cin >> 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++ ) {
    cin >> u >> k;
    u--;
    for ( int j = 0; j < k; j++ ) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

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

using namespace std;

void bfs();

const int Infty = 20000000;

bool **M;
int *dist,n;
string *color;
queue<int> Q;

int main(){
	cin>>n;
	M = new bool*[n]; for(int i=0;i<n;i++) M[i] = new bool[n];
	color = new string[n]; dist = new int[n];
	
	for(int i=0;i<n;i++) M[i] = M[i]-1; M = M-1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++) M[i][j] = false;
	}
	color = color-1; dist = dist-1;

	int vertex,deg,edge;
	for(int i=1;i<=n;i++){
		cin>>vertex>>deg;
		for(int j=1;j<=deg;j++){
			cin>>edge;
			M[vertex][edge] = true;
		}
	}

	bfs();

	for(int i=1;i<=n;i++){
		cout<<i<<" ";
		if(dist[i]!=Infty)
			cout<<dist[i]<<endl;
		else
			cout<<-1<<endl;
	}

	for(int i=1;i<=n;i++) delete [] (M[i]+1); delete [] (M+1);
	delete [] (color+1); delete [] (dist+1);
}

void bfs(){
	for(int i=1;i<=n;i++){
		color[i] = "WHITE"; dist[i] = Infty;
	}

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

C++: #include<bits/stdc++.h>

using namespace std;

#define rep(a, b) for(int i = a; i <= b; i++)
#define mst(a) memset(a, 0, sizeof(a))

const int MAX = 1e2 + 10;


bool mtx[MAX][MAX];
int vis[MAX];
int n;

queue<int> q;

void init(void)
{
	while(!q.empty())
		q.pop();
	mst(vis);
	return;
}

bool check(int x, int y)
{
	if(mtx[x][y] && !vis[y])
		return true;
	return false;
}

void GetAns(void)
{
	rep(2, n)
	{
		if(vis[i] == 0)
			vis[i] = -1;
	}
	return;
}

void Bfs(void)
{
	int cnt = 1;
	q.push(1);
	while(!q.empty())
	{
		int cur = q.front();
		q.pop();
		rep(1, n)
		{
			if(check(cur, i))
			{
				q.push(i);
				vis[i] = vis[cur] + 1;
				cnt++;
				if(cnt == n)
					return;
			}
		}
	}
	return;
}

int main()
{
	while(~scanf("%d", &n))
	{
		init();
		rep(1, n)
		{
			int row;
			scanf("%d", &row);
			int num;
			scanf("%d", &num);
			while(num--)
			{
				int tmp;
				scanf("%d", &tmp);
				mtx[row][tmp] = true;
			}
		}
		
		Bfs();

		GetAns();
		
		rep(1, n)
			printf("%d %d\n", i, vis[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void bfs( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] < 1 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}

int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		D[i] = -1;
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}