Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

queue<pair<int,int> > qu;
vector<int> v[111];
int root;
int b;
int d[111];
pair<int, int> pa;
int g;

void wide(int num){

  memset(d,-1,sizeof(d));
  d[num] = 0;
  qu.push(make_pair(num,d[1]));

  while(!qu.empty()){

    pa = qu.front();
    qu.pop();
    int id = pa.first;

    for(int i = 0; i < (int)v[id].size(); i++){
      if(d[v[id][i]] == -1){
	d[v[id][i]] = pa.second + 1;
	qu.push(make_pair(v[id][i],d[v[id][i]]));
      }
    }
  }
  for(int i = 1; i <= g; i++){
    cout << i << " " << d[i] << endl;
  }

}

int main(){

 
  cin >> g;
  int n;
  for(int i = 0; i < g; i++){
    cin >> n >> b;
    for(int j = 0; j < b; j++){
      cin >> root;
      v[n].push_back(root);
      v[root].push_back(n);
    }
  }

  wide(1);

}

C++: #include<iostream>
#include<queue>
using namespace std;
const int maxx=105;
static const int INFTY = (1<<21);
#define WHITE 0
#define GRAY 1
#define BLACK 2
int graph[maxx][maxx];
int color[maxx];
int n;

int d[maxx];//通过距离管理访问状态
 
void bfs(int u)
{
	queue<int> q;
	q.push(u);
	
	int s;
	 
	for(int i=0;i<n;i++)
	{
		d[i]=INFTY;
	}
	d[u]=0; 
	while(!q.empty())
	{
		s=q.front();
		q.pop();
		for(int v=0;v<n;v++)
		{
			if(graph[s][v]==0)
				continue;
			if(d[v]!=INFTY)
				continue;
			d[v]=d[s]+1;
			q.push(v);
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<i+1<<" "<<(d[i]==INFTY?(-1):d[i])<<endl;
	}	
}
int main()
{
	cin>>n;
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			graph[i][j]=0;
	}
	for(i=0;i<n;i++)
	{
		int index,nn;
		cin>>index>>nn;
		for(j=0;j<nn;j++)
		{
			int ee;
			cin>>ee;
			graph[index-1][ee-1]=1;
		}
	}
	bfs(0);
	return 0;
}

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

#define N 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int Q[N], M[N][N], color[N], d[N], f = 1, c = 1;

void Enqueue(int n) {
  Q[f] = n;
  f++;
  if ( f == N ) f = 1;
}

int Dequeue() {
  int x = c;
  c++;
  if ( c == N ) c = 1;
  return Q[x];
}

void BFS ( int s ) {
  int u;
  for ( int i = 1; i <= N; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  color[s] = GRAY;
  d[s] = 0;
  Enqueue(s);
  while(f!=c ) {
    u = Dequeue();
    for ( int i = 1; i <= N; i++ ) {
      if ( color[i] == WHITE && M[u][i] == 1 ) {
	color[i] = GRAY;
	d[i] = d[u] + 1;
	Enqueue(i);
      }
    }
    color[u] = BLACK;
  }
}

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

C++: #include<iostream> #include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

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

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



int main() {
    int N;
    cin >> N;

    vector<vector<int>> G(N+1);
    rep(i, N) {
        int u, k, vi;
        cin >> u >> k;
        rep(j, k) cin >> vi, G[u].push_back(vi);
    }

    int dist[N+1], d=0;
    rep(i, N+1) dist[i] = -1;

    queue<int> que;
    que.push(1);
    dist[1] = 0;

    while (que.size() > 0) {
        int v = que.front();
        que.pop();

        for (auto next_v: G[v]) {
            if (dist[next_v] == -1) {
                que.push(next_v);
                dist[next_v] = dist[v] + 1;
            }
        }
    }

    for (int i=1; i<=N; i++) cout << i << " " << dist[i] << endl;

    return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

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

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

  cin >> n;
  for( int i = 0; i < n; i++)
    {
      for( int j = 0; j < n; j++)
        {
          M[i][j] = 0;
        }
    }

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

  bfs(0);

  return 0;
}