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 <cstdio>
#include <queue>
using namespace std;
#define MAX_N 100

int g[MAX_N+1][MAX_N+1];
int d[MAX_N+1];
bool visited[MAX_N+1];
queue<int> que;

int n;
int t = 1;

void bfs(int id) {
  visited[id] = true;
  que.push(id);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    queue<int> next_que;
    for(int u=1;u<=n;u++) {
      if(g[v][u]!=0 && !visited[u]) {
        visited[u]=true;
        next_que.push(u);
        d[u] = t;
      }
    }
    if(que.empty()){
      que = next_que;
      t++;
    }
  }
}

int main() {
  scanf("%d\n",&n);
  int u,k,v;
  for(int i=0;i<n;i++) {
    scanf("%d %d ",&u,&k);
    for(int j=0;j<k;j++) {
      scanf("%d ",&v);
      g[u][v] = 1;
    }
  }
  bfs(1);
  for(int i=1;i<=n;i++) {
    if(visited[i])
      printf("%d %d\n",i,d[i]);
    else
      printf("%d -1\n",i);
  }
}

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

void BFS(int s, const vector<vector<int> >& G, vector<int>& D){
    queue<int> Q;
    Q.push(s);
    D[s] = 0;
    while (!Q.empty()) {
        int f = Q.front();
        for (int i = 0; i < G[f].size(); i++) {
            if (D[G[f][i]] == -1) {
                D[G[f][i]] = D[f] + 1;
                Q.push(G[f][i]);
            }
        }
        Q.pop();
    }
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    vector<int> D(n + 1, -1);
    BFS(1, G, D);
    for (int i = 1; i <= n; i++) {
        cout << i << " " << D[i] << endl;
    }
    return 0;
}

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

bool isprime(int n){
    double rootn = sqrt(n);
    if (n < 2){
        return false;
    }
    else if (n == 2){
        return true;
    }
    else if (n % 2 == 0){
        return false;
    }
    else{
        for(int i = 3; i <= rootn; i += 2){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
}

// 空のベクトルを二つ渡す。N (>=2) 以下の素数を primes に順番に入れる。
void prime_vectors(int N, vector<int>& primes, vector<bool>& is_prime){
    primes = {};
    is_prime = {};
    for(int i=0; i<=N; i++){
        is_prime.push_back(true);
    }

    primes.push_back(2);
    for(int j=4; j<=N; j+=2){
        is_prime[j] = false;
    }

    int i;
    for(i=3; i*i<=N; i+=2){
        if(!is_prime[i]) continue;
        primes.push_back(i);
        for(int j=2*i; j<=N; j+=i){
            is_prime[j] = false;
        }
    }

    for(; i<=N; i+=2){
        if(is_prime[i]){
            primes.push_back(i);
        }
    }
}

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int n;
    cin >> n;
    vector<vector<int>> e(n);
    for(int i=0; i<n; i++){
        int u, k;
        cin >> u >> k;
        for(int j=0; j<k; j++){
            int v;
            cin >> v;
            e[i].push_back(v-1);
        }
    }

    int d[n];
    for(int i=0; i<n; i++){
        d[i] = -1;
    }

    queue<int> q;
    q.push(0);
    d[0] = 0;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        for(int i=0; i<e[v].size(); i++){
            if(d[e[v][i]] == -1){
                d[e[v][i]] = d[v] + 1;
                q.push(e[v][i]);
            }
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

void bfs(const vvi& g, vi& ds, int i) {
    int n = g.size();

    vector<bool> seen(n, false);
    queue<int> queue;
    queue.push(i);
    ds[i] = 0;
    seen[i] = true;

    while (!queue.empty()) {
        int v = queue.front();
        queue.pop();

        for (auto itr = g[v].begin(); itr != g[v].end(); ++itr) {
            if (!seen[*itr]) {
                queue.push(*itr);
                ds[*itr] = ds[v] + 1;
                seen[*itr] = true;
            }
        }
    }

    rep(i, 0, n) {
        if (!seen[i]) { ds[i] = -1; }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vvi g(n);
    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;

        rep(j, 0, k) {
            int v;
            cin >> v;
            g[u - 1].push_back(v - 1);
        }
    }

    vi ds(n, 0);

    bfs(g, ds, 0);
    rep(i, 0, n) {
        cout << i + 1 << " " << ds[i] << endl;
    }

    return 0;
}

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

int main(){
	vector<int> graph[100];
	queue<int> que;
	int d[100];
	int n;
	cin >> n;
	for(int i = 0;i < n;i++){
		int now,m;
		cin >> now >> m;
		for(int j = 0;j < m;j++){
			int t;
			cin >> t;
			graph[now - 1].push_back(t - 1);
		}
		d[i] = -1;
	}
	d[0] = 0;
	que.push(0);
	while(!que.empty()){
		int num = que.front();
		que.pop();
		for(int i = 0;i < graph[num].size();i++){
			if(d[graph[num][i]] == -1) {
				d[graph[num][i]] = d[num] + 1;
				que.push(graph[num][i]);
			}
		}
	}
	for(int i = 0;i < n;i++){
		cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}