Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;

int BFS(int s, int t, const vector<vector<int> >& G){
    vector<int> stock1;
    vector<int> stock2;
    vector<int> *s1p = &stock1;
    vector<int> *s2p = &stock2;
    stock1.push_back(s);
    int d = 0;
    while (1) {
        for (int i = 0; i < s1p->size(); i++) {
            if ((*s1p)[i] == t) return d;
            for (int j = 0; j < G[(*s1p)[i]].size(); j++) {
                s2p->push_back(G[(*s1p)[i]][j]);
            }
        }
        if (s1p == &stock1) {
            s1p = &stock2;
            s2p = &stock1;
        } else {
            s1p = &stock1;
            s2p = &stock2;
        }
        d++;
    }
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << i << " " << BFS(1, i, G) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#define MAX 101
#define NIL -1

using namespace std;


int matrix[MAX][MAX] = {0};
int dis[MAX];
//提交时须对time改名 
int n;


void bfs_visit(int u);
int next(int u);

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			matrix[u][v] = 1;			
		}
	}
	
//显示邻接矩阵 
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			if(j) cout << ' ';
//			cout << matrix[i+1][j+1];		
//		}
//		cout << endl;
//	}

	bfs_visit(1);

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



void bfs_visit(int u)
{
	
	//dis[]初始化，取值NIL时表示未访问 
	for (int i = 1; i <= n; i++)
		dis[i] = NIL;	
	dis[u] = 0;	
	
	queue<int> Q;
	Q.push(u);
	
//	int d = 1;
//	while (!Q.empty())
//	{
//		u = next(Q.front());
//
//		if (u)
//		{
//			dis[u] = d; 
//			Q.push(u);
//		}
//		else
//		{
//			Q.pop();			
//		}
//		
//		//判断是否进入下一层 
//		if (dis[Q.front()] == d) d++;
//
//	}

//另一种实现 
	while (!Q.empty())
	{
		u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++)
		{
			if (matrix[u][v] != 0 && dis[v] == NIL)
			{
				dis[v] = dis[u] + 1;
				Q.push(v);
			}
		}
	} 
}


int next(int u)
{
	for (int i = 1; i <= n; i++)
	{
		if (matrix[u][i] != 0 && dis[i] == NIL)
			return i;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100,INFTY=(1<<21);

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

void bfs(int s) {
  queue<int> q;
  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,v,k,i,j;
  cin>>n;
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) {
      M[i][j]=0;
    }
  }
  for (i=0; i<n; i++) {
    cin>>u>>k;
    u--;
    for (j=0; j<k; j++) {
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}