Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

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

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={};
int R[110][110]={};
int n,a,b,k;
void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for(int dst=1;dst<n+1;dst++){
			if(R[cur][dst]==1 && D[dst]==0){
				D[dst] =D[cur]+1;
				Q.push(dst);
			}
		}
	}
}
int main() {
	cin >> n;
	for(int i=0;i<n;i++){
		cin>>a;
		cin>>k;
		for(int j=0;j<k;j++){
		cin>>b;
		R[a][b]=1;
		}
	}
	bfs(1);
	cout <<"1 0"<<endl;
	for(int m=2;m<n+1;m++){
		if(D[m]==0){
		cout <<m<<" "<<-1<<endl;
		}else{
		cout <<m<<" "<<D[m]<<endl;
	}
	}
	return 0;
}

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

int main() {

	int n;
	std::cin >> n;
	std::vector<std::vector<int>> v(n);

	for (int i{}; i < n; ++i) {
		int u, k;
		std::cin >> u >> k;
		for (int j{}; j < k; ++j) {
			int t;
			std::cin >> t;
			v[u - 1].emplace_back(t - 1);
		}
	}


	std::vector<int> dist(n, 1<<25 );
	dist[0] = 0;
//	for (int i{0}; i < n; ++i) {
		std::queue<int> q;
		q.emplace(0);
		while (!q.empty()) {
			auto here = q.front(); q.pop();
			
			for (const auto&e : v[here]) {
				if (e!=0&&dist[e] > dist[here] + 1) {
					dist[e] = dist[here] + 1;
					q.emplace(e);
				}
			}

		}
	

	for (int i{}; i < n; ++i) {
		std::cout
			<< i + 1<< " "
			<<( dist[i] == (1 << 25) ? -1 : dist[i] )<< "\n";
	}

	return 0; 
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

for(int a=0;a<n;a++){
    if (D[a]==0){
        D[a]=-1;
}
    D[0]=0;
    cout<<a+1<<" "<<D[a]<<endl;
}
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define N 100
#define INF (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define rep(i, n) for(int i = 0; i < (n); ++i)

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


void bfs(int r)
{
    //initialization
    rep(i, n) color[i] = WHITE;
    rep(i, n) d[i] = INF;

    queue<int> Q;
    Q.push(r);
    color[r] = GRAY;
    d[r] = 0;

    int u;
    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for (int v = 0; v < n; ++v){
            if (M[u][v] != 0 && d[v] == INF){
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
    }
    for(int i = 0; i < n; ++i){
        cout << i+1 << " " << ((d[i] == INF)? -1 : d[i]) << endl;
    }

}

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

    scanf("%d", &n);
    for(u = 0; u < n; ++u){
        scanf("%d %d", &u, &k);
        --u;
        for(int j = 0; j < k; ++j){
            scanf("%d", &v);
            --v;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}

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

// 頂点s+1から出発する場合の幅優先探索
void bfs(int s, int n, vector<vector<int>>& M, vector<int>& d) {
    static const int INFTY = (1 << 21);

    // 今回は標準ライブラリのキューを使う
    queue<int> q;

    // 各種初期化処理
    // 出発する頂点をpush
    q.push(s);
    // 距離格納配列の初期化
    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
    }
    // 出発点の距離は0
    d[s] = 0;

    // 探索開始
    int u;
    while (!q.empty()) {
        // 先頭を取り出してpopする(訪問)
        u = q.front();
        q.pop();

        // 隣接する訪問可能な頂点をpushする
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0) continue; //訪問不可
            if (d[v] != INFTY) continue; //訪問済み

            d[v] = d[u] + 1; //今回リンクのコストは1固定
            q.push(v);
        }
    }

    // 頂点1から各頂点までの距離を出力
    // INFTYが入っていた場合は到達不可能なので-1を出力
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}


int main() {
    static const int N = 110;
    // n : グラフの頂点数
    // M : 隣接行列(グラフを表現)
    // d : 任意の頂点から各頂点への距離格納配列(INFTYで初期化)
    int n, u, k, v;
    vector<int> d(N);
    vector<vector<int>> M(N, vector<int>(N));

    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, n, M, d);

    return 0;
}

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

const static int MAX = 100;
int R[MAX][MAX];
int color[MAX], ar[MAX];
int n;

int search(int i){
    std::queue<int> qu;
    if(color[i] == 1){
	return 1;
    }
    qu.push(i);
    color[i] = 1;
    ar[i] = 0;
    while(!qu.empty()){
	int cur = qu.front();
	int flag = 0;
	for(int j = 0; j < n; j++){
	    if(R[cur][j] == 1 && color[j] == 0){
		qu.push(j);
		color[j] = 1;
		ar[j] = ar[cur] + 1;
		flag = 1;
	    }
	}
	if(flag == 0){
	    qu.pop();
	}
    }
    return 0;
}

int main(void){
    int u, k, v;
    std::cin >> n;

    for(int i = 0; i < MAX; i++){
	for(int j = 0; j < MAX; j++){
	    R[i][j] = 0;
	}
    }

    for(int i = 0; i < MAX; i++){
	color[i] = 0;
    }

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

    search(0);

    for(int i = 0; i < n; i++){
	if(color[i] == 0){
	    std::cout << i + 1 << " " << -1 << std::endl;
	}else{
	    std::cout << i + 1 << " " << ar[i] << std::endl;
	}
    }

    return 0;
}

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

int main(){
  vector<int> G[100];
  queue<int> q;
  vector<int> d(100,200000000);
  int n,u,k,v,depth;
  bool flg;
  
  cin >> n;
  for (int i=0;i<n;i++){
    cin >> u>> k;
    for (int j=0;j<k;j++){
      cin >> v;
      G[u-1].push_back(v-1);
    }
  }
  for (int vi=0;vi<n;vi++){
    if (d[vi]<=1000)continue;
    depth=0;
    q.push(vi);
    d[vi]=depth;
    while (!q.empty()){
      u=q.front();
      for (int i=0;i<G[u].size();i++){
	v=G[u][i];
	if (d[v]>d[u]+1){
	  q.push(v);
	  d[v]=d[u]+1;
	  flg=true;
	  break;
	}
      }
      if (!flg){
	q.pop();
      }else{
	flg=false;
      }
    }
    
  }
  for (int i=0;i<n;i++)
    cout << i+1 <<" "<< d[i]<<" "<<endl;
  
  return 0;
}