Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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;
}

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<stdio.h>
#include<stack>
#include<iostream>
#include<string>
using namespace std;

int n = 0;
int u = 0;
int k[101];
int v[101][101];
int Short[101] = { 0 };
//into n = 0;
int p = 0;
int fptr = 0;
int lptr = 0;
int x1[10000];
int y1[10000];
void Enqueue(int y, int x)
{
	x1[lptr] = x;
	y1[lptr] = y;
	lptr = (lptr + 1) % n;
}
void bfs(int l) {
	int i;
	while (fptr!=lptr) {
		l=x1[fptr];
		p=y1[fptr];
		fptr = (fptr + 1) % n;
		if(Short[l]!=-1){
			continue;
		}
		for(i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(p+1,v[l][i]);
			}
		}
		//Enqueue(v[l][in]);
		Short[l]=++p;
	}
}
int main()
{
	int i;
	int j;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u;
		cin >> k[u];
		for (j = 0; j < k[u]; j++) {
			cin >> v[u][j];
		}
	}
	Enqueue(-1,1);
	for(i=0;i<n+1;i++){
		Short[i]=-1;
	}
	bfs(0);

	for (i = 1; i < n + 1; i++) {
		cout << i << " " << Short[i] << endl;
	}
}