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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

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

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<"\n";
    }
    return 0;
}

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

using namespace std;

static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;

class BFS {
private:
    int color[MAX];
    int n;
    int M[MAX][MAX];
    queue<int> Q;
    int distance[MAX];

public:
    BFS()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                M[i][j] = 0;
        int vertex;
        int degree;
        for(int i = 0; i < n; i++)
        {
            color[i] = WHITE;
            cin >> vertex;
            cin >> degree;
            int to_v;
            for(int j = 0; j < degree; j++)
            {
                cin >> to_v;
                M[vertex - 1][to_v - 1] = 1;
            }
        }
    }

    void bfs()
    {
        for(int i = 0; i < n; i++)
            distance[i] = -1;

        distance[0] = 0;
        Q.push(0);
        color[0] = GRAY;
        while(Q.size() != 0)
        {
            int u = Q.front();
            Q.pop();
            color[u] = BLACK;
            for(int v = 0; v < n; v++)
            {
                if(M[u][v] && color[v] == WHITE)
                {
                    distance[v]  = distance[u] + 1;
                    Q.push(v);
                    color[v] = GRAY;
                }
            }
        }
    }

    void print()
    {
        for(int i = 0; i < n; i++)
            cout << i + 1 << " " << distance[i] << endl;
    }
};

int main()
{
    BFS bfs;
    bfs.bfs();
    bfs.print();
}

C++: #include <iostream>
#include <stdio.h>
#include <queue>
#define N 101
#define INFTY (1<<21)


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

void bfs(int u) {
    std::queue<int> Q;
    Q.push(u);
    for (int i = 0; i < n; i++) d[i] = INFTY;
    d[u] = 0;
    while ( ! Q.empty() ) {
        int j = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[j][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[j] + 1;
            Q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        u--;
        if (k == 0) continue;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
 
#define N 100
#define INFTY 1000000
 
int M[N][N],d[N];
int n,u,k,v;
 
//bfs
void bfs(int s){
 
  queue <int> Q;
  //十分大きな数字に初期化
  for(int i = 0; i < N; i++ ) d[i] = INFTY;
  
  //最初の訪問先
  d[s] = 0;
  Q.push(s);

  //Qが空でないとき
  while ( !Q.empty() ){

    //queueから出す
    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;

      //queueに追加
      Q.push(v);
    }
  }
 
  //出力
  for( int i = 0; i < n; i++ ){
    cout << i+1 << " ";
    if ( d[i] == INFTY ) cout << -1;
    else cout << d[i];
    cout << endl;
  }
 
}
 
 
int main(){

  //初期化
  for( int i = 0; i < n; i++ ){
    for( int j = 0; j < n; j++ ) M[u][v] = 0;
  }
  
  //入力
  cin >> n;
  for( int i = 0; i < n; i++ ){
    cin >> u;
    u--;
    cin >> k;
    for( int j = 0; j < k; j++ ){
      cin >> v;
      v--;
      //つながっているとき
      M[u][v] = 1;
    }
  }
  
  //bfs
  bfs(0);
   
  return 0;
}

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

using namespace std;

vector<int> d(101, -1);
vector<vector<int> > c(101, vector<int>(101, 0));

int n, u, k, v;
typedef pair<int, int> P; 
int bfs(void) {
    queue<P> que;
    que.push(P(0, 0));
    while(que.size()>0) {
        P p = que.front(); que.pop();
        if(d[p.first]==-1) d[p.first] = p.second;
        for(int i=0; i<n; ++i) {
            if(c[p.first][i]==1 && d[i]==-1) que.push(P(i, d[p.first]+1));
        }
    }
}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

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

	set<int> A[n+1];
	int v1, v2, deg;
	r(i, n){
		cin >> v1 >> deg;

		r(j, deg){
			cin >> v2;
			A[v1].insert(v2);
		}
	}

	queue<int> s;
	s.push(1);
	int dist[n+1]{0, 1};

	while(!s.empty()){
		int v1 = s.front(); s.pop();
		for(int v2 : A[v1]){
			if(!dist[v2]){
				s.push(v2);
				dist[v2] = dist[v1]+1;
			}
		}
	}

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

	return 0;
}