Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : -1);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		while (k--) {
			int v;
			cin >> v;
			adj[u - 1].push_back(v - 1);
		}
	}

	bfs(n, 0);

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

	return 0;
}

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

#define MAX_N (105)

int n;
bool G[MAX_N][MAX_N];
int dist[MAX_N];
int main()
{
    queue<int> q;
    scanf("%d", &n);
    for ( int i = 1; i <= n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	for ( int j = 0; j < k; j++ ) {
	    int d;
	    scanf("%d", &d);
	    G[u][d] = true;
	}
    }

    memset(dist, -1, (n+1) * sizeof(int));

    dist[1] = 0;
    q.push(1);
    
    while ( !q.empty() ) {
	int cur = q.front(); q.pop();
	for ( int i = 1; i <= n; i++ ) {
	    if ( G[cur][i] && (dist[i] == -1) ) {
		dist[i] = dist[cur] + 1;
		q.push(i);
	    }
	}
    }

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

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n = 0;
int adl[MAX][MAX], deg[MAX];
int adm[MAX][MAX];
int color[MAX];
int d[MAX];
queue<int> queueBfs;


void bfs() {
  for ( int i = 0; i < n; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  queueBfs.push(0);
  color[0] = GRAY;
  d[0] = 0;

  while (!queueBfs.empty()) {
    int top = queueBfs.front();

    for (int i = 0; i < n; i++) {
      if (adm[top][i] == 1 && color[i] == WHITE) {
        queueBfs.push(i);
        color[i] = GRAY;
        d[i] = d[top]+1;
      }
    }

    queueBfs.pop();
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) {
      adm[i][j] = 0;
    }
  }
  for ( int i = 0; i < n; i++ ) {
    int num;
    scanf("%d", &num);
    num--;
    scanf("%d", &deg[num]);
    for ( int j = 0; j < deg[num]; j++) {
      scanf("%d", &adl[num][j]);
      adl[num][j]--;
      adm[num][adl[num][j]] = 1;
    }
  }

  bfs();

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



  return 0;
}

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

const int Max = 105;
int map[Max + 1][Max + 1];

typedef struct{
	bool startflag;
	int distance;
}State;

int main()
{
	int n, k, l, temp;
	std::queue<int> q;
	std::vector<State> state;

	for (int i = 1; i < Max + 1; i++){
		for (int j = 1; j < Max + 1; j++){
			map[i][j] = 0;
		}
	}

	std::cin >> n;
	for (int i = 1; i <= n; i++){
		std::cin >> k >> l;
		for (int j = 1; j <= l; j++){
			std::cin >> temp;
			map[k][temp] = 1;
		}
	}

	State s = { false, 0 };
	for (int i = 0; i <= n; i++){
		state.push_back(s);
	}

	int d = 0;
	while (1){
		if (q.empty() == true){
			bool f = false;
			for (int i = 1; i <= n; i++){
				if (state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					d = 0;
					state[i].distance = d;
					d++;
					f = true;
					break;
				}
			}
			if (f == false){
				break;
			}
		}
		int size = q.size();
		for (int j = 0; j < size; j++){
			for (int i = 1; i <= n; i++){
				if (map[q.front()][i] == 1 && state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					state[i].distance = d;
				}
			}
			q.pop();
		}
		d++;
	}

	for (int i = 1; i <= n; i++){
		std::cout << i << " " << state[i].distance << std::endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int M[105][105];
int n;
int d[105];
int INFTY =(1<<21);

void bfs(int s){
  queue<int> q;
  q.push(s);
  rep(i,n) d[i]=INFTY;
    d[s]=0;
    int u;
    while(!q.empty()){
      u=q.front();
      q.pop();
      rep(v,n){
        if(M[u][v]==0||d[v]!=INFTY) continue;
        d[v]=d[u]+1;
        q.push(v);
      }
    }
    rep(i,n){
      cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
    }

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>n;
    rep(i,n){
      rep(j,n) M[i][j]=0;
    }
    rep(i,n){
      int u,k;
      cin>>u>>k;
      u--;
      rep(j,k){
        int v;
        cin>>v;
        v--;
        M[u][v]=1;
      }
    }
    bfs(0);
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main(){
  queue<int> q;
  int n,a,k,u,c[101];
  vector<int>  d[101];
  cin>>n;for(int i=0;i<n;i++){
    cin>>u>>k;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }for(int i=0;i<=n;i++) c[i]=-1;
  q.push(1),c[1]=0;
  while(!q.empty()){
   a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1) q.push(d[a][i]),c[d[a][i]]=c[a]+1;
  } for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			if (n[*it - 1].distance > dist || n[*it - 1].distance == -1) {
				n[*it - 1].distance = dist;
				q.push(*it);
			}
		}
		dist++;
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}