Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

#define NO -1

using namespace std;


int adjacencyList[MAX + 1][MAX + 1];
int color[MAX + 1];
int d[MAX + 1];
int n;



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

	for (int i = 0; i < n; i++) {
		cin >> u >> k;

		for (int j = 0; j < k; j++) {
			cin >> v;
			adjacencyList[u][v] = 1;
		}

	}

	bfs(1);

	for (int i = 1; i <= n; i++) {
		cout << i << " ";
		if (color[i] == BLACK) {
			cout << d[i];

		}
		else {			
			cout << -1;
		}
		cout << endl;
	}


	return 0;
}

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

using namespace std;

struct Edge{
	int to;
};

void BFS(vector<vector<Edge>>& G)
{
	queue<int> Q;
	vector<int> depth(G.size(), -1);
	int dep = 0;
	
	depth[0] = dep;
	for(auto i : G[0])
		Q.push(i.to);
	while(!Q.empty()){
		dep++;
		int size = Q.size();

		for(int i = 0; i < size; i++){
			int v = Q.front();
			Q.pop();
			if(depth[v-1] == -1){
				depth[v-1] = dep;
				for(auto i : G[v-1])
					Q.push(i.to);
			}
		}
	}

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

int main(void)
{
	int num;
	cin >> num;

	vector<vector<Edge>> AdjList(num);
	for(int i = 0; i < num; i++){
		int u, k;
		cin >> u >> k;
		for(int j = 0; j < k; j++){
			int v;
			cin >> v;
			AdjList[i].push_back(Edge{v});
		}
	}

	BFS(AdjList);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> e[105];
int res[105];
bool ok[105];
int n;
int inf=1<<30;
int main(){
//	freopen("a.txt","r",stdin);
	cin>>n;
	int u,v,k;
	fill(res,res+103,inf);
	while(cin>>u){
		cin>>k;
		for(int i=0;i<k;i++)cin>>v,e[u].push_back(v);
	}
	queue<pair<int,int> >q;
	q.push({1,0});
	ok[1]=1;
	while(!q.empty()){
		pair<int,int> p=q.front();q.pop();
		int i=p.first;int d=p.second;
		res[i]=min(res[i],d);
		for(int j=0;j<e[i].size();j++)
			if(!ok[e[i][j]])q.push({e[i][j],d+1}),ok[e[i][j]]=1;
	}
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,(ok[i])?res[i]:-1);
	return 0;
}

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

using namespace std;

int main(){
    int n;
    cin >> n;
    
    int g[102][102];
    int d[102];
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            g[i][j] = 0;
        }
        d[i] = -1;
    }
    
    int u,k;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            int v;
            cin>>v;
            g[u][v] = 1;
        }
    }

    int q[102];
    int p = 0;
    q[++p] = 1;
    d[ q[p] ] = 0;
    while(p > 0){
        u = q[p--];
        for(int i=1; i<=n; i++){
            if(g[u][i] == 1 && d[i] == -1){
                g[u][i] = 0;
                d[i] = d[u]+1; 
                q[++p] = i;
              
            }
        }
    }
    
    for(int i=1; i<=n; i++){
        cout<< i << " " << d[i] << endl;
    }
    
    return 0;
}

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

using namespace std;

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

int n, m[N][N];
int dist[N];

void bfs(int idx)
{
	for (int i = 0; i < n; ++i) dist[i] = INFTY;

	queue<int> q;
	q.push(idx);
	dist[idx] = 0;

	while (!q.empty())
	{
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v)
		{
			if (m[u][v] == 0) continue;
			if (dist[v] != INFTY) continue;

			dist[v] = dist[u] + 1;
			q.push(v);
		}
	}
	for (int i = 0; i < n; ++i)
		cout << i + 1 << " " << ((dist[i] == INFTY) ? (-1) : dist[i]) << endl;
}

int main()
{
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			m[i][j] = 0;
		}
	}
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 0; j < d; ++j)
		{
			int v; cin >> v;
			m[u-1][v-1] = 1;
		}
	}

	bfs(0);

	return 0;
}