Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: //
//  main.cpp
//  breadth_first_search
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>

#define n_max 100
#define INFTY 1000
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int M[n_max][n_max], color[n_max], d[n_max], n;

queue<int> Q;

void encode_graph(){
    int i, j, id, num, node;
    
    //initialize with 0
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
    
    for (i = 0; i < n; i++){
        scanf("%d", &id);
        scanf("%d", &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            M[id-1][node-1] = 1;
        }
    }
}

void bfs(){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    //starting point
    int s = 0;
    color[s] = GRAY;
    d[s] = 0;
    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 argc, const char * argv[]) {
    scanf("%d", &n);
    
    encode_graph();
    bfs();
    
    for(int i = 0; i < n; i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
    
//    std::cout << "Hello, World!\n";
    return 0;
}

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

int main()
{
  int n;
  cin >> n;
  Graph G(n + 1);
  int u, k, v;
  for (int i = 0; i < n; i++)
  {
    cin >> u >> k;
    for (int j = 0; j < k; j++)
    {
      cin >> v;
      G.at(u).push_back(v);
    }
  }
  vector<int> dist(n + 1, -1);
  queue<int> que;
  dist.at(1) = 0;
  que.push(1);
  int nv;
  while (!que.empty())
  {
    v = que.front();
    que.pop();
    for (int i = 0; i < G.at(v).size(); i++)
    {
      nv = G.at(v).at(i);
      if (dist.at(nv) != -1)
      {
        continue;
      }
      dist.at(nv) = dist.at(v) + 1;
      que.push(nv);
    }
  }
  for (int i = 1; i <= n; i++)
  {
    cout << i << " " << dist.at(i) << endl;
  }
}

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

using namespace std;

vector<int> G[100 + 5];
int d[105];
bool travel[105];

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n;
    cin >> n;
    int u, k, now;
    for (int i = 1; i <= n; ++i) {
        cin >> u >> k;
        while (k--) {
            cin >> now;
            G[u].push_back(now);
        }
    }
    fill(d, d + 105, -1);
    queue<pair<int, int> > q;
    q.push({1, 0});
    while (!q.empty()) {
        int now = q.front().first;
        int distance = q.front().second;
        q.pop();
        if (travel[now]) continue;
        travel[now] = true;
        d[now] = distance;
        for (auto i : G[now]) {
            q.push({i, distance + 1});
        }
    }
    for (int i = 1; i <= n; ++i) {
        cout << i << ' ' << d[i] << '\n';
    }
}

C++: //push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include<stdio.h>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


int main(){
  int i,j,u,k,v;

  scanf("%d",&num);
  for(i=1; i<=num; i++){
    color[i] = WHITE;
    for(j=1; j<=num; j++){
      G[i][j] = 0;
    }
  }
  for(i=1; i<=num; i++){
    scanf("%d%d",&u,&k);
    for(j=1; j<=k; j++){
      scanf("%d",&v);
      G[u][v] = 1;
    }
  }

  bfs(1);

  for(i=1; i<=num; i++) {
    if(d[i] == INFTY) printf("%d -1\n",i);
    else printf("%d %d\n",i,d[i]);

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100;
int n;
int d[N+1];
vector< vector<int> > ns;
queue<int> q;

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

  vector<int> v;
  ns.push_back(v);
  cin >> n;
  FOR(i, 1, n+1) {
    int id, k, x; cin >> id;
    vector<int> v;
    cin >> k;
    REP(j, k) {
      cin >> x;
      v.push_back(x);
    }
    ns.push_back(v);
  }

  FOR(i, 1, n+1) d[i] = -1;

  int u = 1;
  q.push(u);
  d[u] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    REP(i, ns[u].size()) {
      int v = ns[u][i];
      q.push(v);
      if (d[v] == -1) d[v] = d[u] + 1;
    }
  }

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