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>
#include <cstring>

using namespace std;

int n, t;
vector<int> G[101];
int d[101];

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

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int u, k; cin >> u >> k;
		while (k--) {
			int v; cin >> v;
			G[u].push_back(v);
		}
	}

	memset(d, -1, sizeof(d));
	bfs(1);

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

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <cstdlib>

const int MAX_N = 100;

struct Node {
  int v;
  int d;
  bool visited;
};

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

  int n = 0;
  std::cin >> n;

  std::vector<std::vector<int> *> *graph = new std::vector<std::vector<int> *>(n + 1);
  std::vector<struct Node *> *nodes = new std::vector<struct Node *>(n + 1);
  
  for (int i = 1; i <= n; i++) {
    int v = 0;
    std::cin >> v;

    int k = 0;
    std::cin >> k;

    std::vector<int> *neighbors = new std::vector<int>(k);

    for (int i = 0; i < k; i++) {
      int a = 0;
      std::cin >> a;

      neighbors->at(i) = a;
    }

    graph->at(i) = neighbors;

    struct Node *node = (struct Node *) std::malloc(sizeof(struct Node));

    node->v = i;
    node->d = -1;
    node->visited = false;
    
    nodes->at(v) = node;
  }

  int count = 0;
  int start = 1;

  std::list<int> *stack = new std::list<int>();

  stack->push_back(nodes->at(start)->v);

  while (!stack->empty()) {
    std::vector<int> *newNodes = new std::vector<int>();

    while (!stack->empty()) {    
      int v = stack->front();
      stack->pop_front();

      nodes->at(v)->d = count;
      nodes->at(v)->visited = true;
      
      std::vector<int> *neighbors = graph->at(v);

      for (std::vector<int>::iterator iterator = neighbors->begin(); iterator != neighbors->end(); ++iterator) {
	struct Node *node = nodes->at(*iterator);

	if (!node->visited) {
	  newNodes->push_back(node->v);
	}
      }
    }

    for (std::vector<int>::iterator iterator = newNodes->begin(); iterator != newNodes->end(); ++iterator) {
      stack->push_front(*iterator);
    }

    count++;
  }

  for (std::vector<struct Node *>::iterator iterator = nodes->begin() + 1; iterator != nodes->end(); ++iterator) {
    struct Node *node = *iterator;
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}

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

using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<vector<int> > a(n+1);
    for(int i = 1; i <= n; i++){
        int id, size;
        cin >> id >> size;
        a[i].resize(size);
        for(int j = 0; j < size; j++){
            int temp;
            cin >> temp;
            a[i][j] = temp;
        }
    }
    vector<int> d(n+1);
    vector<bool> ok(n+1);
    queue<int> s;
    s.push(1);
    ok[1] = true;
    while(!s.empty()){
        int now = s.front();
        s.pop();
        for(int i = 0; i < a[now].size(); i++){
            int next = a[now][i];
            if(!ok[next]){
                d[next] = d[now] + 1;
                s.push(next);
                ok[next] = true;
            }
        }
    }
    for(int i = 2; i <= n; i++){
        if(d[i] == 0)d[i] = -1;
    }
    for(int i = 1; i <= n; i++){
        cout << i << " " << d[i] << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <queue>

int time, n;
int d[100], color[100];
int M[100][100];

void bfs(int s) {
    queue<int> Q;
    
    for (int i=0; i<100; i++) color[i] = 0:
    for (int i=0; i<100; i++) d[i] = -1;
    
    color[s] = 1;
    d[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (int i=0; i<n; i++) {
            if(M[u][i] && color[i] == 0) {
                color[i] = 1;
                d[i] = d[u]+1;
                Q.push(i);
            }
        }
        color[u] = 2;
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        for (int j=0; j<k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs(0);
    
    for (int i=0; i<n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
    return 0;
}

C++: 

#include <iostream>
#include<queue>
#include<string>
#define NIL -1
using namespace std;
string color[101];//白色未访问，灰色访问，黑色退出队列
queue<int >BreathFirstSearch;//创建队列
int Distance[101];
int AM[101][101];
int total;
void includeInsertQueue(int i)
{
	for (int j = 0; j <= total; j++)
	{
		if (AM[i][j] && color[j] == "White")//存在且未被访问
		{
			color[j] = "Grey", Distance[j] =Distance[i]+1;//找到的点距离是其上一个再加一
			BreathFirstSearch.push(j);
		}
	}
}
void includeFirstSearch(int i)
{
	int next;
	BreathFirstSearch.push(i);//压入起点
	Distance[i] =0;//起点到起点的距离为0；
	color[i] = "Grey";
	while (!BreathFirstSearch.empty())
	{
		next = BreathFirstSearch.front();//队列头中取出元素
		BreathFirstSearch.pop();
		color[next] = "Black";
		includeInsertQueue(next);

	}
}
int main()
{	int TotalOfConnectedNode, node[101], connectednode;
	for (int i = 0; i < 101; i++) Distance[i]= NIL;
	for (int i = 0; i < 101; i++)color[i] = "White";
	for (int i = 0; i < 101; i++)
		for (int j = 0; j < 101; j++)
			AM[i][j] = 0;
	cin >> total;
	for (int i = 0; i < total; i++)//有向图
	{
		cin >> node[i] >> TotalOfConnectedNode;
		if (TotalOfConnectedNode == 0) continue;
		for (int j = 0; j < TotalOfConnectedNode; j++)
		{
			cin >> connectednode;
			AM[node[i]][connectednode] = 1;
		}
	}
	includeFirstSearch(node[0]);
	for (int i = 0; i < total; i++)
		cout << node[i] << " " << Distance[node[i]] << endl;

	
	return 0;
}