Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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

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

#define NO -1

using namespace std;


int adjacencyList[MAX + 1][MAX + 1];
int color[MAX + 1];
int d[MAX + 1];
int n;



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

	for (int i = 0; i < n; i++) {
		cin >> u >> k;

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

	}

	bfs(1);

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


	return 0;
}

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

#define INFTY (1 << 30)

void BFS(bool mT[][100], int firstVerVal, int verNum, int distance[]) {
	queue<int>  verQ;
	verQ.push(firstVerVal);
	for (int i = 0; i < verNum; i++)
		distance[i] = INFTY;
	distance[firstVerVal] = 0;

	while (!verQ.empty()) {
		int curVerVal = verQ.front();
		verQ.pop();
		for (int i = 0; i < verNum; i++) {
			if (!mT[curVerVal][i] || distance[i] != INFTY) continue;
			distance[i] = distance[curVerVal] + 1;
			verQ.push(i);
		}
	}
}

int main() {
	bool matrixTable[100][100];
	int distance[100];
	int verNum;
	cin >> verNum;

	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++)
			matrixTable[i][j] = false;
		
	for (int i = 0; i < verNum; i++) {
		int anyVerVal, anyVerNum;
		cin >> anyVerVal >> anyVerNum;
		for (int j = 0; j < anyVerNum; j++) {
			int verValue; cin >> verValue;
			matrixTable[anyVerVal - 1][verValue - 1] = true;
		}
	}

	BFS(matrixTable, 0, verNum, distance);

	for (int i = 0; i < verNum; i++) {
		cout << i + 1 << " ";
		if (distance[i] == INFTY)
			cout << -1 << endl;
		else
			cout << distance[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>
 
using namespace std;
 
int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離
 
void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] == -1){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}
 
int main(){
    scanf("%d", &N);
        int id, c;
        for (int i = 0; i < N; i++){
            scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
            scanf("%d", V + id);
            for (int j = 0; j < V[id]; j++){
                scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
                G[id][c] = 1;
            }
        }//データの読み込み完了。
    bfs(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];

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, 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<bits/stdc++.h>
/*
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>
*/
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

int main(){
    int n;
    cin >> n;
    int g[110][110];
    int i, j;
    int u, k, v;
    pair<int, int> p;
    int dist[110];
    bool visited[110];
     
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
     
    for(i = 0; i < n; i++){
        dist[i] = -1;
        visited[i] = false;
    }
     
    for(i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for(j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = 1;
        }
    }

	queue< pair<int, int> > q;
	for(i = 0; i < n; i++){
		if(g[0][i]) q.push(make_pair(0, i));
	}

	dist[0] = 0;
	visited[0] = true;
	while(!q.empty()){
		p = q.front();
		q.pop();

		if(visited[p.second]) continue;
		visited[p.second] = true;

		dist[p.second] = dist[p.first] + 1;

		for(j = 0; j < n; j++){
            if(g[p.second][j]){
                q.push(make_pair(p.second, j));
            }
        }
	}

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