Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: // ALDS1_11_C: Breadth First Search
#include <iostream>
#include <vector>
#include <algorithm>
#define INF 1000
bool debug = false;

std::vector<std::vector<int>> adjacency_list;
std::vector<int> node_distance;

void SetnodeRelationship() {
    int total; std::cin >> total;
    int max = total+1;
    /* initialize adjacency_list */
    adjacency_list.resize(max);
    for (int i=1; i<adjacency_list.size(); i++) {
        adjacency_list.at(i).resize(max);
        for (int j=1; j<adjacency_list.at(i).size(); j++) {
            adjacency_list.at(i).at(j) = 0;
        }
    }

    /* initialize node_distance */
    node_distance.resize(max);
    for (int i=1; i<node_distance.size(); i++) {
        node_distance.at(i) = INF;
    }

    /* set node relationship */
    for (int i=1; i<adjacency_list.size(); i++) {
        int node, outdegree; std::cin >> node >> outdegree;
        for (int j=0; j<outdegree; j++) {
            int tmp; std::cin >> tmp;
            adjacency_list.at(node).at(tmp) = 1;
        } 
    }
}

void BreadthFirstSearch(int node, int distance) {
    node_distance.at(node) = distance;

    /* Recerve */
    for (int i=1; i<adjacency_list.at(node).size(); i++) {
        /* have next vertex? */
        if (adjacency_list.at(node).at(i) == 1) {
            /* is the path to the next vertex SHORTEST distance? */
            if (node_distance.at(i) > distance+1) {
                if (debug) std::cout << "BreadthFirstSearch(" << i << ", " << distance+1 << ")\n";
                BreadthFirstSearch(i, distance+1);
            }
        }
    }
}

int main() {
    SetnodeRelationship();
    BreadthFirstSearch(1, 0);

    for (int i=1; i<node_distance.size(); i++) {
        if (node_distance.at(i) == INF) {
            node_distance.at(i) = -1;
        }
        std::cout << i << " " << node_distance.at(i) << "\n";
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#define NUM 100
#define NIL -1
using namespace std;

int V[NUM + 1][NUM + 1];
int dist[NUM + 1];
int node_num;
int t;
int pos;
queue<int> Q;

void init(){
    int V[NUM + 1][NUM + 1];
    for(int i = 1; i <= NUM; i++){
        for(int j = 1; j <= NUM; j++){
            V[i][j] = 0;
        }
    }

    for(int i = 0; i <= NUM; i++) {
        dist[i] = NIL;
    }
    
    t = 0;
}

void get_adj(){
    scanf("%d", &node_num);

    for(int i = 0; i < node_num; i++){
        int num1;
        scanf("%d", &num1);

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

        int num2;
        for(int j = 0; j < n; j++){
            scanf("%d", &num2);
            V[num1][num2] = 1;
        }
    }
}

void touchNextNode(int n){
    t += 1;
    for(int i = 1; i <= node_num; i++){
        if(V[n][i] == 1 && dist[i] == NIL){
            Q.push(i);
            dist[i] = dist[n] + 1;
        }
    }
    Q.pop();
}

void BFS(){
    Q.push(1);
    dist[1] = t;
    touchNextNode(1);
    
    while(Q.size()){
        int pos = Q.front();
        touchNextNode(pos);
    }
}

int main(void){
    init();
    get_adj();
    BFS();
    
    for(int i = 1; i <= node_num; i++){
        printf("%d %d\n", i, dist[i]);
    }

}

C++: #include<stdio.h>
 
 
int G[101][101], q1[101], q2[101], n;
 
void f(int a, int b){
  int i;
  q1[a] = b;
  for(i = 1; i <= q2[a]; i++){
    if(b + 1 < q1[G[a][i]]) f(G[a][i], b + 1);
  }
}
 
int main(){
  int i, j, m;
 
  scanf("%d", &n);
 
  for(i = 1; i <= n; i++){
    q1[i] = 100000000;
    scanf("%d", &m);
    scanf("%d", &q2[m]);
 
    for(j = 1; j <= q2[m]; j++) scanf("%d", &G[m][j]);
  }
 
  f(1, 0);
 
  for(i = 1; i <= n; i++){
    if(q1[i] == 100000000) q1[i] = -1;
  }
 
  for(i = 1; i <= n; i++) printf("%d %d\n", i, q1[i]);
 
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define white 0
#define black -1
#define grey 1
#define infly 1<<21
using namespace std;
int a[110][110],n;
int d[110];
int color[110];

void bfs()
{
    memset(d,0,sizeof(d));
    queue<int> Q;
    for(int i=1;i<=n;i++)
        d[i]=infly;
    d[1]=0;
    for(int i=1;i<=100;i++)
        color[i]=white;
    Q.push(1);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for(int i=1;i<=n;i++)
        {
            if(a[u][i]==0)
                continue;
            if(d[i]!=infly)
                continue;
                Q.push(i);
                d[i]=d[u]+1;
        }
    }
    for(int i=1;i<=n;i++)
   {
       cout<<i<<" "<<((d[i]==infly)?(-1):d[i])<<endl;
   }

}

int main()
{
   int key,d,x;
   memset(a,0,sizeof(a));
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {
       scanf("%d%d",&key,&d);
       for(int j=1;j<=d;j++)
       {
        scanf("%d",&x);
        a[key][x]=1;
       }
   }
   bfs();
   return 0;
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<std::endl;
    }
    return 0;
}