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 <algorithm>
#include <queue>

struct Node {
	int ID;
	int d;
	bool visited;
	bool operator<(const Node& rhs) {return this->ID < rhs.ID;}
	bool operator>(const Node& rhs) {return this->ID > rhs.ID;}
	bool operator<=(const Node& rhs) {return this->ID <= rhs.ID;}
	bool operator>=(const Node& rhs) {return this->ID >= rhs.ID;}
	bool operator==(const Node& rhs) {return this->ID == rhs.ID;}
	bool operator!=(const Node& rhs) {return this->ID != rhs.ID;}
};

void BFS(std::vector<int>* adjlist, Node* nodes, std::queue<int> que)
{	
	nodes[que.front()].d = 0;
	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
			if (!nodes[*it].visited) { 
				que.push(*it);
				nodes[*it].d = nodes[id].d + 1;
				nodes[*it].visited = true;
			}
		}
	}
}

int main()
{
	int n; std::cin >> n;
	
	std::vector<int>* adjlist = new std::vector<int>[n];
	Node* nodes = new Node[n];
	
	std::queue<int> que;
	
	for (int i=0; i<n; i++) {
		nodes[i].visited = false; nodes[i].d = -1;
	}
	
	for (int i=0; i<n; i++) {
		int x;
		int deg;
		std::cin >> x >> deg;
		nodes[i].ID = x-1;
		for (int j=0; j<deg; j++) {
			int y;
			std::cin >> y;
			adjlist[x-1].push_back(y-1);
		}
	}
	
	std::sort(nodes, nodes + n);
	

	que.push(0); nodes[0].visited = true;
	BFS(adjlist, nodes, que);
	
	
	
	for (int i=0; i<n; i++) {
		std::cout << nodes[i].ID + 1 << " " << nodes[i].d << std::endl;
	}
	
	delete[] adjlist;
	delete[] nodes;
	
	return 0;
}

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

int graph[101][101];
int dis[101]; //距離を格納
bool check[101];//訪問済みチェック
int main(){
  int n;
  cin>>n;
  fill(dis,dis+101,INF);
  dis[1]=0;
  check[1]=true;
  for(int i=0;i<n;i++){
    int u,k,v;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      graph[u][0]=k;
      cin>>graph[u][j];
    }
  }
  queue<int> qu;
  qu.push(1);
  while(!qu.empty()){
    int num=qu.front(); qu.pop();
    for(int j=1;j<=graph[num][0];j++){
      int temp=graph[num][j];
      if(check[temp]){//訪問済みならば
        continue;
      }else{
        check[temp]=true;
      }
      dis[temp]=dis[num]+1;
      qu.push(temp);
    }
  }
  //表示部
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]>=INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

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

int D[101]; 
int edges[101][101];

void bfs(int src,int n)
{
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while(!Q.empty())
	{
		int cur=Q.front();
		Q.pop();
		for(int dst=1;dst<=n;dst++)
		{
			int flag=0;
			for(int i=0;i<n;i++)
			{
				if(edges[cur][i]==dst)
				{
					flag=1;
					break;
				}
			}
			if(D[dst]==-1&&flag)
			{
				D[dst]=D[cur]+1;
				Q.push(dst);
			}
		}
	}
}

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		int u,k;
		cin >> u >> k;
		for(int j=0;j<k;j++)
		   cin >> edges[u][j];
	}
	for(int i=1;i<=n;i++)
	   D[i]=-1;
	bfs(1,n);
	for(int i=1;i<=n;i++)
	   cout << i << ' ' << D[i] << endl;
}

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

using namespace std;

enum COLOR {
  WHITE,
  GRAY,
  BLACK,
};

static const int INFTY = INT_MAX;
static const int N = 100;

static int M[N][N] = {};
static vector<COLOR> color(N, WHITE);
static vector<int> d(N, INFTY);

static void bfs(int n, int s) {
  color[s] = GRAY;
  d[s] = 0;

  queue<int> Q;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] && color[v] == WHITE) {
          color[v] = GRAY;
          d[v] = d[u] + 1;
          Q.push(v);
      }
      color[u] = BLACK;
    }
  }
}

int main() {
  int n;
  cin >> n;

  int number, count, value;
  for (int i = 0; i < n; i++) {
    cin >> number >> count;
    number--;
    for (int j = 0; j < count; j++) {
     cin >> value;
     value--;
     M[number][value] = 1; 
    }
  }
  
  bfs(n, 0);
  
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? -1 : d[i]) << endl; 
  }

  return 0;
}

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

#define INF 100000000
int n;
int Graph[100][100] = {};

int color[100] = {}, d[100];

void bfs(int x){
  int u;
  queue<int> Q; //queueを生成
  Q.push(x); //始点をキューに積む
  for(int i = 0 ; i < n ; i++){
    d[i] = INF;
  }

  d[x] = 0; //始点を0にする
  
  while(!Q.empty()){
    u = Q.front(); //取り出して削除
    Q.pop();
    for(int v = 0 ; v < n ; v++){
      if(Graph[u][v] == 0) continue;
      if(d[v] != INF) continue;
      d[v] = d[u] + 1;
      Q.push(v); //次点を積む
    }
  }

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


int main(){
  int u, k, v; //u = 頂点番号 k = 出次数 v = u に隣接する頂点番号

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

  bfs(0);
     
  return 0;
}

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


class Graph {
  public:
    Graph(int n) : n(n) {
        adj_arr = std::vector<std::vector<int>>(n);
        depth = std::vector<int>(n, 0);
    }

    void breadth_search(int i, int d) {
        std::queue<int> queue;
        depth[i] = d;
        
        for (auto && v : adj_arr[i]) {
            if (depth[v - 1] == 0 || depth[v - 1] > d) {
                queue.push(v - 1);
            }
        }

        while (!queue.empty()) {
            int v = queue.front();
            queue.pop();
            breadth_search(v, d + 1);
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i + 1 << " " << depth[i] << std::endl;
        }
    }

  public:
    std::vector<std::vector<int>> adj_arr;
    std::vector<int> depth;
    
  private:
    int n;
};


int main () {
    int n;
    std::cin >> n;

    Graph graph(n);

    for (int i = 0; i < n; i++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> arr(k);
        
        for (int j = 0; j < k; j++) {
            std::cin >> arr[j];
        }
        graph.adj_arr[u - 1] = arr;
    }

    graph.breadth_search(0, 0);
    graph.print();
}