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++: #include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INF=-1;
int n,adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1];
queue<int> Q;

void bfs(){
  for(int i=1;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[1]=0;
  color[1]=GRAY;
  Q.push(1);
  while(!Q.empty()){
    int u=Q.front(); Q.pop();
    for(int i=1;i<=n;i++){
      if(adj[u][i] && color[i]==WHITE){
        color[i]=GRAY;
        d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BLACK;
  }
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  bfs();
  for(int id=1;id<=n;id++)
    printf("%d %d\n",id,d[id]);
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

typedef struct{
  int q[10000];
  int head;
  int tail;
}Queue;

Queue que;
int g[N][N]; 
int n;
int d[N];
bool us[N];
void Enque(int data){
  que.q[que.tail]=data;
  que.tail++;
}

int Deque(void){
  int data;
  data=que.q[que.head];
  que.head++;
  return data;
}

bool isEmpty(){
  if(que.tail-que.head==0)return true;
  else return false;
}


void bfs(int s){
  int u,v;
  Enque(s);
  while(true){

    if(isEmpty()==true) {
      break;
    }
    u=Deque();
    //  cout << "u = " << u << endl;
      for(int i=1;i<= n;i++){
 
      if(us[i]==false && g[u][i] == 1){
	v=i;
	//	cout << "i - " << i <<endl;
	us[i]=true;
	d[v]=d[u]+1;
	Enque(v);
      }
    }
  }
}

int main(){

  int n2,data,u;

 //load n
  cin >>n;

  //initialize
  que.tail=0;
  que.head=0;
  for(int i=0;i<N;i++){
    us[i]=false;
    d[i]=-1;
    for(int j=0;j<N;j++){
      g[i][j]=0;
    }
  }

  //load data
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&n2);
    for(int j=0;j<n2;j++){
      scanf("%d",&data);
      g[u][data]=1;
    }
  }
  //execute

  d[1]=0;
  us[1]=true;
  bfs(1);


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

  return 0;
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

int main()
{
    int G[101][102];
    int dist[101];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, a, b, e, v1, v2;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        for(j = 0; j <= n + 1; j++){ G[i][j] = 0; } }

    for(i = 1; i <= n; i++){
        scanf("%d %d", &a, &e);
		G[i][0] = e;  // number of edge.
        for(j = 1; j <= e; j++){
            scanf("%d", &b); G[a][j] = b; } }  // adjacent representation.

    for(i = 1; i <= n; i++) dist[i] = -1;
    f_insert(head, 1);
    dist[1] = 0;

    while(1){
        v1 = b_pop(tail);
		e = G[v1][0];
        for(i = 1; i <= e; i++){
            v2 = G[v1][i];
            if(dist[v2] < 0){
                dist[v2] = dist[v1] + 1;
                f_insert(head, v2);
            }
        }
        if(isEmpty(head, tail)) break;
    };

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

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vector<int> g[101];
bool visited[101]{};
int n, ans[101]{};

void bfs() {
    fill(ans, ans + n + 1, -1);
    ans[1] = 0;
    queue<P> que;
    que.push({1, 0});
    visited[1] = true;
    while (!que.empty()) {
        int u, d;
        u = que.front().first;
        d = que.front().second;
        que.pop();
        REP(i, g[u].size()) {
            if (visited[g[u][i]]) continue;
            que.push({g[u][i], d + 1});
            visited[g[u][i]] = true;
            ans[g[u][i]] = d + 1;
        }
    }
}

int main() {
    cin >> n;
    REP(i, n) {
        int u, m;
        cin >> u >> m;
        REP(j, m) {
            int v;
            cin >> v;
            g[u].push_back(v);
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        cout << i << " " << ans[i] << endl;
    }
    return 0;
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}