Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 999999

int n,M[N][N];
int d[N];
queue<int> q;

void bfs(int s)
{
   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,node;
    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;//row
        if(i==u-1)
        {
            cin >> node;//number of node
            for(int j=0;j<node;j++)
            {
                cin >> v;
                M[i][v-1]=1;
            }
        }
    }

    /*
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout << M[i][j] << " " ;
        }
        cout << endl;
    }*/

    bfs(0);
    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;
    }
}

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

const int MAX = 100;
int n;
int G[MAX + 1][MAX + 1];
int states[MAX + 1];
int d[MAX + 1];
queue<int> Q;

void bfs(int id) {
  d[id] = 0;
  Q.push(id);

  while (!Q.empty()) {
    id = Q.front(); Q.pop();
    for (int i = 0; i < n; i++) {
      if (G[id][i] == 1 && states[i] == 0) {
        Q.push(i);
        states[i] = 1;
        d[i] = d[id] + 1;
      }
    }
  }
}

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

  cin >> n;

  int u, v, k;

  for (int i = 0; i < n; i++) {
    d[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }

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

  bfs(0);

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

  return 0;
}

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

using namespace std;

const int WHITE = 0, GRAY = 1, BLACK = 2;

struct BFS {
    int n;
    vector<vector<int>> g;
    vector<int> depth;
    vector<int> state;
    int time = 0;

    BFS(int n) : n(n) {
        g.assign(n, vector<int>());
        depth.assign(n, -1);
        state.assign(n, WHITE);
    }

    void addEdge(int u, int v) {
        g[u].push_back(v);
    }

    void bfs(int s) {
        queue<int> q;
        state[s] = GRAY;
        depth[s] = 0;
        q.push(s);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            if (state[u] == BLACK) {
                return;
            }
            for (int v : g[u]) {
                if (state[v] == WHITE) {
                    state[v] = GRAY;
                    depth[v] = depth[u] + 1;
                    q.push(v);
                }
            }
            state[u] = BLACK;
        }
    }

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

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    BFS dfs(n);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            dfs.addEdge(u, v);
        }
    }

    //dfs.print();

    dfs.bfs(0);

    for (int i = 0; i < n; i++) {
        cout << (i + 1) << " " << dfs.depth[i] << endl;
    }

    return 0;
}

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

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> d;
	std::list<std::vector<int> > Q;
	int n;

	Graph(int n);

	void BreadthFirstSearch();

};

Graph::Graph(int n){
	A.resize(n);
	d.resize(n);
	for (int i = 0; i < n; i++){
		A[i].resize(n);
		for (int j = 0; j < n; j++){
			A[i][j] = 0;
		}
		d[i] = -1;
	}
	this->n = n;
}

void Graph::BreadthFirstSearch(){
	d[0] = 0;
	std::vector<int> a(2, 0);
	a[1] = 1;
	Q.push_back(a);
	while (!Q.empty()){
		std::vector<int> f = Q.front();
		Q.pop_front();
		for (int i = 0; i < n; i++){
			if (A[f[0]][i] == 1 && d[i] == -1){
				d[i] = f[1];
				a[0] = i;
				a[1] = f[1] + 1;
				Q.push_back(a);
			}
		}
	}
	return;
}

int main(){
	int n, u, k, v;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++){
			scanf("%d", &v);
			G.A[u - 1][v - 1] = 1;
		}
	}

	G.BreadthFirstSearch();

	for (int i = 0; i < n; i++){
		printf("%d %d\n", i+1, G.d[i]);
	}


	return 0;
}

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

using namespace std;



int main() {
	queue<pair<int, int>> q;
	int n,row,size,tmp;
	pair<int, int> ptmp;
	cin >> n;
	int** mat = new int* [n];
	int *distance = new int[n];
	for (int i = 0;i < n;i++) {
		cin >> row >> size;
		mat[row - 1] = new int[n];
		for (int j = 0;j < n;j++) {
			mat[row - 1][j] = 0;
		}
		for (int j = 0;j < size;j++) {
			cin >> tmp;
			mat[row - 1][tmp - 1] = 1;
		}
		distance[i] = -1;
	}
	distance[0] = 0;
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		ptmp = q.front();
		q.pop();
		for (int i = 0;i < n;i++) {
			if (mat[ptmp.first][i] && distance[i] == -1) {
				distance[i] = ptmp.second + 1;
				q.push(make_pair(i, ptmp.second + 1));
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << distance[i] << endl;
	}


}