Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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;
	}
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1<<30
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define RREP(i,a,b) for(ll i=(a); i>=(b); i--)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef int64_t in;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef pair<ll, ll> p;
typedef vector<pair<ll, ll>> vpl;

vvl m(105, vl(105,0));
vl d(105, INF);
ll n;

void bfs(ll s){
    queue<ll> q;
    q.push(s);
    d[s] = 0;
    ll u;
    while(!q.empty()){
        u = q.front(); q.pop();
        rep(v,n){
            if(m[u][v] == 1){
                if(d[v] == INF){
                    d[v] = d[u] + 1;
                    q.push(v);
                }
            }
        }
    }
    rep(i,n){
        if(d[i] != INF) cout << i+1 << " " << d[i] << endl; 
        else cout << i+1 << " " << -1 << endl;
    }
}

int main(){
    ll u,k,v;
    cin >> n;
    rep(i,n){
        cin >> u >> k;
        rep(j,k){
            cin >> v;
            m[u-1][v-1] = 1;
        }
    }
    bfs(0);
    
    return 0;
}