Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

C++: #include <stdio.h>
#define INF 10000000 

int k[101]; 
int list[101][101]; 

int dist[101]; 

int n;

void walk(int id,int step){
	dist[id-1]=step;
	for(int i=0;i<k[id];i++){
		if(dist[list[id][i]-1]>step+1) walk(list[id][i],step+1);
	}
}

int main(void){
	int w;
	int num;

	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&num);
		scanf("%d",k+num);
		for(int j=0;j<k[num];j++) scanf("%d",&list[num][j]);
	}
	for(int i=0;i<n;i++) dist[i]=INF;
	walk(1,0);
	for(int i=1;i<=n;i++){
		if(dist[i-1]!=INF) printf("%d %d\n",i,dist[i-1]);
		else printf("%d -1\n",i);
	}
	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];

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++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <queue>
#include <limits.h>

using namespace std;
static const int NMAX = 100;
int N, D[NMAX] = {};
bool G[NMAX][NMAX] = { { false } };

void breadthFirstSearch(int base) {
	int ix;
	for (int i = 0; i < N; i++) {
		D[i] = INT_MAX;
	}

	D[base] = 0;
	queue<int> q;
	q.push(base);
	while (!q.empty()) {
		ix = q.front(); q.pop();
		for (int i = 0; i < N; i++) {
			if (!G[ix][i]) continue;
			if (D[i] != INT_MAX) continue;
			D[i] = D[ix] + 1;
			q.push(i);
		}
	}

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

int main() {
	int ix, deg, cache;
	cin >> N;

	for (int i = 0; i < N; i++) {
		cin >> ix >> deg;
		for (int j = 0; j < deg; j++) {
			cin >> cache;
			G[ix-1][cache-1] = true;
		}
	}

	breadthFirstSearch(0);

	return 0;
}

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

const int MAXV = 1010, MAXE = 1000000;

struct edge { int u, v, weight, next; } g[MAXE];


int n;
int head[MAXV], idx;

int parent[MAXV], visited[MAXV], dist[MAXV];

void bfs(int u){
    memset(parent, -1, sizeof(parent));
    memset(visited, 0, sizeof(visited));
    memset(dist, -1, sizeof(dist));
    
    queue<int> q;
    q.push(u);
    visited[u] = 1;
    dist[u] = 0;
    
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = head[u]; ~i; i = g[i].next) {
            int v = g[i].v;
            if (!visited[v]) {
                q.push(v);
                visited[v] = 1, parent[v] = u, dist[v] = dist[u] + 1;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    
 
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    idx = 0;
    memset(head, -1, sizeof(head));
    
    cin >> n;
    
    for (int i = 0, u, m, v; i < n; i++) {
        cin >> u >> m;
        for (int j = 0; j < m; j++) {
            cin >> v;
            g[idx] = edge{u, v, 0, head[u]};
            head[u] = idx++;
        }
    }
    
    bfs(1);
    
    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

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);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty())
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

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

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(j, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	REP(i, n)
	{
		REP(j, n)
		{
			cout << M[i][j];
		}
		cout << endl;
	}

	bfs(0);
	return 0;
}

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

int main() {
	int n;
	std::cin >> n;
	std::vector<int> d(n, -1);
	std::vector<int> dtmp(n, -1);
	std::vector<std::vector<int>> vec(n);
	for (int i = 0; i < n; i++) {
		int u, k;
		std::cin >> u >> k;
		while (k--) {
			int a;
			std::cin >> a;
			vec[i].push_back(a - 1);
		}
	}

	dtmp[0] = 0;
	for (int i = 0; i < n; i++) {
		d = dtmp;
		for (int idx = 0; idx < n; idx++) {
			if (d[idx] >= 0) {
				for (int j = 0; j < vec[idx].size(); j++) {
					if (dtmp[vec[idx][j]] == -1) {
						dtmp[vec[idx][j]] = i + 1;
					}
				}
			}
		}
	}

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