Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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

using namespace std;

void read_data(int n, vector<vector<int>> & Es){
    int u = 0, k = 0, v = 0;
    while (n--){
        cin >> u;
        cin >> k;
        while (k--){
            cin >> v;
            Es[u - 1].push_back(v - 1);
        }
    }
}

void wfs(int node, vector<int> & dist, vector<vector<int>> & Es){
    int d = 0;
    vector<int> frontiers;
    frontiers.push_back(node);
    while (!frontiers.empty()){
        vector<int> new_frontiers;
        for (auto u : frontiers){
            if (dist[u] == -1){
                dist[u] = d;
                for (auto v : Es[u]){
                    new_frontiers.push_back(v);
                }
            }
        }
        frontiers = move(new_frontiers);
        ++d;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    vector< vector<int> > Es(n);
    vector<int> dist(n, -1);
    read_data(n, Es);
    wfs(0, dist, Es);
    for (int i = 0; i != n; ++i) cout << i + 1 << ' ' << dist[i] << endl;
    
    return 0;
}

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

#define N 100
#define MAX 100000

int A[N + 1][N + 1] = {};
int n, u, k, v;
int t1[N + 1]; //発見時刻

//sは始点,tは時間
void bfs(int s, int t) {
    t1[s] = t;
    for(int i = 1; i <= n; ++i) {
        if(A[s][i] == 1 && t < t1[i]) {
            bfs(i, t + 1);
        }
    }
}

int main(void) {
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        cin >> u >> k;
        for(int j = 0; j < k; ++j) {
            cin >> v;
            A[u][v] = 1;
        }
    }
    
    for(int i = 0; i <= n; ++i) t1[i] = MAX;
    
    bfs(1, 0);
    
    for(int i = 1; i <= n; ++i) {
        cout << i << ' ';
        if(t1[i] == MAX) cout << -1 << endl;
        else cout << t1[i] << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, u, k, vi;
    std::cin >> n;
    std::vector<std::vector<int>> v(n+1);
    std::queue<int> ex;
    ex.push(1);
    int distance[101]={};
    for (int i=2; i<101; i++) {
        distance[i] = -1;
    }
    for (int i=0; i<n; i++) {
        std::cin >> u >> k;
        for (int j=0; j<k; j++) {
            std::cin >> vi;
            v[u].push_back(vi);
        }
    }

    while (not ex.empty()) {
        int a = ex.front();
        ex.pop();
        for (int i=0; i<v[a].size(); i++) {
            if (distance[v[a][i]] == -1) {
                ex.push(v[a][i]);
                distance[v[a][i]] = distance[a] + 1;
            }
        }
    }

    for (int i=1; i<=n; i++) {
        std::cout << i << " " << distance[i] << "\n";
    }

    return 0;
}

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

const int Max = 100;
int map[Max + 1][Max + 1];

typedef struct{
	bool startflag;
	int distance;
}State;

int main()
{
	int n, k, l, temp;
	std::queue<int> q;
	std::vector<State> state;

	for (int i = 1; i < Max + 1; i++){
		for (int j = 1; j < Max + 1; j++){
			map[i][j] = 0;
		}
	}

	std::cin >> n;
	for (int i = 1; i <= n; i++){
		std::cin >> k >> l;
		for (int j = 1; j <= l; j++){
			std::cin >> temp;
			map[k][temp] = 1;
		}
	}

	State s = { false, 0 };
	for (int i = 0; i <= n; i++){
		state.push_back(s);
	}

	int d = 0;
	while (1){
		if (q.empty() == true){
			bool f = false;
			for (int i = 1; i <= n; i++){
				if (state[i].startflag == false){
					if (i != 1){
						state[i].startflag = true;
						state[i].distance = -1;
						continue;
					}
					q.push(i);
					state[i].startflag = true;
					d = 0;
					state[i].distance = d;
					d++;
					f = true;
					break;
				}
			}
			if (f == false){
				break;
			}
		}
		int size = q.size();
		for (int j = 0; j < size; j++){
			for (int i = 1; i <= n; i++){
				if (map[q.front()][i] == 1 && state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					state[i].distance = d;
				}
			}
			q.pop();
		}
		d++;
	}

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

	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();   //front:キューから取り出し、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;   //u:頂点番号、k:uの出次数、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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int m[100][100];
int d[100];
int n;
const int INF = 1e8;

void bfs(int u)
{
    queue<int> q;
    q.push(u);

    d[u] = 0;

    while (!q.empty())
    {
        int t = q.front();
        q.pop();

        rep(i, n)
        {
            if (m[t][i] == 0 || d[i] != INF)
                continue;
            d[i] = d[t] + 1;
            q.push(i);
        }
    }
}

int main()
{
    cin >> n;
    rep(i, n) d[i] = INF;

    rep(i, n) rep(j, n) m[i][j] = 0;

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

    bfs(0);

    rep(i, n)
    {
        if (d[i] == INF)
            cout << i + 1 << " " << -1 << endl;
        else
            cout << i + 1 << " " << d[i] << endl;
    }

    return 0;
}