Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[i] = -1;
    }

    color[u] = black;
    d[u] = 0;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

    for(int i=0; i<n; i++){
        cout << i+1 << " " << d[i] << endl;
    }
    
    return 0;
}

C++: #include<stdio.h>

int main()
{
  int n, h, i, j, k, l, m, z, flag, Q[100], Z[100];
  int G[100][100];
  scanf("%d", &n);
  
  for(i = 0; i < 100; i++ ) Z[i] = -1;
  
  for( i = 0; i < n; i++){
    scanf("%d", &G[i][0]);
    scanf("%d", &G[i][1]);
    for( j = 2; j < 2 + G[i][1]; j++){
      scanf("%d", &G[i][j]);
    }
  }
  
  m = 0; 
  h = 0; 
  Q[h] = 1;
  z = 0;
  Z[1] = z;
  z++;
  for( i = 0; i < n; i++){
    flag = 0;
    k = Q[m];
    l = G[k - 1][1];
    
    for( j = 0; j < l; j++){
      if( Z[ G[k - 1][2 + j] ] == -1 ){
        h++;
        Q[h] = G[k - 1][2 + j];
        Z[ G[k - 1][2 + j] ] = z;
        flag = 1;
      }
    }
    if( flag == 1 ) z++;
    m++;
    if( Q[m] == 0 ) i = i + n;
  }
  
  for(i = 1; i <= n ; i++){
    printf("%d %d\n", i, Z[i] );
  }
  return 0;
}

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){
    printf("before: %d\n", n);
    t += 1;
    for(int i = 1; i <= node_num; i++){
        if(V[n][i] == 1 && dist[i] == NIL){
            printf("i %d\n", i);
            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 <iostream>
#include <vector>
#include <numeric>
#include <cassert>
#include <queue>

const int MAXINT = 1000000000;

void bfs(std::vector<std::vector<int> >& adj_list, std::vector<int>& d, int root = 0 ){
  std::queue<int> target_q;
  // std::queue<int> dist_q;
  target_q.push(root);
  d[root] = 0;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      target_q.push(u);
      if(d[vtx] + 1 < d[u]){
        // std::cout<< "/* message */" << std::endl;
        d[u] = d[vtx] + 1;
      }
    }
  }
}


int main(int argc, char const *argv[]) {
  int n; // .. 100
  std::cin >> n;
  std::vector<std::vector<int> > adj_list(n);
  std::vector<int> d(n, MAXINT);

  for(int q=0; q<n; ++q){
    int i_plus_1, k;
    std::cin >> i_plus_1;
    std::cin >> k;
    for(int p=0; p<k; ++p){
      int j_plus_1;
      std::cin >> j_plus_1;
      // adj[i_plus_1-1][j_plus_1-1] = true;
      adj_list[i_plus_1 - 1].push_back(j_plus_1-1);
    }
  }
  bfs(adj_list, d, 0);

  for(int i=0; i<n; ++i){

    // if(d[i] < MAXINT){
      std::cout << (i+1)
        << " " << (d[i]==MAXINT ? -1 :: d[i])
        << std::endl;
  }


  // for(auto it = adj_list.begin(); it != adj_list.end(); ++it){
  //   std::cout << ": ";
  //   for(auto jt = (*it).begin(); jt != (*it).end(); ++jt){
  //     std::cout << (*jt) << ' ';
  //   }
  //   std::cout << std::endl;
  // }

  return 0;
}

C++: /* 
 * ALDS1_11B.cpp
 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int N;
int ad_k[101];
int ad_list[101][101];
int result[101];

struct Data {
    int d, v;
};

queue<Data> q;

int main(void) {

    scanf("%d", &N);
    for (int i = 1, id; i <= N; ++i) {
        scanf("%d", &id);
        scanf("%d", ad_k + id);
        for (int j = 0; j < ad_k[id]; ++j) {
            scanf("%d", ad_list[id] + j);
        }
    }

    q.push({0, 1});
    while (!q.empty()) {
        Data data = q.front();
        q.pop();

        for (int i = 0; i < ad_k[data.v]; ++i) {
            if (result[ad_list[data.v][i]] != 0 || ad_list[data.v][i] == 1)
                continue;
            result[ad_list[data.v][i]] = data.d + 1;
            q.push({data.d + 1, ad_list[data.v][i]});
        }
    }

    for (int i = 1; i <= N; ++i) {
        printf("%d ", i);
        if (result[i] == 0 && i != 1)
            printf("-1\n");
        else
            printf("%d\n", result[i]);
    }

    return 0;
}