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++: // 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]) {
				q.push(i);
				if (D[ix] + 1 < D[i]) D[i] = D[ix] + 1;
			}
		}
	}

	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 <iostream>
#include <queue>
#include <stdio.h>

using namespace std;

int n, u, k, v;
int adjList[101][101];
int adjNum[101];
bool visited[101];

int lv[101];

queue<int> q;

void bfs(int entry_node) {
    lv[entry_node] = 0;
    q.push(entry_node);

    while (!q.empty()) {
        u = q.front(); q.pop();
        visited[u] = true;

        for (int i = 0; i < adjNum[u]; i++) {
            if (!visited[adjList[u][i]]) {
                if (lv[adjList[u][i]] == -1) {
                    lv[adjList[u][i]] = lv[u] + 1;
                }
                q.push(adjList[u][i]);
            }
        }
    }

}

int main () {
	scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        adjNum[u-1] = k;
        for (int j = 0; j < k; ++j) {
            scanf("%d", &v);
            adjList[u-1][j] = v-1; // all IDs start from 1
        }

        visited[u-1] = false;
        lv[u-1] = -1; // set lv to -1 by default
    }

    bfs(0);
    for (int u = 0; u < n; ++u) {
        cout << u+1 << " " << lv[u] << "\n";
    }

	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], 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++: #include<iostream>
#include<queue>
using namespace std;
int M[101][101];
int n;
int d[101];

int search(int s){
    queue<int> Q;
    int u;

    Q.push(s);
    d[s]=0;

    while( !Q.empty() ){
	u=Q.front(); Q.pop();

	for(int i=1;i<=n;i++){
	    if(M[u][i]==0) continue;
	    if(d[i]>0) continue;
    
	    d[i]=d[u]+1;
	    Q.push(i);
	}

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

    cin >> n;
    for(int i=0;i<n;i++){
	cin >> u >> k;
	for(int j=0;j<k;j++){
	    cin >> v;
	    M[u][v]=1;
	}
    }

    search(1);

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

    return 0;
}

C++: #include <iostream>
#include <queue>
#define INFTY -1
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


  while(!q.empty()){
    u = q.front(); q.pop();
    for(int v = 0;v < n;v++){
      if(a[u][v] == 0) continue;
      if(d[v] == INFTY) {
        d[v] = d[u] + 1;
        q.push(v);
        //cout << v << " " << d[v] << endl;
      }
    }

  }
}

int main(){

  int u,k,x;
  cin >> n;

d[0] = 0;
for(int i = 1; i < n;i++) d[i] = INFTY;

  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> x;
      a[u - 1][x - 1] = 1;
    }
  }

  bfs(0);

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

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++: #include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;

class Vertex {
public:
    int num;
    bool flag;                  // 以前訪れたかどうか
    vector<int> adj;            // 隣接リスト
};

int dist[101];
void bfs(map<int, Vertex>& graph, Vertex s) {
    queue<Vertex> que;

    dist[s.num] = 0;
    que.push(s);
    while (!que.empty()) {
        Vertex u = que.front(); que.pop();
        for (auto i = 0; i < u.adj.size(); i++) {
            Vertex v = graph[u.adj[i]];
            if (!v.flag) {
                graph[u.adj[i]].flag = true;
                dist[v.num] = dist[u.num]+1;
                que.push(v);
            }
        }
    }
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < 101; i++) {
        dist[i] = -1;
    }

    map<int, Vertex> graph; // 頂点の番号->頂点
    for (int i = 0; i < n; i++) {
        Vertex v;
        int num, k;
        cin >> num >> k;
        v.flag = false;
        v.num = num;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            v.adj.push_back(tmp);
        }
        graph[num] = v;
    }

    bfs(graph, graph[1]);
    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }

    return 0;
}