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<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<k;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 <cstdio>
#include <queue>
using namespace std;
#define INF (1<<21)

int n, G[100][100], dist[100], visited[100];


int main(void) {
    int u, k, v, i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) G[i][j] = 0;

    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        u--;
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            G[u][v] = 1;
        }
    }
    
    for (i = 1; i < n; i++) {
        dist[i] = INF;
        visited[i] = 0;
    }
    dist[0] = 0;
    visited[0] = 1;

    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (G[u][i] == 0 || visited[i] == 1) continue;
            q.push(i);
            visited[i] = 1;
            dist[i] = dist[u] + 1;
        }
    }

    for (i = 0; i < n; i++) {
        if (dist[i] == INF) dist[i] = -1;
        printf("%d %d\n", i + 1, dist[i]);
    }

    return 0;
}

C++: //Breadth First Search
#include <iostream>
#include <queue>

using namespace std;

static const int N = 100;

int d[N];

void bfs(vector<vector<int>> &graph, int u) {
    queue<int> q;
    q.push(u);
    d[u] = 0;

    while(!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto w: graph[v]) {
            if(d[w] != -1) continue;
            q.push(w);
            d[w] = d[v] + 1;
        }
    }
}

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

    bfs(graph, 0);

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100

int n, M[N][N], d[N];

queue<int> Q;

void bfs(int x){
    //初期化
    rep(i,n){
        d[i] = -1;
    }

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

    while(!Q.empty()){
        int v = Q.front(); Q.pop();
        for(int i = 0; i < n; i++){
            if(M[v][i] == 1){
                if(d[i] == -1){
                    Q.push(i);
                    d[i] = d[v] + 1;
                }else{
                    d[i] = min(d[i], d[v] + 1);
                }
                
            }
        }
    }
}


int main(){
    cin >> n;

    rep(i,N)rep(j,N) M[i][j] = 0;

    int u, k, v;
    rep(i,n){
        cin >> u >> k;
        u--; //0オリジンに変換
        rep(j,k){
            cin >> v;
            v--; //0オリジンに変換
            M[u][v] = 1;
        }
    }

    bfs(0);

    rep(i,n){
        cout << i + 1 << " " << d[i] << endl;
    }

    return 0;
    
}

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

using namespace std;
int main(){
  int u, i, id, k, j;
  cin >> u;
  unordered_map<int, vector<int> > mymap;
  queue<vector<int> > myqueue;
  for (i = 0; i < u; ++i){
    cin >> id >> k;
    vector<int> points;
    for (j = 0; j < k; ++j){
      int a;
      cin >> a;
      points.push_back(a);
    }
    mymap[id] = points;
    if(id == 1){
      myqueue.push(points);
    }
  }
  
  bool visited[u+1];
  for (j = 1; j <= u; ++j){
    visited[j] = false;
  }
  visited[1] = true;

  int dist[u+1];
  for (j = 1; j <= u; ++j){
    dist[u] = -1;
  }
  dist[1] = 0;
  int d = 1;

  while(!myqueue.empty()){
    vector<int> combined;
    for (i = 0; i < myqueue.front().size(); ++i){
      if(!visited[myqueue.front()[i]]){
        dist[myqueue.front()[i]] = d;
        visited[myqueue.front()[i]] = true;
        if(combined.empty()){
          combined = mymap[myqueue.front()[i]];
        }else{
          combined.insert(combined.end(), mymap[myqueue.front()[i]].begin(), mymap[myqueue.front()[i]].end());
        }
      }
    }
    if(!combined.empty()){
      myqueue.push(combined);
    }
    myqueue.pop();
    d++;
  }

  for (j = 1; j <= u; ++j){
    cout << j << " " << dist[j] << endl;
  }

}

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

int main()
{
    queue<int> Q;
    int n;
    int a, b, c;
    cin >> n;
    int D[100], in[100][100];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}