Go: package main

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

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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