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
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

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

	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++: #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int id);
	int dist;
};

Node::Node()
{
	next_num = 0;
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node Node_list[NODE_NUM + 1];

bool Node::Search(int id)
{
	for (int k = 0; k < next_num; k++) {
		if (Node_list[next_node_list[k]].dist == -1) {
			Node_list[next_node_list[k]].dist = dist + 1;
		}
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == id) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		que.push(next_node_list[j]);
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u;
		cin >> k;
		Node_list[u] = Node();
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			Node_list[u].AddNextNode(v);
		}
	}
	
	//dist
	for (int i = 1; i <= n; i++) {
		Node_list[i].dist = -1;
	}

	Node_list[1].dist = 0;
	for (int i = 2; i <= n; i++) {
		bool ret = Node_list[1].Search(i);
		if (ret) continue;
		if (que.empty()) continue;
		do {
			ret = Node_list[que.front()].Search(i);
			que.pop();
		} while (!ret);
		while (!que.empty()) {
			que.pop();
		}
	}

	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << Node_list[i].dist << "\n";
	}
	
	return 0;
}

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