Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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;
}

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

int n;
Graph G;

int main()
{
    cin >> n;
    G.resize(n);
    rep(i, n)
    {
        int id, k;
        cin >> id >> k;
        rep(j, k)
        {
            int v;
            cin >> v;
            G[i].push_back(v - 1);
        }
    }
    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    while (!que.empty())
    {
        int v = que.front();
        que.pop();
        for (int nv : G[v])
        {
            if (dist[nv] != -1)
                continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << dist[i] << endl;
    }
    return 0;
}

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

const int nIMPOS = -1;
struct StDir
{
  int m_nx;
  int m_nEtc;

  StDir(int nx) : m_nx(nx), m_nEtc(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<int>& rvnDst)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  rvnDst.resize(nxSiz + 1, nIMPOS);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();
    if (rvnDst[oDir.m_nx] == nIMPOS)  continue;

    for (int ny = 1; ny < cnrvvnAdjcy[oDir.m_nx].size(); ++ny)
      if (cnrvvnAdjcy[oDir.m_nx][ny] == 1)
      {
        int nDst = rvnDst[oDir.m_nx] + 1;
        if (rvnDst[ny] == nIMPOS)
          rvnDst[ny] = nDst;
        else
          rvnDst[ny] = min(rvnDst[ny], nDst);

        StDir oDir(ny);
        rqoBFSrch.push(oDir);
      }
  }
}

void fnResult(const vector<int>& cnrvnDst)
{
  for (int i = 1; i < cnrvnDst.size(); ++i)
    cout << i <<  " " << cnrvnDst[i] << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(vvnAdjcy, vnDst);

  vnDst[1] = 0;
  StDir oDir(1);
  qoBFSrch.push(oDir);
  fnBreadthFirstSearch(vvnAdjcy, qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;

static const int INFTY = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
int M[110][110] = {};
int color[110];
int d[110];
queue<int> Q;

void bfs(int s) {
  rep(u, n) color[u] = WHITE;
  rep(u, n) d[u] = INFTY;

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

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

int main() {
  cin >> n;
  rep(i, n) {
    int id, deg;
    cin >> id >> deg;
    --id;    // 0-indexedに直す
    rep(j, deg) {
      int v;
      cin >> v;
      --v;    // 0-indexedに直す
      ++M[id][v];
    }
  }
  bfs(0);
  rep(i, n) {
    cout << i + 1 << " ";
    if (d[i] != INFTY) cout << d[i];
    else cout << -1;
    cout << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
void bfs(int);
int n,M[N][N],d[N],u,i,j;
string color[N];
int main(){
  int inu,k,v;
  cin >> n;
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      M[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    cin >> inu >> k;
    for(j=0;j<k;j++){
      cin >> v;
      M[inu][v]=1;
    }
  }
  bfs(1);
  for(i=1;i<=n;i++){
    if(d[i]>1000000) cout << i << ' ' << -1 << endl;
    else cout << i << ' ' << d[i] << endl;
  }
  return 0;
}
void bfs(int s){
  queue<int> q;
  for(i=1;i<=n;i++){
    color[i]="WHITE";
    d[i]=1000000000;
  }
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=true){
    u=q.front();
    q.pop();
    for(i=1;i<=n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
}