Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 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;
}