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) {
			if !dl.isVisited(v) {
				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++: #include<bits/stdc++.h>
using namespace std;
static const int N=100;
enum COLOR{WHITE,GRAY,BLACK};
static const int 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;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)M[i][j]=0;
    }

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

    bfs(0);
 	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define int long long
 	
const int N = 100 + 1;
vector< int > g[N];
int d[N]; // stores the depth of all nodes

void BFS(int u){
   queue<int> q;
   q.push(u);
   d[u] = 0;
   while (!q.empty()){
	   int x = q.front(); q.pop();
	   for(auto it : g[x]){
		     if (d[it] != -1) continue;
		     d[it] = d[x] + 1;
		     q.push(it);
		 }  
	 }
}

int32_t main(){
   int n, m, deg;
   cin >> n;
   for (int i = 0; i < n; i++){
	    cin >> m >> deg;
	    m--;
	    for(int i = 0; i < deg; i++){
		    int x;
		    cin >> x;
		    x--;
		    g[m].push_back(x);
	    }  
	 }
	for (int i = 0; i < n; i++) d[i] = -1;
    BFS(0);
    for (int i = 0; i < n; i++){
	   int idx = i + 1;
	   if (d[i] == -1) cout << idx << " " << -1 << endl;
	   else cout << idx << " " << d[i] << endl; 
	 }
}

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:INF;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==INF){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

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

C++: #include <queue>
#include <vector>
#include <iostream>
using namespace std;
int N;
int D[110];
int G[110][110];
void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();
		for(int i=0;i<N;i++){
			if(G[cur][i] && D[i]==0){
				D[i] = D[cur]+1;
				Q.push(i);
			}
		}
	}
	for(int i=0;i<N;i++){
		if(i!=src && D[i]==0) D[i]=-1;
	}
	D[src]=0;
}

int main(){
	while(cin >> N && N>0){
		vector<vector<int> > g(110,vector<int>(110,0));
		int u,k;
		for(int i=0;i<N;i++){
			cin >> u >> k;
			for(int j=0;j<k;j++){
				int a;
				cin >> a;
				g[u-1].push_back(a); 
			}
		}
		for(int i=0;i<N;i++){
			for(int j=0;j<g[i].size();j++){
				int f = g[i][j];
				G[i][f-1] = 1;
			}
		}
		bfs(0);
		for(int i=0;i<N;i++){
			cout << i+1 << " " << D[i] << endl;
		}
	}
}

#if 0
無限ループに陥らないようにするのに少し苦労した。プログラム上では番号を0から始めたのもよくないかもしれない。
#endif

C++: #include<stdio.h>
#include<queue>
#include<algorithm>

#define MAX 100

int G[MAX+1][MAX+1], d[MAX+1];
int n;
std::queue<int> Q;

void bfs() {
    d[1] = 0;

    Q.push(1);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 1; i <= n; ++i) {
            if (G[cur][i] == 0) continue;
            if (d[i] != -1) continue;

            Q.push(i);
            d[i] = d[cur] + 1;
        }
    }

}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        d[i] = -1;
        for (int j = 1; j <= n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; ++j) {
            int t;
            scanf("%d", &t);
            G[u][t] = 1;
        }
    }

    bfs();
    for (int i = 1; i <= n; ++i) {
        printf("%d %d\n", i, d[i]);
    }

}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

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

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << i+1 << " " << D[i] << endl;
    }

    return 0;
}