Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <math.h>
using namespace std;
typedef long long int ll;
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int n;
int graph[101][101];
int d[101];
int k[101];
bool beg[101];
int cnt=0;
int pre=-6;
queue<int>now;
queue<int>nextg;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        beg[i]=false;
        d[i]=-1;
        int number;
        cin>>number;
        cin>>k[i];
        for(int j=1;j<=k[i];j++)
        {  cin>>graph[i][j];
        }
    }
    for(int j=1;j<=k[1];j++)
    {  now.push(1);
        nextg.push(graph[1][j]);
    }
    d[1]=0;
    beg[1]=true;
    while(now.size())
    {
        int nowt=now.front();
        int nextt=nextg.front();
        
        now.pop();
        nextg.pop();
        if(beg[nextt]==false)
        {
            d[nextt]=d[nowt]+1;
            beg[nextt]=true;
            pre=nowt;
            for(int j=1;j<=k[nextt];j++)
            {
                now.push(nextt);
                nextg.push(graph[nextt][j]);
                //cout<<"now "<<nextt<<"nextg "<<graph[nextt][j]<<endl;
            }
        }
        
    }
    for(int i=1;i<=n;i++)
    {
        if(d[i]<0)
            d[i]=-1;
        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[u] != 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;
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 N;
int D[101],d[101][101];
void bfs(int src){
    int u,k,a;
    bool visit[101];
    for(int i=1;i<=N;++i){
        cin>>u>>k;
        for(int j=0;j<k;++j){
            cin>>a;
            d[u][a]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

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

char s[13][1];
int D[102][102];
int A[103],f[103],w,h,n,sx,sy,gx,gy,e,g[103];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
	//		printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}