Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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