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;

void BFS(int matrix[][MAX_NUM], int num, int target, int result[])
{
    queue<int> Q;
    int  distance = 0;
    char state[MAX_NUM] = {0};

    Q.push(target);
    result[target] = distance;
    state[target] = 1;

    while (Q.empty() == false) {
        int index = Q.front();
        Q.pop();

        for (int i = 1; i < num + 1; i++) {
            if ((matrix[index][i] == 1) && (state[i] == 0)) {
                Q.push(i);
                state[i]  = 1;
                result[i] = result[index] + 1;
            }
        }
    }

    return;
}

int main(void)
{
    int num;
    int index, elemNum, tmp;
    int matrix[MAX_NUM][MAX_NUM] = {0};
    int result[MAX_NUM];

    scanf("%d", &num);
    for (int i = 1; i < num + 1; i++) {
        result[i] = -1;
        scanf("%d %d", &index, &elemNum);
        for (int j = 0; j < elemNum; j++) {
            scanf("%d", &tmp);
            matrix[index][tmp] = 1;
        }
    }

    BFS(matrix, num, 1, result);

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

    return 0;
}

C++: //Breadth First Search (?????????) Algorithm
#include<iostream>
#include<queue>
#define WH 0
#define GR 1
#define BL 2
#define INF 2000000
void bfs(int);
int n, *color, **M, *d;
std::queue<int> Q;
int main(){
  std::cin>>n;
  color=new int[n], d=new int[n], M=new int*[n];
  for(int i=0, u, k;i<n;i++){
    M[i]=new int[n];
    color[i]=WH, d[i]=INF;
    for(int j=0;j<n;j++) M[i][j]=0;
    std::cin>>u>>k;
    for(int j=0, v;j<k;j++){
      std::cin>>v;
      M[u-1][v-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++) std::cout<<i+1<<" "<<(d[i]!=INF?d[i]:-1)<<std::endl;
  delete color;
  delete d;
  for(int i=0;i<n;i++) delete M[i];
  delete M;
  return 0;
}

void bfs(int s){
  color[s]=GR, d[s]=0;
  Q.push(s);
  while(!Q.empty()){
    int u=Q.front();
    Q.pop();
    for(int i=0;i<n;i++){
      if(M[u][i]&&color[i]==WH){
        color[i]=GR, d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BL;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

const int N = 100;
bool G[N][N];
int n;
vector<int> d(N, -1);

void bfs(int r) {
    queue<int> q;
    q.push(r);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v = 0; v < n; ++v) {
            if (G[u][v] && d[v] == -1) {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
}

int main() {
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        int u, k;
        scanf("%d %d", &u, &k);
        --u;
        for (int j = 0; j < k; ++j) {
            int v;
            scanf("%d", &v);
            --v;
            G[u][v] = true;
        }
    }
    d[0] = 0;
    bfs(0);
    for (int u = 0; u < n; ++u) {
        cout << u + 1 << " " << d[u] << endl;
    }
}

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

int BFS(int s, int t, const vector<vector<int> >& G){
    vector<int> stock1;
    vector<int> stock2;
    vector<int> *s1p = &stock1;
    vector<int> *s2p = &stock2;
    stock1.push_back(s);
    int d = 0;
    while (1) {
        for (int i = 0; i < s1p->size(); i++) {
            if ((*s1p)[i] == t) return d;
            for (int j = 0; j < G[(*s1p)[i]].size(); j++) {
                s2p->push_back(G[(*s1p)[i]][j]);
            }
        }
        if (s1p == &stock1) {
            s1p = &stock2;
            s2p = &stock1;
        } else {
            s1p = &stock1;
            s2p = &stock2;
        }
        d++;
    }
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << i << " " << BFS(1, i, G) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#define MAX 101
#define NIL -1

using namespace std;


int matrix[MAX][MAX] = {0};
int dis[MAX];
//提交时须对time改名 
int n;


void bfs_visit(int u);
int next(int u);

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			matrix[u][v] = 1;			
		}
	}
	
//显示邻接矩阵 
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			if(j) cout << ' ';
//			cout << matrix[i+1][j+1];		
//		}
//		cout << endl;
//	}

	bfs_visit(1);

	for (int i = 1; i <= n; i++)
	{
		cout << i << ' ';
		cout << dis[i] << endl;
	}
}



void bfs_visit(int u)
{
	
	//dis[]初始化，取值NIL时表示未访问 
	for (int i = 1; i <= n; i++)
		dis[i] = NIL;	
	dis[u] = 0;	
	
	queue<int> Q;
	Q.push(u);
	
//	int d = 1;
//	while (!Q.empty())
//	{
//		u = next(Q.front());
//
//		if (u)
//		{
//			dis[u] = d; 
//			Q.push(u);
//		}
//		else
//		{
//			Q.pop();			
//		}
//		
//		//判断是否进入下一层 
//		if (dis[Q.front()] == d) d++;
//
//	}

//另一种实现 
	while (!Q.empty())
	{
		u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++)
		{
			if (matrix[u][v] != 0 && dis[v] == NIL)
			{
				dis[v] = dis[u] + 1;
				Q.push(v);
			}
		}
	} 
}


int next(int u)
{
	for (int i = 1; i <= n; i++)
	{
		if (matrix[u][i] != 0 && dis[i] == NIL)
			return i;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100,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,v,k,i,j;
  cin>>n;
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) {
      M[i][j]=0;
    }
  }
  for (i=0; i<n; i++) {
    cin>>u>>k;
    u--;
    for (j=0; j<k; j++) {
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}