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 <cstdio>
#include <iostream>
#include <queue>
using namespace std;

static const int MAX_NUM = 101;
static const int NO_VISIT  = 0;
//static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void BFS(int *matrix, int num)
{
    int i;
    int state[MAX_NUM]    = {0};
    int distance[MAX_NUM] = {0};
    queue<int> Q;
    int target;
    int dist = 0;
    int *tmp;
    bool first;

    Q.push(1);
    state[1] = END_VISIT;
    while (Q.empty() == false) {
        target = Q.front();
        Q.pop();
        tmp = matrix + target * MAX_NUM;
        first = true;
        for (i = 1; i <= num; i++) {
            if ((*(tmp + i) == 1) && (state[i] == NO_VISIT)) {
                if (first == true) {
                    ++dist;
                    first = false;
                }
                distance[i] = dist;
                Q.push(i);
                state[i] = END_VISIT;
            }
        }
    }

    for (i = 1; i <= num; i++) {
        printf("%d %d\n", i, distance[i]);
    }

    return;
}

int main(void)
{
    int i, j;
    int num;
    int elem;
    int nume;
    int tmp;
    int matrix[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &elem, &nume);
        for (j = 0; j < nume; j++) {
            scanf("%d", &tmp);
            matrix[elem][tmp] = 1;
        }
    }

    BFS(&matrix[0][0], num);

    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

using namespace std;

#define REP(x,l,u) for(ll x = l;x<u;x++) // for loop
#define RREP(x,l,u) for(ll x = l;x>=u;x--) // reverse for loop

/**
 *  debugging stuff
 */
string to_string(string s) {return s;}
string to_string(char c) {return string(1,c);}
template <typename A, typename B> string to_string(pair<A,B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(vector<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }
template <typename A> string to_string(set<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }

/**
 *  more debugging stuff
 */
void debug_out() {cerr << endl;}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }

#ifndef ONLINE_JUDGE
#define debug(...) do { cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); } while (false)
#else
#define debug(...) do { } while (false)
#endif

typedef long long ll;
typedef double db;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL; 
typedef pair<ll,ll> PLL;
typedef vector<string> VS;

template <typename A, typename B> void in(A & a, B & b) { in(a); in(b); }
template <typename A, typename B, typename C> void in(A & a, B & b, C & c) { in(a); in(b); in(c); }

const ll inf = (ll)1e18 + 5;

ll V;

VLL bfs(ll source, VVLL & adj_list) {

    VLL distance((V+2), inf);
    queue<ll> Q;

    Q.push(source);
    distance[source] = 0;

    while (Q.size()) {
        // pop front of queue
        ll at = Q.front();
        Q.pop();

        for (ll to : adj_list[at]) {

            if (distance[to] == inf) {
                Q.push(to);
                distance[to] = distance[at] + 1;
            }

        }
    }

    return distance;
}

namespace SOLVE {	
	void main() {
    

        cin >> V;
        ll vertex;

        VVLL adj_list(V+1);

        // create adjacency list
        REP (i,1,V+1) {
            cin >> vertex;

            ll adj_vertices;

            cin >> adj_vertices;

            REP (j,0,adj_vertices) {

                ll add_vertex;
                cin >> add_vertex;
                adj_list[vertex].push_back(add_vertex);
            }
        }

        VLL distance = bfs(1, adj_list);

        REP(i,1,V+1) {
            if (distance[i] == inf) {
                distance[i] = -1;
            }
            cout << i << " " << distance[i] << endl;
        }
	}
}

signed main() {
	ll t;
	t = 1;
    // cin >> t;
	REP(i,0,t) {
		SOLVE::main();
	}

	return 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 < n; 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 <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<bitset>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;
const int INF = 0x7fffffff;

int gcd(int x,int y){
  int mi,ma;
  ma = max(x,y);
  mi = min(x,y);
  if(ma % mi == 0){
    return mi;
  }
  else{
    return gcd(mi,ma % mi);
  }
}

int g[101][101];
int dist[101];
int seen[101];

int main(){

  long ii,jj,kk;
  vector<int> a;

  memset(g,0,sizeof(g));
  int n;
  cin >> n;
  int v,vn,r;
  for(ii=0;ii<n;ii++){
    cin >> v;
    cin >> vn;
    for(jj=0;jj<vn;jj++){
      cin >> r;
      g[v][r] = 1;
    }
  }
  vector<int> todo;
  memset(seen,0,sizeof(seen));
  for(ii=0;ii<101;ii++){
    dist[ii] = -1;
  }

  dist[1] = 0;
  seen[1] = 1;
  todo.push_back(1);
  //int v;
  while(!todo.empty()){

    v = todo[0];
    todo.erase(todo.begin());
    
    //cout << "v:" << v <<endl;
    for(ii=1;ii<=n;ii++){
      if(g[v][ii] == 1 && seen[ii] == 0){
        dist[ii] = dist[v] + 1;
        seen[ii] = 1;
        todo.push_back(ii);
      }
    }
    
  }

  for(ii=1;ii<=n;ii++){
    cout << ii << " " << dist[ii] << endl;
  }
  
//  for(ii=1;ii<=n;ii++){
//    for(jj=1;jj<=n;jj++){
//      cout << g[ii][jj] ;
//      if(jj!=n){
//        cout << " ";
//      }
//    }
//    cout << endl;
//  }
  
  return 0;
}