Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

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

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);

static const int N = 100;
static const int INF = (1<<21);

int d[N]; //距離で訪問状態を管理する
int n, M[N][N];

void bfs(int s){
  queue<int> q;
  q.push(s);

  REP(i,n) d[i] = INF;

  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front(); q.pop();
    REP(v,n){
      //つながっていなかったら，飛ばす
      if(M[u][v] == 0) continue;
      //既に訪れていたら，飛ばす
      if(d[v] != INF) continue;

      d[v] = d[u] + 1;
      q.push(v);
    }
  }

  REP(i,n){
    cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i] ) << endl;
  }




}
int main(){
  int u, k, v;
  cin >> n;
  REP(i,n){
    REP(j,n) M[i][j] = 0;
  }

  REP(i,n){
    cin >> u >> k;
    --u;
    REP(i,k){
      cin >> v;
      --v;
      M[u][v] = 1;
    }
  }

  bfs(0);
  return 0;
}

C++: 
//push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include<stdio.h>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


int main(){
  int i,j,u,k,v;

  scanf("%d",&num);
  for(i=1; i<=num; i++){
    color[i] = WHITE;
    for(j=1; j<=num; j++){
      G[i][j] = 0;
    }
  }
  for(i=1; i<=num; i++){
    scanf("%d%d",&u,&k);
    for(j=1; j<=k; j++){
      scanf("%d",&v);
      G[u][v] = 1;
    }
  }

  bfs(1);

  for(i=1; i<=num; i++) {
    if(d[i] == INFTY) printf("%d -1\n",i);
    else printf("%d %d\n",i,d[i]);
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
bool G[MAX + 1][MAX + 1];
int col[MAX + 1];
int d[MAX + 1];

void init(int n) {
	for (int i = 1; i <= n; i++) {
		col[i] = WHITE;
		d[i] = -1;
		for (int j = 1; j <= n; j++)
			G[i][j] = 0;
	}
}

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

int main() {
	cin >> n;
	init(n);
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	bfs(1);
	for (int u = 1; u <= n; u++) {
		cout << u << " " << d[u] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;


int main(){
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;++i){
    int id,k;
    cin >> id >> k;
    id--;
    for(int j=0;j<k;++j){
      int v;
      cin >> v;
      v--;
      g[id].push_back(v);
    }
  }
  vector<int> dist(n,-1);
  queue<int> que;
  dist[0]=0;
  que.push(0);
  while(!que.empty()){
    int v=que.front();
    que.pop();
    for(auto nv : g[v]){
      if(dist[nv]!=-1)continue;
      dist[nv]=dist[v]+1;
      que.push(nv);
    }
  }
  for(int i=0;i<n;++i){
    cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int G[100][100];
int d[100];
int color[100];
int n;
queue<int> qu;
void bfs ()
{
    color[0] = 1;
    d[0] = 0;
    qu.push(0);
    while (!qu.empty())
    { 
        int u = qu.front(); qu.pop();
        for (int i = 0; i < n; i++)
        {
            if (G[u][i] == 1 && color[i] == 0)
            {
                color[i] = 1;
                d[i] = d[u] + 1;
                qu.push(i);
            }
        }
        color[u] = 2;
    } 
    return;
}

int main()
{
    int k, u, v;
    cin>>n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 

    for (int i = 0; i < n; i++)
    {   
        cin>>u>>k;
        for (int j = 0; j < k; j++)
        {
            cin>>v;
            G[u-1][v-1] = 1;
        } 
    }

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

C++: #include<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int INF=1<<30;
int n,graph[101][101],v[101];
void bfs(int s){
    queue< int > q;
    fill(v,v+101,INF);
    q.push(s);
    v[s]=0;
    while(!q.empty()){
        int p=q.front();q.pop();
        FOR(i,1,n+1){
            if(!graph[p][i]) continue;
            if(v[i]==INF){
                v[i]=v[p]+1;
                q.push(i);
            }
        }
    }
    FOR(i,1,n+1){
        printf("%d ",i);
        printf("%d\n",v[i]!=INF?v[i]:-1);
    }
}
int main(){
    int u,k,e;
    scanf("%d",&n);
    REP(i,n){
        scanf("%d%d",&u,&k);
        REP(j,k){
            scanf("%d",&e);
            graph[u][e]=1;
        }
    }
    bfs(1);
    return 0;
}