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 <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAX_V 110

int main() {
    
    vector<vector<int>> G(MAX_V);
    int n; cin >> n;
    for(int i=0; i<n; i++){
        int v, k; cin >> v >> k;

        for(int j=0; j<k; j++){
            int u; cin >> u;
            G[v].push_back(u);
        }
    }

    vector<int> dist(MAX_V, -1);
    dist[1] = 0;

    queue<int> q;
    q.push(1);
    while(!q.empty()){

        int v = q.front();
        q.pop();

        for(int i=0; i<G[v].size(); i++){
            int u = G[v][i];
            if(dist[u] == -1){
                dist[u] = dist[v] + 1;
                q.push(u);
            }
        }
    }

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

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


#define ALREADY 1
#define YET 0

int *passed;
int *distance_ps;

using namespace std;


void bfs(vector< vector<int> > a, int i, int dis){
  // i?????????vector<int>??????????????¢?????????
  dis++;
  vector<int> ai = a[i];
  int n = ai.size();
  // cout << "i: " << i << ",n: " << n << endl;
  for(int j=0;j<n;j++){
    int nexti=ai[j]-1;
    
    if(passed[nexti]==YET || distance_ps[nexti]>dis){
      passed[nexti]=ALREADY;
      distance_ps[nexti]=dis;
      bfs(a, nexti, dis);
    }
  }
}

int main(){
  int n;
  cin >> n;
  vector< vector<int> > a;
  for(int i=0;i<n;i++){
    int u;
    int k;
    cin >> u >> k;
    vector<int> tmpa;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      tmpa.push_back(tmp);
    }
    a.push_back(tmpa);
  }
  
  int a2[n][n];
  passed = new int[n];
  distance_ps = new int[n];
  
  for(int i=0;i<n;i++){
    passed[i] = 0;
    distance_ps[i] = 1000;
  }

  distance_ps[0]=0;
  bfs(a, 0, 0);

  for(int i=0;i<n;i++){
    if(passed[i]==YET){
      distance_ps[i] = -1;
    }
  }
  distance_ps[0]=0;
  
  for(int i=0;i<n;i++){
    cout << i+1 << " " << distance_ps[i] << endl;
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      a2[i][j]=0;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<(int)a[i].size();j++){
      a2[i][a[i][j]-1] = 1;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      // if(j!=0)cout << " ";
      // cout << a2[i][j];
    }
    // cout << endl;
  }
      
  
}

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

template<class T>
struct Edge{
	int from,to;
	T cost;
	Edge(int to,T cost) : to(to),cost(cost){}
	Edge(int from,int to,T cost) : from(from),to(to),cost(cost){}
};

template<class T>
using WeightedGraph = vector<vector<Edge<T>>>;
using Graph = vector<vector<int>>;
template<class T>
using Matrix = vector<vector<T>>;

template<class T = int>
vector<T> BFS(const Graph &G,int source = 0){
	vector<T> dist(G.size(),-1);
	queue<int> que;
	dist[source] = 0;
	que.push(source);
	for(T d = 0;;d++){
		int siz = que.size();
		if(siz == 0) break;
		for(int i = 0;i < siz;i++){
			int v = que.front();que.pop();
			for(int to : G[v]){
				if(dist[to] == -1){
					dist[to] = d + 1;
					que.push(to);
				}
			}
		}
	}
	return dist;
}

signed main(){
	int n;
	cin >> n;
	Graph G(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;
			G[u - 1].push_back(v - 1);
		}
	}
	auto dist = BFS(G,0);
	for(int i = 0;i < n;i++) cout << i + 1 << " " << dist[i] << endl;
}

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

const int INF = 1001001001;
int n;
int v[100][100];
int d[100]; //?§??????????????????¢

void bfs(int s) //s????§????
{
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) d[i] = INF;

  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();

    for (int i = 0; i < n; i++) {
      if (v[u][i] == 0) continue;
      if (d[i] != INF) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }

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

signed main()
{
  int u, k;

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

  bfs(0);

  return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
void main_sub(void);
int main(int argc, char* argv[]) {
    main_sub();
    return 0;
}
// int を int64_t にする
// g++ に -DUSE_STRICT_INT をつけるか、
// 下のコメントアウトを外すと無効化
// #define USE_STRICT_INT
using i64 = int64_t;
#ifndef USE_STRICT_INT
#define int i64
#endif
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = (1 << 30);
constexpr i64 INFL = (1LL << 62);
constexpr i64 MOD = 1000000007;

void main_sub(void) {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, n) {
        int u, k;
        cin >> u;
        u--;
        cin >> k;
        rep(j, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    queue<pair<int, int>> q;
    vector<int> dist(n, INF);
    q.push({0, 0});
    while (!q.empty()) {
        int from, d;
        tie(from, d) = q.front();
        q.pop();
        if (dist[from] > d) {
            dist[from] = d;
        }
        for (auto& to : g[from]) {
            if (dist[to] > d + 1) {
                dist[to] = d + 1;
                q.push({to, d + 1});
            }
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << (dist[i] == INF ? -1 : dist[i]) << endl;
    }

    return;
}