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

vector<int> bfs(vector<vector<int>> &adj_list)
{
    vector<int> res(adj_list.size(), -1);
    vector<bool> visit(adj_list.size(), 0);
    queue<int> q;
    q.push(0);
    visit[0] = 1;
    res[0] = 0;

    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < adj_list[u].size(); i++)
        {
            int v = adj_list[u][i];
            if (visit[v] == 0)
            {
                visit[v] = 1;
                res[v] = res[u] + 1;
                q.push(v);
            }
        }
    }

    return res;
}

int main()
{
    int n;
    cin >> n;

    vector<vector<int>> adj_list(n);
    for (int i = 0; i < n; i++)
    {
        int id, k;
        cin >> id >> k;
        for (int j = 0; j < k; j++)
        {
            int u;
            cin >> u;
            adj_list[id - 1].push_back(u - 1);
        }
    }

    // for (int i = 0; i < n; i++)
    // {
    //     for (int j = 0; j < adj_list[i].size(); j++)
    //     {
    //         cout << adj_list[i][j] << ", ";
    //     }
    //     cout << endl;
    // }

    vector<int> res = bfs(adj_list);
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << res[i] << endl;
    }

    return 0;
}

C++: /*
?????????: Graph I - Breadth First Search
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
?????°
19:08 ??°?????????????????????????????£??¨??????????????????????????§???????????????????????£?????????
19:08 ???????????\????????\????????°??????????????????????¢????
19:09 ????????°???????????????????§£
19:13 ??¨???????????????????????£???
19:19 BFS????¨???¶???????¶??????????????????§??¨?????????????????????????????????
19:28 ???????????????????§?????????????????????£????????§????????????????????????????????????????????§????????????????????£????????????
19:29 ?????????????????????????????????????????? 1 ????????????????????????????????????????????????????????¢??¨??????-1 ?????????????????????????????????
*/
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#define INF 114514
using namespace std;

int main()
{
	int n; cin>>n;
	vector< set<int> > graphdat(n+1);  
	for(int i=0;i<n;i++){
		int u; int k; cin >> u >> k;
		set<int> v;
		for(int j=0;j<k;j++){
			int tmp;
			cin >> tmp;
			v.insert(tmp);
		}
		graphdat[u] = v;
	}

	vector<int> distance(n+1);
	distance[1] = 0;
	for(int i=2;i<=n;i++){
		distance[i] = INF;
	}

	queue<int> Q;
	Q.push(1);

	while(!Q.empty()) {
		int cur = Q.front();
		Q.pop(); // pop?????????????????????????????¨?¬???\??§??????
		for(int dest=1; dest<=n; dest++) {
			if(graphdat[cur].find(dest) != graphdat[cur].end()) { // ??????????????????
				if(distance[dest] > distance[cur] + 1) { // ??´??°??§????????????
					distance[dest] = distance[cur] + 1;
					Q.push(dest);
				}
			}
		}
	}

	for(int i=1;i<=n;i++){
		cout << i << " " << (distance[i] == INF ? -1 : distance[i])<< endl;
	}

}

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];
    queue<int> Q;
    int target;
    int dist = 0;
    int *tmp;
    bool first;

    for (i = 1; i <= num; i++) {
        distance[i] = -1;
    }

    Q.push(1);
    state[1] = END_VISIT;
    distance[1] = 0;

    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)) {
                distance[i] = distance[target] + 1;
                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++: #include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge
{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
//using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;
const double PI = acos(-1.0);

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N]; // 距離で訪問状態(color)を管理する

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()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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;
}